forked from seanmcortes/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprites.py
More file actions
507 lines (446 loc) · 19.1 KB
/
sprites.py
File metadata and controls
507 lines (446 loc) · 19.1 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import os
import pygame
from settings import *
from helper import Animate, Animate_Attack
from menu import GameOverScene
#for the audio inplementation, the following resources were used.
#https://pythonprogramming.net/adding-sounds-music-pygame/
#Disclaimer: all of these sounds were not authored by the writers of this program and are credited to the
#authors below and used in this program under the creative commons licenses listed next to the sounds.
#This program uses these sounds from https://freesound.org/:
#Rock Scrape 2.wav by Benboncan | License: Attribution | https://freesound.org/people/Benboncan/sounds/74441/
#the sound volume levels were altered and any extra silence at the beginning and end of the files were trimmed.
#These sounds were used under creative commons 0, and have had their levels changed and silence trimmed:
#Game sound by chris_schum | License: Creative Commons 0 | https://freesound.org/people/chris_schum/sounds/418149/
#Retro Game sfx_jump bump.wav by mikala_oidua | License: Creative Commons 0 | https://freesound.org/people/mikala_oidua/sounds/365672/
#metal sound, fighting game by evilus | License: Creative Commons 0 | https://freesound.org/people/evilus/sounds/203454/
#this sound has also had some parts removed that were clipping and popping in order to make for a smoother listening experience.
#Game Over Sound by TheZero | License: Creative Commons 0 | https://freesound.org/people/TheZero/sounds/368367/
class GameObject(pygame.sprite.Sprite):
def __init__(self, scene, x, y):
self.scene = scene
self.image = pygame.Surface((TILESIZE, TILESIZE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.interactable = False
self.collidable = False
self.orientation = None
self.enemy = None
self.sliding = False #tells if the object is sliding on the ice
def update(self):
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
"""
Check if enemy has collided with a collidable object
"""
def collision_object(self, dx, dy):
for game_object in self.scene.all_sprites:
if game_object.collidable and (game_object.x == self.x + dx and game_object.y == self.y + dy):
return True
return False
"""
Check if enemy has collided with an ice tile
"""
def collision_ice(self):
for ice in self.scene.ice:
if ice.x == self.x and ice.y == self.y:
return True
return False
"""
Check if enemy has collided with a block object (which moves when collided with)
"""
def collision_block(self, dx, dy):
for block in self.scene.blocks:
if block.x == self.x + dx and block.y == self.y + dy and block.sliding == False:
return True
return False
"""
This object represents the player character
"""
class Player(GameObject):
def __init__(self, scene, x, y):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.players
pygame.sprite.Sprite.__init__(self, self.groups)
self.orientation = RIGHT
self.health = 3
self.prev_orientation = self.orientation # might not need this
self.prev_location = (x, y)
# State
self.sliding = False #tells if the player is sliding on the ice
self.attacking = False
self.hit = False
self.hit_detected = False
# Animation
self.update_delay = PLAYER_IDLE_DELAY
self.update_attack_delay = PLAYER_ATTACK_DELAY
self.hit_delay = HIT_DELAY
self.last_update = pygame.time.get_ticks()
self.animation_index = 0
self.animation_attack_index = 0
self.walking_up = []
self.walking_down = []
self.walking_left = []
self.walking_right = []
self.attacking_up = []
self.attacking_down = []
self.attacking_left = []
self.attacking_right = []
self.damage_up = []
self.damage_down = []
self.damage_left = []
self.damage_right = []
#audio
self.hurt_sound = pygame.mixer.Sound(path.join(MUSIC_FOLDER,"hurt_sound.wav"))
self.gameover_sound = pygame.mixer.Sound(path.join(MUSIC_FOLDER,"game_over.wav"))
self.attack_sound = pygame.mixer.Sound(path.join(MUSIC_FOLDER,"attack.wav"))
# Sprite sheet definition
sprite_sheet = SpriteSheet(PLAYER_SPRITE_SHEET)
self.image = sprite_sheet.get_image(0, 0, 32, 32)
# Add sprites to walking arrays
for x in range(0, 97, 32):
self.walking_right.append(sprite_sheet.get_image(x, 0, 32, 32))
self.walking_left.append(sprite_sheet.get_image(x, 32, 32, 32))
self.walking_down.append(sprite_sheet.get_image(x, 64, 32, 32))
self.walking_up.append(sprite_sheet.get_image(x, 96, 32, 32))
# Add sprites to attacking arrays
for x in range(0, 97, 32):
self.attacking_right.append(sprite_sheet.get_image(x, 128, 32, 32))
self.attacking_left.append(sprite_sheet.get_image(x, 160, 32, 32))
self.attacking_down.append(sprite_sheet.get_image(x, 192, 32, 32))
self.attacking_up.append(sprite_sheet.get_image(x, 224, 32, 32))
for x in range(128, 161, 32):
self.damage_right.append(sprite_sheet.get_image(x, 0, 32, 32))
self.damage_left.append(sprite_sheet.get_image(x, 32, 32, 32))
self.damage_down.append(sprite_sheet.get_image(x, 64, 32, 32))
self.damage_up.append(sprite_sheet.get_image(x, 96, 32, 32))
"""
Check if enemy has collided with a collidable object
"""
def move(self, dx=0, dy=0):
if not self.collision_object(dx, dy):
self.prev_location = (self.x, self.y)
self.x += dx
self.y += dy
self.prev_orientation = self.orientation
self.orientation = (dx, dy)
return True
else:
return False
def collision_enemy(self):
self.hurt_sound.set_volume(self.scene.volume_level)
for enemy in self.scene.enemies:
if enemy.x == self.x and enemy.y == self.y:
pygame.mixer.Sound.play(self.hurt_sound)
self.enemy = enemy
self.enemy.attacking = True
return True
return False
def take_damage(self):
self.health -= 1
if self.prev_location[0] + self.orientation[0] == self.x and\
self.prev_location[1] + self.orientation[1] == self.y:
self.x, self.y = self.prev_location[0], self.prev_location[1]
else:
self.move(self.enemy.direction[0], self.enemy.direction[1])
"""
Kill self if health = 0, render game over scene
"""
def check_health(self):
if self.health <= 0:
self.kill()
self.gameover_sound.set_volume(self.scene.volume_level)
pygame.mixer.Sound.play(self.gameover_sound)
self.scene.game.go_to(GameOverScene(self.scene.game))
"""
Have player interact with different objects:
box: push box one unit in the direction the player is facing
enemy: attack the enemy, deal one damage to health, have enemy enter invulnerable state
"""
def interact(self):
self.attacking = True
self.attack_sound.set_volume(self.scene.volume_level)
pygame.mixer.Sound.play(self.attack_sound)
for object in self.scene.all_sprites:
if object.x == self.x + self.orientation[0] and \
object.y == self.y + self.orientation[1]:
if object in self.scene.enemies: # check if object is an enemy
if object.hit_detected is False: # check if enemy is not invulnerable
object.take_damage()
self.attacking = True
"""
Update the player's state for animations
"""
def update(self):
now = pygame.time.get_ticks()
# Check and animate for damage
if self.hit and not self.hit_detected:
self.last_update = now
self.hit_detected = True
elif self.hit and self.hit_detected:
if now - self.last_update < self.hit_delay:
if self.orientation == UP:
Animate(self, self.damage_up)
elif self.orientation == LEFT:
Animate(self, self.damage_left)
elif self.orientation == DOWN:
Animate(self, self.damage_down)
else:
Animate(self, self.damage_right)
else:
self.last_update = now
self.hit = False
self.hit_detected = False
# Animate player attack animation
elif self.attacking:
if now - self.last_update >= self.update_attack_delay:
self.last_update = now
if self.orientation == UP:
Animate_Attack(self, self.attacking_up)
elif self.orientation == LEFT:
Animate_Attack(self, self.attacking_left)
elif self.orientation == DOWN:
Animate_Attack(self, self.attacking_down)
else:
Animate_Attack(self, self.attacking_right)
# Render idle animation
else:
if now - self.last_update >= self.update_delay:
self.last_update = now
if self.orientation == UP:
Animate(self, self.walking_up)
elif self.orientation == LEFT:
Animate(self, self.walking_left)
elif self.orientation == DOWN:
Animate(self, self.walking_down)
else:
Animate(self, self.walking_right)
if self.sliding == True: #if the player is sliding on the Ice
self.rect.x += (self.orientation[0] * 32)
self.rect.y += (self.orientation[1] * 32)
self.x = (self.rect.x - (self.rect.x % TILESIZE)) / TILESIZE
self.y = (self.rect.y - (self.rect.y % TILESIZE)) / TILESIZE
#stop sliding if they collide into a collidable object
if self.collision_object(self.orientation[0], self.orientation[1]):
self.sliding = False
#stop sliding if they collide with a block
if self.collision_block(self.orientation[0], self.orientation[1]):
self.sliding = False
#stop sliding if they are not on an ice tile
elif self.rect.x % TILESIZE == 0 and self.rect.y % TILESIZE == 0 and not self.collision_ice():
self.sliding = False
#align the rectangle with a tile
if self.sliding == False:
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
else:
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
if self.collision_enemy() and not self.hit_detected:
self.take_damage()
self.hit = True
self.check_health()
"""
The block class is used for the sliding boxes
"""
class Block(GameObject):
def __init__(self, scene, x, y):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.blocks
pygame.sprite.Sprite.__init__(self, self.groups)
self.image.fill(BLUE)
self.collidable = True
self.interactable = True
self.sliding = False
self.orientation = None
#audio
self.boxslide_sound = pygame.mixer.Sound(path.join(MUSIC_FOLDER,"boxslide.wav"))
sprite_sheet = SpriteSheet(BLOCK_SPRITE_SHEET)
self.image = sprite_sheet.get_image(0, 0, 32, 32)
"""
Moves the sliding box if possible according to the surface it is contacting(normal/ice)
"""
def move(self, dx=0, dy=0):
#Test if this block is going to collide with anything
if not self.collision_object(dx, dy) and not self.collision_block(dx, dy):
self.boxslide_sound.set_volume(self.scene.volume_level)
pygame.mixer.Sound.play(self.boxslide_sound)
self.x += dx
self.y += dy
if self.collision_ice(): #if the box has just been pused onto ice
self.sliding = True
return True
return False
"""
Updates the state of the box
"""
def update(self):
#test if the block is sliding on the ice
if self.sliding == True: #if the block is sliding on the Ice
self.rect.x += (self.orientation[0] * 32)
self.rect.y += (self.orientation[1] * 32)
self.x = (self.rect.x - (self.rect.x % TILESIZE)) / TILESIZE
self.y = (self.rect.y - (self.rect.y % TILESIZE)) / TILESIZE
#stop sliding if they collide into a collidable object
if self.collision_object(self.orientation[0], self.orientation[1]):
self.sliding = False
#stop sliding if they collide with a block
if self.collision_block(self.orientation[0], self.orientation[1]):
self.sliding = False
#stop sliding if they are not on an ice tile
elif self.rect.x % TILESIZE == 0 and self.rect.y % TILESIZE == 0 and not self.collision_ice():
self.sliding = False
else:
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
"""
Non passable structure
"""
class Wall(GameObject):
def __init__(self, scene, x, y):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.walls
pygame.sprite.Sprite.__init__(self, self.groups)
self.interactable = False
self.collidable = True
self.checked = False #used to tell if the switch has been checked each updated
"""
A boolean object that is either on or off, resulting in assigned effects like opened doors
"""
class Switch(GameObject):
def __init__(self, scene, x, y, type):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.switches
pygame.sprite.Sprite.__init__(self, self.groups)
self.interactable = True
self.collidable = False
self.switchType = type
self.isSwitched = False
sprite_sheet = SpriteSheet(SWITCH_SPRITESHEET)
self.image = sprite_sheet.get_image(0, 0, 32, 32)
self.switch_sound = pygame.mixer.Sound(path.join(MUSIC_FOLDER,"switch.wav"))
self.switch_sound.set_volume(.5)
def switchOn(self):
self.isSwitched = True
pygame.mixer.Sound.play(self.switch_sound)
def switchOff(self):
self.isSwitched = False
def adjustVolume(self,volume):
self.switch_sound.set_volume(volume)
"""
Structure with two states, opened or closed. May be opened with keys or switches
"""
#Door object. ImageType filed is "Ice" if this is to be an ice door
class Door(GameObject):
def __init__(self, scene, x, y, doorType, imageType=None):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.doors
pygame.sprite.Sprite.__init__(self, self.groups)
self.image.fill(RED)
self.isOpen = False
self.interactable = False
self.collidable = True
self.doorType = doorType
self.unlocked = False
self.checked = False #used to tell if the door has been checked to see if it is open each update
if imageType == "Ice":
self.sprite_sheet = SpriteSheet(ICEDOOR_SPRITESHEET)
elif doorType == "Entrance" or doorType == "Exit":
self.sprite_sheet = SpriteSheet(DOOR_SPRITESHEET)
else:
self.sprite_sheet = SpriteSheet(WALLDOOR_SPRITESHEET)
self.image = self.sprite_sheet.get_image(0, 0, 32, 32)
"""
Changes the doors state to open
"""
def openDoor(self):
if self.doorType != 'Entrance':
self.isOpen = True
self.collidable = False
self.image = self.sprite_sheet.get_image(64, 0, 32, 32)
"""
Changes the doors state to closed
"""
def closeDoor(self):
if self.unlocked == False: #only unlocked doors can be closed
if self.doorType != "Entrance":
self.isOpen = False
self.collidable = True
self.image = self.sprite_sheet.get_image(32, 0, 32, 32)
"""
This is the Treasure Chest the player opens to win the game
It is stored in the doors container since it functions similar
to an exit door
"""
class Chest(GameObject):
def __init__(self, scene, x, y):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.doors
pygame.sprite.Sprite.__init__(self, self.groups)
self.isOpen = False
self.interactable = False
self.collidable = True
self.doorType = "Chest"
self.unlocked = False
self.checked = False #used to tell if the door has been checked to see if it is open each update
self.sprite_sheet = SpriteSheet(CHEST_SPRITESHEET)
self.image = self.sprite_sheet.get_image(32, 0, 32, 32)
# Opens the treasure chest
def openDoor(self):
self.isOpen = True
self.collidable = False
self.image = self.sprite_sheet.get_image(64, 0, 32, 32)
"""
This is the ice tiles. The player will slide on an ice tile
until colliding with another object
"""
class Ice(GameObject):
def __init__(self, scene, x, y):
super().__init__(scene, x, y)
self.groups = scene.all_sprites, scene.ice
pygame.sprite.Sprite.__init__(self, self.groups)
self.interactable = False
self.collidable = False
"""
Represents the amount of life the player currently has
"""
class LifeHUD(GameObject):
def __init__(self, scene, x, y):
super().__init__(scene, x, y)
self.image = pygame.Surface((TILESIZE * 3, TILESIZE))
self.groups = scene.all_sprites, scene.hud
pygame.sprite.Sprite.__init__(self, self.groups)
self.heart_state = []
sprite_sheet = SpriteSheet(LIFE_SPRITESHEET)
self.image = sprite_sheet.get_image(0, 0, 96, 32)
for x in range(0, 97, 32):
self.heart_state.append(sprite_sheet.get_image(0, x, 96, 32))
"""
update the players life hud
"""
def update(self):
heart_state_index = self.scene.player.health - 1
self.image = self.heart_state[heart_state_index]
self.rect.x = self.x * TILESIZE
self.rect.y = self.y * TILESIZE
"""
Collection of images for the game
"""
class SpriteSheet(object):
def __init__(self, file_name):
# You have to call `convert_alpha`, so that the background of
# the surface is transparent.
self.sprite_sheet = pygame.image.load(file_name).convert_alpha()
"""
Fetches an image from the spritesheet
"""
def get_image(self, x, y, width, height):
# Use a transparent surface as the base image (pass pygame.SRCALPHA).
image = pygame.Surface([width, height], pygame.SRCALPHA)
image.blit(self.sprite_sheet, (0,0), (x, y, width, height))
return image
# https://stackoverflow.com/questions/48055291/spritesheet-help-in-pygame
# Sources:
# https://github.com/kidscancode/pygame_tutorials/blob/master/tilemap/part%2002/sprites.py
# https://stackoverflow.com/questions/48055291/spritesheet-help-in-pygame