-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrex_game_sprites.py
343 lines (286 loc) · 12.8 KB
/
trex_game_sprites.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import random
import pygame
class Animated(pygame.sprite.Sprite):
"""Parent class for characters which are consisted of more than one image"""
def __init__(self, default):
super().__init__()
self.current_sprite_index = 0
self.current_list = default
self.vel = 0
def _animate_through(self):
"""Changing trex image accordingly between the images in a list to have an animation behavior"""
self.current_sprite_index += self.vel
if self.current_sprite_index >= len(self.current_list):
self.current_sprite_index = 0
self.image = self.current_list[int(self.current_sprite_index)]
def set_vel(self, new_vel):
self.vel = new_vel
class TRex(Animated):
"""Building Trex sprite"""
def __init__(self, t_game, y):
self.settings = t_game.settings
self.screen = t_game.screen
self.screen_rect = self.screen.get_rect()
# init walking images
self.trex_walking_sprites = []
self._init_walk()
# calling super animated init
super(TRex, self).__init__(self.trex_walking_sprites)
# setting the trex velocity by calling the parent class set_vel
self.set_vel(self.settings.character_animation_velocity)
# init crouching images
self.trex_crouching_sprites = []
self._init_crouching()
# init jumping image
self.jump_image = pygame.image.load("assets/t-rex/t-rex-0.png").convert_alpha()
self.collision_image = pygame.image.load("assets/t-rex/t-rex-4.png").convert_alpha()
# setting current image to start with the jump image
self.image = self.jump_image
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.center = (self.rect.width, self.rect.height)
self.y = y
self.rect.y = self.y - self.rect.height * 0.5
# trex attributes
self.jump_count = self.settings.jump_count
self.jumping = False
self.crouching = False
self.collided = False
def update(self):
"""Sets the current trex image according to trex condition (crouching, jumping, walking)"""
self._set_y()
if not self.jumping and not self.crouching:
if self.current_list != self.trex_walking_sprites:
self.current_list = self.trex_walking_sprites
self._animate_through()
elif self.jumping:
self._jump()
elif self.crouching:
self.jump_count = self.settings.jump_count
if self.current_list != self.trex_crouching_sprites:
self.current_list = self.trex_crouching_sprites
self._animate_through()
def _set_y(self):
"""Sets the y-axis of the trex according to current condition (crouching, jumping, walking)"""
if not self.crouching and not self.jumping:
self.rect.y = self.y - self.rect.height * 0.5
elif self.crouching:
self.rect.y = self.y - self.rect.height * 0.12
def _init_walk(self):
self.trex_walking_sprites.append(pygame.image.load("assets/t-rex/t-rex-1.png").convert_alpha())
self.trex_walking_sprites.append(pygame.image.load("assets/t-rex/t-rex-2.png").convert_alpha())
def _init_crouching(self):
self.trex_crouching_sprites.append(pygame.image.load("assets/t-rex/t-rex-5.png").convert_alpha())
self.trex_crouching_sprites.append(pygame.image.load("assets/t-rex/t-rex-6.png").convert_alpha())
def _jump(self):
y = self.rect.y
self.image = self.jump_image
if self.jump_count >= -self.settings.jump_count:
# identify whether the sprite is moving against the gravity
# while 1 is against and -1 is with
status = 1
if self.jump_count < 0:
status = -1
y -= ((self.jump_count ** 2) * 0.5 * status) / 2
self.rect.y = y
self.jump_count -= 1
else:
self.jumping = False
self.jump_count = self.settings.jump_count
def collide(self):
self.collided = True
if self.crouching:
# setting the y-axis of the trex accordingly, so it won't fell below the ground if the image was
# crouching image
self.rect.y = self.y - self.rect.height * 0.5
self.image = self.collision_image
def reset(self):
self.jump_count = self.settings.jump_count
self.jumping = False
self.crouching = False
self.collided = False
class Ground(pygame.sprite.Sprite):
"""Building Ground sprite"""
def __init__(self, t_game):
super(Ground, self).__init__()
self.image = pygame.image.load("assets/ground/ground.png").convert_alpha()
self.rect = self.image.get_rect()
self.settings = t_game.settings
self.screen = t_game.screen
self.screen_rect = self.screen.get_rect()
self.rect.y = self.screen_rect.height * 0.75
def should_attach_another_ground(self):
"""Determining whether we want to add another ground by checking if the right of the current ground is less or
equal to screen right"""
return self.rect.right <= self.screen_rect.right
def set_left(self, right):
"""Setting the left of the current ground according to the right of the old ground"""
self.rect.left = right - 15
def update(self):
self._check_kill()
self.rect.x -= self.settings.ground_velocity
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.right <= self.screen_rect.left:
self.kill()
class Cloud(pygame.sprite.Sprite):
"""Building Ground sprite"""
def __init__(self, t_game, extra_x=0):
super(Cloud, self).__init__()
self.image = pygame.image.load("assets/cloud/cloud.png").convert_alpha()
self.rect = self.image.get_rect()
self.settings = t_game.settings
self.screen = t_game.screen
self.screen_rect = self.screen.get_rect()
x = self.screen_rect.width + self.rect.width + extra_x
y = random.randrange(self.screen_rect.height * .40, self.screen_rect.height * .65)
self.rect.center = (x, y)
def update(self):
self.rect.x -= self.settings.cloud_velocity
self._check_kill()
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.right <= self.screen_rect.left:
self.kill()
class Cactus(pygame.sprite.Sprite):
"""Building Cactus sprite"""
def __init__(self, t_game, extra_y, current_x):
super(Cactus, self).__init__()
self.id = random.randrange(13)
cactus_file = f"assets/cactus/cactus-{self.id}.png"
cactus_damage_file = f"assets/cactus/cactus-{self.id}-shot.png"
self.image = pygame.image.load(cactus_file).convert_alpha()
self.damaged_image = pygame.image.load(cactus_damage_file).convert_alpha()
self.rect = self.image.get_rect()
self.screen = t_game.screen
self.screen_rect = self.screen.get_rect()
self.settings = t_game.settings
# if current_x was not 0 then we have a previous cactus created,
# and therefore we will place the current cactus to right of the old cactus
# and spacing them with some void
if current_x:
x = current_x + self.rect.width / 2
else:
x = self.screen_rect.width
y = extra_y - self.rect.height / 3
self.rect.center = (x, y)
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect.x -= self.settings.ground_velocity
self._check_kill()
def set_damaged(self):
"""Setting the current image of the cactus to a damaged image to reflect receiving a shooting bullet action"""
self.image = self.damaged_image
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.right <= self.screen_rect.left:
self.kill()
class Star(pygame.sprite.Sprite):
"""Building Star sprite"""
def __init__(self, t_game, extra_x):
super(Star, self).__init__()
star_file = f"assets/star/star-{random.randrange(1, 4)}.png"
self.image = pygame.image.load(star_file).convert_alpha()
self.rect = self.image.get_rect()
self.settings = t_game.settings
self.screen = t_game.screen
self.screen_rect = self.screen.get_rect()
self.x = float(self.screen_rect.width + self.rect.width + extra_x)
self.y = float(random.randrange(self.screen_rect.height * .45, self.screen_rect.height * .60))
self.rect.center = (self.x, self.y)
def update(self):
self.x -= self.settings.star_velocity
self.rect.center = (self.x, self.y)
self._check_kill()
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.right < 0:
self.kill()
class Bird(Animated):
"""Building Bird sprite"""
def __init__(self, t_game, list_of_ys):
self.screen = t_game.screen
self.screen_rect = t_game.screen_rect
self.settings = t_game.settings
self.bird_sprites = [pygame.image.load("assets/bird/bird-1.png"), pygame.image.load("assets/bird/bird-2.png")]
self.damaged_bird_sprites = [pygame.image.load("assets/bird/bird-1-shot.png"),
pygame.image.load("assets/bird/bird-2-shot.png")]
# calling super animated init
super().__init__(self.bird_sprites)
# setting the bird velocity by calling the parent class set_vel
self.set_vel(self.settings.bird_animation_velocity)
self.image = self.bird_sprites[self.current_sprite_index]
self.rect = self.image.get_rect()
# used for circle collision
self.radius = self.rect.width * 0.3
# generating random number between the list of y length
# as 0 is top, 1 is bottom
# in order to place the y of the bird correctly
# so if index == 0 we place top of bird to top of trex with some space (bird height halved)
# and if index == 1 bottom of bird is bottom of trex
index = random.randrange(0, len(list_of_ys))
self.y = 0
if index == 0:
self.rect.top = list_of_ys[0]
elif index == 1:
self.rect.bottom = list_of_ys[1]
else:
self.rect.top = list_of_ys[2]
self.rect.x = self.screen_rect.width
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self._animate_through()
self.rect.x -= self.settings.bird_velocity
self._check_kill()
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.right < 0:
self.kill()
def set_damaged(self):
"""Setting the current image of the cactus to a damaged image to reflect receiving a shooting bullet action"""
self.current_list = self.damaged_bird_sprites
class Moon(pygame.sprite.Sprite):
"""Building Moon sprite"""
def __init__(self, t_game):
super().__init__()
# generating random number to pic a random image from the moon assets
i = random.randint(0, 6)
self.image = pygame.image.load(f"assets/moon/moon-{i}.png")
self.rect = self.image.get_rect()
self.settings = t_game.settings
self.screen = t_game.screen
self.screen_rect = self.screen.get_rect()
self.x = self.screen_rect.width
self.y = self.screen_rect.height * .45
self.rect.center = (self.x, self.y)
def update(self):
self.x -= self.settings.moon_velocity
self.rect.center = (self.x, self.y)
self._check_kill()
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.right < 0:
self.kill()
class Bullet(pygame.sprite.Sprite):
"""Building Bullet sprite"""
def __init__(self, t_game):
super().__init__()
self.screen = t_game.screen
self.screen_rect = t_game.screen.get_rect()
self.settings = t_game.settings
self.left = t_game.trex.rect.right
# placing the bullet just in front of the trex gun which is equivalent to 4361 percent of the height of the trex
self.top = t_game.trex.rect.top + t_game.trex.rect.height * 0.4361
self.image = pygame.surface.Surface((self.settings.bullet_width, self.settings.bullet_height))
self.image.fill(self.settings.items_color)
self.rect = self.image.get_rect()
self.rect.top = self.top
self.rect.left = self.left
def update(self):
self.left += self.settings.bullet_speed
self.rect.center = (self.left, self.top)
self._check_kill()
def _check_kill(self):
"""Killing the sprite if it is beyond the screen"""
if self.rect.left > self.screen_rect.width:
self.kill()