-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.py
160 lines (114 loc) · 5.91 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pygame, csv
class CameraGroup(pygame.sprite.Group):
def __init__(self, game, zone):
super().__init__()
self.game = game
self.zone = zone
self.display_surf = pygame.display.get_surface()
self.offset = pygame.math.Vector2()
self.scroll_delay = 32
self.screen_transition_speed = 32
self.scroll_speed = 32
self.get_zone_size()
# load images
self.bg_surf = pygame.image.load(f'maps/map art/zone_1/07.png').convert_alpha()
self.bg_surf = pygame.transform.scale(self.bg_surf, (self.game.WIDTH, self.game.HEIGHT))
self.cloud_surf = pygame.image.load(f'maps/clouds.png').convert_alpha()
self.cloud_surf = pygame.transform.scale(self.cloud_surf, (self.zone_size))
self.cloud_surf.set_alpha(100)
def get_zone_size(self):
# get size (length and width) of zone in pixels for bg surf...
with open(f'maps/zone_block.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
rows = (sum (1 for row in reader) + 1)
cols = len(row)
self.zone_size = (cols * self.game.TILESIZE, rows * self.game.TILESIZE)
def bg_blit(self):
# display bg image as zoe width divided by screen size...
for x in range(int(self.zone_size[0])//self.display_surf.get_width()):
for y in range (int(self.zone_size[1])//self.display_surf.get_height()):
self.display_surf.blit(self.bg_surf,((x*self.display_surf.get_width()) - int(self.offset[0]), (y*self.display_surf.get_height()) - int(self.offset[1])))
# below is to create a camera box around the target to move the scroll instead of the target moving the scroll if wanted
# get cam position
# if target[0] < self.camera_rect.left:
# self.camera_rect.left = target[0]
# if target[0] > self.camera_rect.right:
# self.camera_rect.right = target[0]
# if target[1] < self.camera_rect.top:
# self.camera_rect.top = target[1]
# if target[1] > self.camera_rect.bottom:
# self.camera_rect.bottom = target[1]
# # cam offset
# self.offset = pygame.math.Vector2(self.camera_rect.left - self.CAMERA_BORDERS['left'], self.camera_rect.top - self.CAMERA_BORDERS['top'])
def keyboard_control(self):
# scroll with keyboard
keys = pygame.key.get_pressed()
self.zone_boundary_limits()
if keys[pygame.K_a] and self.offset[0] > 0:
self.offset[0] -= self.scroll_speed
elif keys[pygame.K_d] and self.offset[0] < self.zone_size[0] - self.display_surf.get_width():
self.offset[0] += self.scroll_speed
if keys[pygame.K_w] and self.offset[1] > 0:
self.offset[1] -= self.scroll_speed
elif keys[pygame.K_s] and self.offset[1] < self.zone_size[1] - self.display_surf.get_width():
self.offset[1] += self.scroll_speed
def zone_boundary_limits(self):
if self.offset[0] <= 0:
self.offset[0] = 0
elif self.offset[0] >= self.zone_size[0] - self.display_surf.get_width():
self.offset[0] = self.zone_size[0] - self.display_surf.get_width()
if self.offset[1] <= 0:
self.offset[1] = 0
elif self.offset[1] >= self.zone_size[1] - self.display_surf.get_height():
self.offset[1] = self.zone_size[1] - self.display_surf.get_height()
def offset_draw(self, target):
self.bg_blit()
self.zone_boundary_limits()
# get room transition when collide with room
for room in self.game.room_dict[self.game.current_zone].values():
if (room[1]).collidepoint(target):
room_pos = room[1].topleft
room_size = room[0]
# normal scrolling only when cutscene not running (cutscene has its own scroll control)
if not self.zone.cutscene_running:
self.offset[0] += (target[0] - self.offset[0] - self.display_surf.get_width() *0.5)/self.scroll_delay
self.offset[1] += (target[1] - self.offset[1] - self.display_surf.get_height() *0.5)/self.scroll_delay * (16/9)
if self.offset[0] <= room_pos[0]:
self.offset[0] += (self.display_surf.get_width()/self.screen_transition_speed)
if self.offset[0] >= room_pos[0]:
self.offset[0] = room_pos[0]
elif self.offset[0] >= room_size[0] - self.display_surf.get_width():
self.offset[0] -= (self.display_surf.get_width()/self.screen_transition_speed)
if self.offset[0] <= room_size[0] - self.display_surf.get_width():
self.offset[0] = room_size[0] - self.display_surf.get_width()
if self.offset[1] <= room_pos[1]:
self.offset[1] += self.display_surf.get_height()/self.screen_transition_speed
if self.offset[1] >= room_pos[1]:
self.offset[1] = room_pos[1]
elif self.offset[1] >= room_size[1] - self.display_surf.get_height():
self.offset[1] -= self.display_surf.get_height()/self.screen_transition_speed
if self.offset[1] <= room_size[1] - self.display_surf.get_height():
self.offset[1] = room_size[1] - self.display_surf.get_height()
# if self.offset[0] <= room_pos[0]:
# self.offset[0] = room_pos[0]
# elif self.offset[0] >= room_size[0] - self.display_surf.get_width():
# self.offset[0] = room_size[0] - self.display_surf.get_width()
# if self.offset[1] <= room_pos[1]:
# self.offset[1] = room_pos[1]
# elif self.offset[1] >= room_size[1] - self.display_surf.get_height():
# self.offset[1] = room_size[1] - self.display_surf.get_height()
else:
self.keyboard_control()
# seperate blits to layer target on top of other sprites
for sprite in sorted(self.sprites(), key = lambda sprite: sprite.rect.centery):
offset = sprite.rect.topleft - self.offset
self.display_surf.blit(sprite.image, offset)
# if sprite.rect.left > self.zone_size[0] or sprite.rect.right < 0 or\
# sprite.rect.top > self.zone_size[1] or sprite.rect.bottom < 0:
# sprite.kill()
def enemy_update(self, target):
#enemy_sprites = [sprite for sprite in self.sprites() if hasattr(sprite, 'sprite_type') and sprite.sprite_type == 'enemy']
#for enemy in enemy_sprites:
for sprite in self.zone.enemy_sprites:
sprite.enemy_update(target)