-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
67 lines (58 loc) · 2.5 KB
/
camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pygame
vec = pygame.math.Vector2
SCREEN_LEFT_BOUNDx = 0
SCREEN_RIGHT_BOUNDx = 1280
class Background(pygame.sprite.Sprite):
"""background"""
def __init__(self, image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.mostRighted = False
self.mostLefted = False
def draw(self, screen):
screen.blit(self.image, (0,0))
def update(self, camera_offset_x, camera_offset_y):
# print(self.rect.left, self.mostLefted)
self.rect.x -= camera_offset_x
self.rect.y -= camera_offset_y
# if self.rect.left < 0 or self.rect.right > 1280:
# pass
if self.rect.left >= 0:
self.mostLefted = True
else:
self.mostLefted = False
if self.rect.right <= 1280:
self.mostRighted = True
else:
self.mostRighted = False
# print(self.rect.right, self.mostRighted)
# elif self.rect.right <= 1280:
# self.mostRighted = True
class Camera:
def __init__(self, player):
self.player = player
self.offset = vec(0, 0)
self.offset_float = vec(0, 0)
self.DISPLAY_W, self.DISPLAY_H = 1280, 720
self.CONST = vec(-self.DISPLAY_W / 2 + player.rect.w / 2, -self.player.ground_y + 20)
self.camera_offset_tracker = vec(0, 0)
def scroll(self, mostRighted, mostLefted):
self.offset_float.x += (self.player.rect.x - self.offset_float.x + self.CONST.x)
# self.offset_float.y += (self.player.y - self.offset_float.y + self.CONST.y)
self.offset.x, self.offset.y = int(self.offset_float.x), int(self.offset_float.y)
if mostLefted:
if self.offset.x <0:
self.offset.x = 0
if mostRighted:
if self.offset.x >0:
self.offset.x = 0
self.camera_offset_tracker.x += self.offset.x
self.camera_offset_tracker.y += self.offset.y
# print(self.camera_offset_tracker.x, self.camera_offset_tracker.y)
# print(self.player.rect.x, self.offset.x)
# self.offset.x = min(self.offset.x, LEFT_BOUND)
# self.offset.x = max(self.offset.x, RIGHT_BOUND)
# self.offset.x = max(self.player.rect.x, self.offset.x)
# self.offset.x = min(self.offset.x, self.player.rect.x +self.player.rect.width - self.DISPLAY_W)
# print(self.offset.x, self.player.rect.x +self.player.rect.width - self.DISPLAY_W)