AI-Project/survival/camera.py

32 lines
948 B
Python
Raw Normal View History

2021-03-04 14:07:44 +01:00
from pygame.rect import Rect
from survival import SCREEN_WIDTH, SCREEN_HEIGHT
2021-03-04 14:07:44 +01:00
class Camera:
def __init__(self, width, height, window):
2021-03-04 14:07:44 +01:00
self.camera = Rect(0, 0, width, height)
self.width = width
self.height = height
self.window = window
def draw(self, image):
image.draw(self.window, self)
def apply(self, pos):
return pos[0] + self.camera.left, pos[1] + self.camera.top
def get_visible_area(self):
2021-03-15 14:10:19 +01:00
return Rect(-self.camera.left, -self.camera.top,
SCREEN_WIDTH - self.camera.left, SCREEN_HEIGHT - self.camera.top)
2021-03-04 14:07:44 +01:00
def update(self, target):
x = -target.pos[0] + int(SCREEN_WIDTH / 2)
y = -target.pos[1] + int(SCREEN_HEIGHT / 2)
2021-03-04 14:07:44 +01:00
x = min(0, x)
y = min(0, y)
x = max(-(self.width - SCREEN_WIDTH), x)
y = max(-(self.height - SCREEN_HEIGHT), y)
2021-03-04 14:07:44 +01:00
self.camera = Rect(x, y, self.width, self.height)