-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.py
488 lines (428 loc) · 18.2 KB
/
classes.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import pygame as pg
from pygame.locals import *
import math, os, sys
from random import randint
import constants
class Spritesheet:
def __init__(self, filename, bgColor):
filename = os.path.join('resources','images','spritesheets',filename)
self._spritesheet = pg.image.load(filename).convert()
self.backgroundColor = bgColor
def get_image(self, x, y, width, height):
image = pg.Surface((width, height))
image.set_colorkey(self.backgroundColor)
image.blit(self._spritesheet, (0, 0), (x, y, width, height))
return image
class Butterfly(pg.sprite.Sprite):
def __init__(self, game, x, y):
super().__init__()
self.spritesheet = Spritesheet('butterfly.png', constants.WHITE)
self.animation = self.load_animation()
self.liveframes = 0
self.game = game
self.image = self.animation[self.liveframes % len(self.animation)]
self.hitbox = Rect(x+6, y+6, 4, 4)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.direction = randint(0,359)
self.type = "butterfly"
self.x = x
self.y = y
self.speed = 1.5
def load_animation(self):
f1 = self.spritesheet.get_image(0, 0, 16, 16)
f2 = self.spritesheet.get_image(16, 0, 16, 16)
f3 = self.spritesheet.get_image(32, 0, 16, 16)
animation = [f1, f1, f1, f2, f2, f2, f3, f3, f3, f3, f3, f2, f2, f2]
return animation
def update(self):
self.liveframes += 1
self.direction += randint(-30,30)
if self.direction < 0:
self.direction += 360
elif self.direction >= 360:
self.direction -= 360
rad = math.radians(self.direction)
yAdjust = math.sin(rad) * self.speed * -1
xAdjust = math.cos(rad) * self.speed
self.x += xAdjust
self.y += yAdjust
if self.x < -16:
self.x += 216
elif self.x > 200:
self.x -= 216
self.rect.x = int(self.x)
self.rect.y = int(self.y)
self.image = self.animation[self.liveframes % len(self.animation)].copy()
self.image = pg.transform.rotate(self.image, self.direction)
self.hitbox.x = self.x+6
self.hitbox.y = self.y+6
class Background(pg.sprite.Sprite):
def __init__(self, filename, yPlacement):
super().__init__()
self.image = pg.image.load(os.path.join('resources','images','backgrounds',filename))
self.rect = self.image.get_rect()
self.rect.left = 0
self.rect.top = yPlacement
class Platform(pg.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pg.Surface((width, height))
self.image.fill(constants.PURPLE)
self.has_texture = False
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.type = "platform"
class Spike(pg.sprite.Sprite):
def __init__(self, game, x, y, direction):
super().__init__()
self.game = game
self.direction = direction
rotation_guide = {"N" : 0, "W" : 90, "S" : 180, "E" : 270}
self.image = pg.image.load(os.path.join('resources','images','spike_{}.png'.format(direction))).convert()
self.image.set_colorkey(constants.WHITE)
self.rect = self.image.get_rect()
self.type = "spike"
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y
class PhysicsObject(pg.sprite.Sprite):
def __init__(self, game):
super().__init__()
self.game = game
def update_position(self, posAdjustment, walls):
'''Moves the object (updates rect) within the confines of the walls, returns dict collision_directions
@param walls: a sprite.Group containing all platforms currently loaded'''
#Assume no collisions
collision_directions = {'top':False, 'bottom':False,
'right':False, 'left':False}
#Update horizontal position
self.x += posAdjustment[0]
self.hitbox.x = int(self.x)
self.rect.x = int(self.x)-4
#Check for collisions after horizontal movement
collisions = self.list_collisions(walls)
for wall in collisions:
#If we were moving towards the right, adjust the rightmost edge of
#our Rect to align with the leftmost edge of the wall
if posAdjustment[0] > 0:
self.hitbox.right = wall.rect.left
collision_directions['right'] = True
elif posAdjustment[0] < 0:
self.hitbox.left = wall.rect.right
collision_directions['left'] = True
#Update self.x after collision adjustment
self.x = self.hitbox.x
self.rect.x = self.x-4
#Update vertical position
self.y += posAdjustment[1]
self.hitbox.y = int(self.y)
self.rect.y = int(self.y)-2
#Check for collisions again after vertical movement
collisions = self.list_collisions(walls)
for wall in collisions:
if posAdjustment[1] > 0:
self.hitbox.bottom = wall.rect.top
collision_directions['bottom'] = True
else:
self.hitbox.top = wall.rect.bottom
collision_directions['top'] = True
self.y = self.hitbox.y
self.rect.y = self.y-2
return collision_directions
def list_collisions(self, walls):
collisions = []
for wall in walls:
if wall.rect.colliderect(self.hitbox):
collisions.append(wall)
return collisions
class Bomb(PhysicsObject):
def __init__(self, game, x, y, width, height, hspeed, vspeed):
super().__init__(game)
self.game = game
self.spritesheet = Spritesheet("bomb.png", constants.WHITE)
self.animations = self.load_animations()
self.image = self.animations['preboom'][0]
self.currentAnimation = "preboom"
self.rect = self.image.get_rect()
self.rect.x = x-4
self.rect.y = y-2
self.hitbox = Rect(x, y, self.rect.width-8, self.rect.height-4)
self.type = "bomb"
self.x = x
self.y = y
self.lifespan = 2*(len(self.animations['preboom'])+len(self.animations['postboom']))
self.hspeed = hspeed
self.vspeed = vspeed
if vspeed > 0:
self.vspeed = 0
if not self.game.down_pressed:
self.vspeed -= 4
self.sound_boom = pg.mixer.Sound(os.path.join('resources', 'sounds', 'boom.wav'))
def update(self):
if self.lifespan > 2*len(self.animations["postboom"]):
self.vspeed += constants.GRAVITY
if self.vspeed > 15:
self.vspeed = 15
self.hspeed *= 0.97
collision_directions = self.update_position([self.hspeed, self.vspeed], self.game.platforms)
if collision_directions['bottom']:
self.vspeed = 0
elif collision_directions['right'] or collision_directions['left']:
self.hspeed = 0
if self.lifespan == 0:
self.kill()
if self.lifespan > 2*len(self.animations["postboom"]): #if lifespan > 30
self.image = self.animations["preboom"][len(self.animations["preboom"])-(self.lifespan - 2*len(self.animations['postboom']))//2-1]
elif self.lifespan == 2*len(self.animations["postboom"]):
self.explode()
else:
self.image = self.animations["postboom"][len(self.animations["postboom"])-self.lifespan//2-1]
self.lifespan -= 1
for spike in self.game.spikes:
if spike.rect.colliderect(self.rect):
self.explode()
self.lifespan = 2*len(self.animations["postboom"])
for butt in self.game.butterflies:
if butt.rect.colliderect(self.rect) and self.lifespan > 2*len(self.animations["postboom"]):
self.explode()
self.lifespan = 2*len(self.animations["postboom"])
def explode(self):
self.hspeed = 0
self.vspeed = 0
explosionZone = Rect(self.rect.left-5, self.rect.top-5, self.rect.width+10, self.rect.width+10)
for spike in self.game.spikes:
if spike.rect.colliderect(explosionZone):
spike.kill()
self.game.spikes.remove(spike)
for butt in self.game.butterflies:
if butt.rect.colliderect(explosionZone):
if randint(1,3) == 3:
self.game.allSprites.add(BombUpgrade(self.game, butt.rect.x, butt.rect.y))
butt.kill()
self.game.butterflies.remove(butt)
self.sound_boom.play()
def load_animations(self):
'''returns a dictionary of lists, each list an animation cycle'''
animations = {}
pb1 = self.spritesheet.get_image(0, 0, 20, 20)
pb2 = self.spritesheet.get_image(20, 0, 20, 20)
pb3 = self.spritesheet.get_image(0, 20, 20, 20)
pb4 = self.spritesheet.get_image(20, 20, 20, 20)
pb5 = self.spritesheet.get_image(0, 40, 20, 20)
pb6 = self.spritesheet.get_image(20, 40, 20, 20)
animations['preboom'] = [pb1]*4 + [pb2]*4 + [pb3]*4 + [pb4]*4 + [pb5]*4 + [pb6]*4
pob1 = self.spritesheet.get_image(60, 40, 20, 20)
pob2 = self.spritesheet.get_image(40, 40, 20, 20)
pob3 = self.spritesheet.get_image(60, 20, 20, 20)
pob4 = self.spritesheet.get_image(40, 20, 20, 20)
pob5 = self.spritesheet.get_image(60, 0, 20, 20)
pob6 = self.spritesheet.get_image(40, 0, 20, 20)
animations['postboom'] = [pob2, pob2, pob1, pob1, pob1, pob2, pob2,
pob3, pob3, pob4, pob4, pob5, pob5, pob6, pob6]
return animations
class Player(PhysicsObject):
def __init__(self, game, x, y, width, height):
super().__init__(game)
self.spritesheet = Spritesheet("player.png", constants.PLAYER_BG)
self.animations = self.load_animations()
self.game = game
self.image = self.spritesheet.get_image(16, 478, 16, 24)
self.hitbox = Rect(x, y, 8, 19)
self.currentAnimation = 'standing'
self.rect = self.image.get_rect()
self.rect.x = x-4
self.rect.y = y-2
self.type = "player"
self.x = x
self.y = y
self.grounded = False
self.airtime = 0
self.vspeed = 0
self.hspeed = 0
self.debugTick = 0
self.canDropBomb = True
self.sound_jump = pg.mixer.Sound(os.path.join('resources','sounds','jump.wav'))
def update(self):
'''Update the player's position and animation'''
#Gravity, speed caps
if not self.grounded:
self.vspeed += constants.GRAVITY
if self.vspeed > 8:
self.vspeed = 8
#Key input / controls
if self.game.right_pressed:
self.hspeed += 2
if self.grounded:
self.currentAnimation = 'walking'
if self.game.left_pressed:
self.hspeed -= 2
if self.grounded:
self.currentAnimation = 'walking'
if self.game.up_pressed:
if self.grounded:
self.grounded = False
self.vspeed = -4
self.sound_jump.play()
self.currentAnimation = 'jumping'
elif self.vspeed < 0:
self.vspeed -= 0.2
if self.game.space_pressed and self.canDropBomb:
if self.game.bombs > 0:
bomb = Bomb(self.game, self.rect.x, self.rect.y, 20, 20, self.hspeed, self.vspeed)
self.game.allSprites.add(bomb)
self.game.bombs -= 1
self.canDropBomb = False
if not self.game.space_pressed:
self.canDropBomb = True
collision_directions = self.update_position([self.hspeed, self.vspeed], self.game.platforms)
if collision_directions['bottom']:
self.grounded = True
self.vspeed = 0
else:
if not self.thereIsGroundBeneathMe():
self.grounded = False
#Screen scrolling, object unloading
if self.hitbox.y > constants.SCROLL_HEIGHT:
scrollBonus = self.hitbox.y - constants.SCROLL_HEIGHT
self.game.scrollLength += scrollBonus
for _ in range(int(self.game.scrollLength) // 20 - self.game.rows_killed):
self.game.rows.pop(0)
self.game.rows_killed += 1
for sprite in self.game.allSprites:
if sprite != self:
sprite.rect.y -= scrollBonus
if sprite.type in ("butterfly", "bomb"):
sprite.y -= scrollBonus
if sprite.rect.bottom < 0:
if sprite.type == "spike":
self.game.spikes.remove(sprite)
elif sprite.type == "platform":
self.game.platforms.remove(sprite)
elif sprite.type == "butterfly":
self.game.butterflies.remove(sprite)
elif sprite.type == "bomb upgrade":
self.game.bomb_upgrades.remove(sprite)
sprite.kill()
self.hitbox.y -= scrollBonus
self.y = self.hitbox.y
self.rect.y = int(self.y-2)
#Player animation
if self.currentAnimation != 'standing' and self.grounded and self.hspeed == 0:
self.currentAnimation = 'standing'
if self.vspeed > 0 and not self.thereIsGroundBeneathMe():
self.currentAnimation = 'falling'
self.image = self.animations[self.currentAnimation]\
[(self.game.ticks_passed // 2) % len(self.animations[self.currentAnimation])]
if self.game.left_pressed:
self.image = pg.transform.flip(self.image, True, False)
elif not self.game.right_pressed and self.currentAnimation in ('jumping', 'falling'):
self.image = self.animations['jumpingstraight'][0]
#Check if we've collided with a spike
self.spike_collision()
#Check if we've collided with a butterfly
self.butterfly_collision()
#Check if we've collided with a bomb upgrade
self.bomb_upgrade_collision()
#Reset hspeed
self.hspeed = 0
def bomb_upgrade_collision(self):
for bu in self.game.bomb_upgrades:
if bu.hitbox.colliderect(self.hitbox):
if self.game.maxbombs < 11:
self.game.maxbombs += 1
bu.kill()
self.game.bomb_upgrades.remove(bu)
def butterfly_collision(self):
for butt in self.game.butterflies:
if butt.hitbox.colliderect(self.hitbox):
self.die()
def spike_collision(self):
for spike in self.game.spikes:
if spike.rect.colliderect(self.hitbox):
triggered = False
if spike.direction == "N" and self.vspeed > 0:
triggered = True
if spike.direction == "W" and self.hspeed > 0:
triggered = True
if spike.direction == "S" and self.vspeed < 0:
triggered = True
if spike.direction == "E" and self.hspeed < 0:
triggered = True
if triggered:
self.die()
def die(self):
self.game.alive = False
self.spawnDeadFox()
self.kill()
pg.mixer.music.stop()
def load_animations(self):
'''returns a dictionary of lists, each list an animation cycle'''
animations = {}
animations['standing'] = [self.spritesheet.get_image(16, 478, 16, 24)]
w1 = self.spritesheet.get_image(86, 439, 16, 24)
w2 = self.spritesheet.get_image(104, 439, 16, 24)
w3 = self.spritesheet.get_image(122, 439, 16, 24)
animations['walking'] = [w2, w2, w2, w3, w3, w3, w3, w2, w2, w2, w1, w1, w1, w1]
jUp = self.spritesheet.get_image(308, 439, 16, 24)
jDown = self.spritesheet.get_image(332, 439, 16, 24)
jStraight = self.spritesheet.get_image(290, 465, 16, 24)
animations['jumping'] = [jUp]
animations['jumpingstraight'] = [jStraight]
animations['falling'] = [jDown]
i1 = self.spritesheet.get_image(86, 465, 16, 24)
animations['idle1'] = [i1, i1, i1, i1, i1, i1, i1, i1, i1, i1, i1]
return animations
def spawnDeadFox(self):
dead_img = self.spritesheet.get_image(152, 491, 16, 24)
deadFox = DeadFox(self.game, self.rect.x, self.rect.y, dead_img)
self.game.allSprites.add(deadFox)
self.game.deadFox = deadFox
def thereIsGroundBeneathMe(self):
slightlyLower = pg.Rect(self.hitbox.left, self.hitbox.top+1, self.hitbox.width, self.hitbox.height)
for wall in self.game.platforms:
if wall.rect.colliderect(slightlyLower):
return True
return False
class DeadFox(pg.sprite.Sprite):
def __init__(self, game, x, y, dead_img):
super().__init__()
self.image = dead_img
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.rect.x = int(self.x)
self.rect.y = int(self.y)
self.vspeed = -4
def update(self):
self.vspeed += constants.GRAVITY
self.y += self.vspeed
self.rect.y = self.y
if self.rect.top > 300:
self.kill()
class BombUpgrade(PhysicsObject):
def __init__(self, game, x, y):
super().__init__(game)
self.game = game
self.image = self.game.guibook['bomb_upgrade']
self.rect = self.image.get_rect()
self.hitbox = Rect(x, y, self.rect.width-8, self.rect.height-4)
self.type = 'bomb upgrade'
self.x = x
self.y = y
self.rect.x = x
self.rect.y = y
self.vspeed = -3
self.hspeed = randint(-2,2)
self.game.bomb_upgrades.append(self)
def update(self):
self.vspeed += constants.GRAVITY
self.hspeed *= 0.97
collision_directions = self.update_position([self.hspeed, self.vspeed], self.game.platforms)
if collision_directions['bottom']:
self.vspeed = 0
elif collision_directions['right'] or collision_directions['left']:
self.hspeed = 0