-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
25 lines (19 loc) · 977 Bytes
/
Copy pathcamera.py
File metadata and controls
25 lines (19 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pygame
class Camera:
def __init__(self, screen_width, screen_height, world_width, world_height,scale_x =1 , scale_y =1 ):
self.camera = pygame.Rect(0, 0, screen_width, screen_height)
self.screen_width = screen_width
self.screen_height = screen_height
self.world_width = world_width
self.world_height = world_height
self.scale_x = scale_x
self.scale_y = scale_y
def apply(self, entity):
return entity.rect.move(self.camera.topleft)
def update(self, target):
x = -target.rect.centerx + (self.screen_width // 2)
y = -target.rect.centery + (self.screen_height // 2)
# Limit camera to world bounds
x = max(-(self.world_width - self.screen_width), x) # Right
y = max(-(self.world_height - self.screen_height), y) # Bottom
self.camera = pygame.Rect(x, y, self.world_width / self.scale_x, self.world_height / self.scale_y)