forked from matheusmaldaner/JasonJourney
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
719 lines (578 loc) · 27.3 KB
/
main.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
from pygame import mixer
import csv
from weapon import Weapon
from item import Item
from world import World
from button import Button
from cutscene import *
# *** ADDITIONAL TO DO LIST ***
# ( ) Fix enemy hit box
# ( ) Make potion have 4 total frames, so it is animated
# ( ) Tweak Enemy Health and hit box arguments
# ( ) Find other Music/Sounds effects
# (X) Fix character field of view orientation (following mouse cursor)
# ( ) Fix Merchant Spawn position (spawning a tile too low)
# defines which level to load from csv
level = 0
screen_scroll = [0, 0]
# initializes pygame and music mixer
mixer.init()
pygame.init()
# sets screen dimensions
screen = pygame.display.set_mode((constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT))
pygame.display.set_caption("Jason Journey")
# sets game frame rate
clock = pygame.time.Clock()
# movement trigger variables
moving_left = False
moving_right = False
moving_up = False
moving_down = False
# game intro animation and whether player completed the level
start_intro = False
start_game = False
pause_game = False
level_complete = False
# ----------------------------------------------------
# | Sounds Effects |
# ----------------------------------------------------
# music
pygame.mixer.music.load("assets/audio/music.wav")
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1, 0.0, 5000)
# projectile shot sound
projectile_fx = pygame.mixer.Sound("assets/audio/arrow_shot.mp3")
projectile_fx.set_volume(0.5)
# enemy is hit
hit_fx = pygame.mixer.Sound("assets/audio/arrow_hit.wav")
hit_fx.set_volume(0.5)
# coin is collected
coin_fx = pygame.mixer.Sound("assets/audio/coin.wav")
coin_fx.set_volume(0.5)
# potion is collected
potion_fx = pygame.mixer.Sound("assets/audio/heal.wav")
potion_fx.set_volume(0.5)
# define font!!
font = pygame.font.Font("assets/fonts/AtariClassic.ttf", 20)
cutscene_font = pygame.font.Font("assets/fonts/MinimalPixel.ttf", 20)
# helper function for image scaling
def scale_image(local_image, scale):
w = local_image.get_width()
h = local_image.get_height()
return pygame.transform.scale(local_image, (w * scale, h * scale))
# scales background images (game menu, game over screen, etc)
def background_scaler(local_image, mini=True):
# gets the original dimensions of the image
original_width = local_image.get_width()
original_height = local_image.get_height()
# finds appropriate scale factor
scale_factor_width = constants.SCREEN_WIDTH / original_width
scale_factor_height = constants.SCREEN_HEIGHT / original_height
# uses greater factor so image covers the entire screen
if mini:
scale_factor = min(scale_factor_width, scale_factor_height)
else:
scale_factor = 1
# scales the image
return scale_image(local_image, scale_factor)
# ----------------------------------------------------
# | Cutscene Methods |
# ----------------------------------------------------
# cutscene images
uf_scenery = background_scaler(pygame.image.load("assets/images/backgrounds/uf_scenery.png").convert_alpha())
at_desk = background_scaler(pygame.image.load("assets/images/backgrounds/at_desk.png").convert_alpha())
fallen_book = background_scaler(pygame.image.load("assets/images/backgrounds/fallen_book.png").convert_alpha())
loading_gun = background_scaler(pygame.image.load("assets/images/backgrounds/loading_gun.png").convert_alpha())
face_tower = background_scaler(pygame.image.load("assets/images/backgrounds/facing_tower.png").convert_alpha())
reaching_power = background_scaler(pygame.image.load("assets/images/backgrounds/reaching_power.png").convert_alpha())
the_end = background_scaler(pygame.image.load("assets/images/backgrounds/the_end.png").convert_alpha())
# create a cutscene manager and add scenes for beginning of story
cutscene_manager = CutsceneManager()
cutscene_manager.add_scene(DialogueScene('It had been a rough year at the University of Florida...', 1, cutscene_font, uf_scenery))
cutscene_manager.add_scene(DialogueScene('It had been a rough year at the University of Florida...', 5, cutscene_font, uf_scenery))
cutscene_manager.add_scene(DialogueScene('Jason had been trying to solve Fermat\'s Last Theorem with no luck', 5, cutscene_font, at_desk))
cutscene_manager.add_scene(DialogueScene('All of a sudden, a book fell off his shelf', 3, cutscene_font, fallen_book))
cutscene_manager.add_scene(DialogueScene('Its pages told the tale of the mysteries that lied on the last floor of Century Tower', 4, cutscene_font, fallen_book))
cutscene_manager.add_scene(DialogueScene('Loading up his Lightsaber pistol, Jason was determined to get to the bottom of this', 5, cutscene_font, loading_gun))
cutscene_manager.add_scene(DialogueScene('And so our hero\'s journey begins...', 5, cutscene_font, face_tower))
cutscene_manager.start()
# create a cutscene manager for end scenes
end_images = []
for i in range(1, 17):
temp_image = background_scaler(pygame.image.load(f"assets/images/backgrounds/{i}.jpeg").convert_alpha(), False)
end_images.append(temp_image)
cutscene_end = CutsceneManager()
cutscene_end.add_scene(DialogueScene('In his relentless pursuit, Jason finally uncovered the ultimate math equation.', 5, cutscene_font, reaching_power))
cutscene_end.add_scene(DialogueScene('In his relentless pursuit, Jason finally uncovered the ultimate math equation.', 5, cutscene_font, reaching_power))
cutscene_end.add_scene(DialogueScene('But with this knowledge, the very essence of reality began to unravel.', 5, cutscene_font, reaching_power))
cutscene_end.add_scene(TrippyScene(end_images, 6, cutscene_font, 'Visions of infinite equations danced before his eyes, warping his perception.'))
cutscene_end.add_scene(TrippyScene(end_images, 6, cutscene_font, 'In this moment of transcendence, he understood everything and nothing.'))
cutscene_end.add_scene(TrippyScene(end_images, 6, cutscene_font, 'He had ascended to godhood, but at what cost?'))
cutscene_end.add_scene(DialogueScene('His newfound power allowed him to solve mysteries of the universe.', 6, cutscene_font, the_end))
cutscene_end.add_scene(DialogueScene('Yet, in this omniscience, a profound loneliness enveloped him.', 6, cutscene_font, the_end))
cutscene_end.add_scene(DialogueScene('Now the greatest mind dwells in isolation, haunted by his boundless knowledge.', 6, cutscene_font, the_end))
cutscene_end.add_scene(DialogueScene('The End... or a new beginning?', 4, cutscene_font, the_end))
cutscene_end.start()
# ----------------------------------------------------
# | Loads Items |
# ----------------------------------------------------
# game intro image
intro = pygame.image.load("assets/images/backgrounds/game_intro.png").convert_alpha()
intro_image = background_scaler(intro)
# game over image
gameover = pygame.image.load("assets/images/backgrounds/game_over.png").convert_alpha()
gameover_image = background_scaler(gameover)
# button images
start_image = scale_image(pygame.image.load("assets/images/buttons/button_start.png").convert_alpha(),
constants.BUTTON_SCALE)
exit_image = scale_image(pygame.image.load("assets/images/buttons/button_exit.png").convert_alpha(),
constants.BUTTON_SCALE)
restart_image = scale_image(pygame.image.load("assets/images/buttons/button_restart.png").convert_alpha(),
constants.BUTTON_SCALE)
resume_image = scale_image(pygame.image.load("assets/images/buttons/button_resume.png").convert_alpha(),
constants.BUTTON_SCALE)
# hearts
empty_heart = scale_image(pygame.image.load("assets/images/items/heart_empty.png").convert_alpha(),
constants.ITEM_SCALE)
half_heart = scale_image(pygame.image.load("assets/images/items/heart_half.png").convert_alpha(),
constants.ITEM_SCALE)
full_heart = scale_image(pygame.image.load("assets/images/items/heart_full.png").convert_alpha(),
constants.ITEM_SCALE)
# coin
coin_frames = []
for i in range(4):
image = scale_image(pygame.image.load(f"assets/images/items/coin_f{i}.png").convert_alpha(), constants.ITEM_SCALE)
coin_frames.append(image)
# consumables (eg. potion)
pwr_up = []
for i in range(4):
image = scale_image(pygame.image.load("assets/images/items/potion_red.png").convert_alpha(), constants.ITEM_SCALE)
pwr_up.append(image)
# creates list with the created items
all_items = [coin_frames, pwr_up]
# weapons
pistol_img = pygame.image.load("assets/images/weapons/pistol.png").convert_alpha()
pistol_image = scale_image(pistol_img, constants.WEAPON_SCALE)
fireball_image = scale_image(pygame.image.load("assets/images/weapons/fireball.png").convert_alpha(),
constants.FIREBALL_SCALE)
# player projectiles
projectile_img = pygame.image.load("assets/images/weapons/blue_lightsaber.png").convert_alpha()
projectile_image = scale_image(projectile_img, constants.PROJECTILE_SCALE)
# mob entities
mob_animations = []
mob_types = ["jason", "imp", "skeleton", "goblin", "muddy", "tiny_zombie", "big_demon", "merchant"]
animation_types = ["idle", "run"]
# creates our level tile set
tile_list = []
for x in range(constants.DIFF_TILES + 1): # addresses out of bounds error
tile_image = pygame.image.load(f"assets/images/tiles/{x}.png").convert_alpha()
tile_image = pygame.transform.scale(tile_image, (constants.TILE_SIZE, constants.TILE_SIZE))
tile_list.append(tile_image)
for mob in mob_types:
# clears animation list
temp_mob_list = []
for animation in animation_types:
# clears temporary list
temp_action_list = []
for i in range(4):
img = pygame.image.load(f"assets/images/characters/{mob}/{animation}/{i}.png").convert_alpha()
img = scale_image(img, constants.PLAYER_SCALE)
temp_action_list.append(img)
temp_mob_list.append(temp_action_list)
mob_animations.append(temp_mob_list)
# draws text onto screen
def draw_text(text, local_font, color, local_x, local_y):
local_image = local_font.render(text, True, color)
screen.blit(local_image, (local_x, local_y))
def reset_level():
damage_text_group.empty()
projectile_group.empty()
item_group.empty()
fireball_group.empty()
# create empty tile list
data = []
for row in range(constants.ROWS):
r = [-1] * constants.COLS
data.append(r)
return data
# displays game info
def game_info():
# draw HUD
pygame.draw.rect(screen, (constants.HUD_COLOR), (0, 0, constants.SCREEN_WIDTH, 50))
# draw hearts
has_half_heart = False
for i in range(5):
if player.health >= ((i + 1) * 20):
screen.blit(full_heart, (10 + i * 50, 0))
elif (player.health % 20 > 0) and has_half_heart == False:
screen.blit(half_heart, (10 + i * 50, 0))
has_half_heart = True
else:
screen.blit(empty_heart, (10 + i * 50, 0))
# draws the level number
if level != 4:
draw_text("LEVEL: " + str(level), font, constants.WHITE, constants.SCREEN_WIDTH / 2, 15)
else:
draw_text("SHOP", font, constants.WHITE, constants.SCREEN_WIDTH / 2, 15)
# draws player wallet
draw_text(f"x{player.money}", font, constants.WHITE, constants.SCREEN_WIDTH - 75, 15)
# class used for screen fading
class ScreenFade:
def __init__(self, direction, color, speed, fade_image=None):
self.direction = direction
self.color = color
self.speed = speed
self.fade_counter = 0
self.image = fade_image
def fade(self):
fade_complete = False
self.fade_counter += self.speed
if self.image:
# if an image is provided, draw it at the center of the screen
local_image_x = (constants.SCREEN_WIDTH - self.image.get_width()) // 2
local_image_y = (constants.SCREEN_HEIGHT - self.image.get_height()) // 2
screen.blit(self.image, (local_image_x, local_image_y))
if self.direction == 1: # fades up to open the game
pygame.draw.rect(screen, self.color, (0 - self.fade_counter, 0,
constants.SCREEN_WIDTH // 2, constants.SCREEN_HEIGHT))
pygame.draw.rect(screen, self.color, (constants.SCREEN_WIDTH // 2 + self.fade_counter, 0,
constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT))
pygame.draw.rect(screen, self.color, (0, 0 - self.fade_counter,
constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT // 2))
pygame.draw.rect(screen, self.color, (0, constants.SCREEN_HEIGHT // 2 + self.fade_counter,
constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT))
elif self.direction == 2: # fades down like it's closing the game
pygame.draw.rect(screen, self.color, (0, 0, constants.SCREEN_WIDTH, 0 + self.fade_counter))
if self.fade_counter >= constants.SCREEN_WIDTH:
fade_complete = True
return fade_complete
# ----------------------------------------------------
# | World Gen |
# ----------------------------------------------------
level_width, level_height = 15, 15
def add_perimeter_walls(world_data, wall_tile_index):
rows = level_width
columns = level_height
# Top and bottom rows
for col in range(columns):
world_data[0][col] = wall_tile_index
world_data[rows - 1][col] = wall_tile_index
# Left and right columns
for row in range(1, rows - 1):
world_data[row][0] = wall_tile_index
world_data[row][columns - 1] = wall_tile_index
# unused method
def place_exit(world_data, exit_tile_index):
rows = level_width
columns = level_height
while True:
x = random.randint(1, rows - 2)
y = random.randint(1, columns - 2)
# ensures the chosen spot for the exit is not a wall
if world_data[x][y] != 7:
world_data[x][y] = exit_tile_index
break
# tile set creation for level
world_data = []
# dummy data to populate the list so it is not empty
for row in range(constants.ROWS):
r = [-1] * constants.COLS
world_data.append(r)
# overwrites dummy data with actual level
with open(f"levels/level{level}_data.csv", newline="") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for x, row in enumerate(reader):
for y, tile in enumerate(row):
world_data[x][y] = int(tile)
# wave = WaveFunctionCollapse()
# random_level = wave.collapse(level_width, level_height)
#
# def is_wall(tile_name):
# return "wall" in tile_name
#
# for x, row in enumerate(random_level):
# for y, potential in enumerate(row):
# tile_type = potential.state.name
# if is_wall(tile_type):
# world_data[x][y] = 7
# elif tile_type == "floor":
# world_data[x][y] = 0
# else:
# print(tile_type)
# world_data[x][y] = -1
# add_perimeter_walls(world_data, 7)
# place_exit(world_data, 8)
# world_data[5][5] = 11
world = World()
world.process_data(world_data, tile_list, all_items, mob_animations)
# damage class
class DamageText(pygame.sprite.Sprite):
def __init__(self, x, y, damage, color):
pygame.sprite.Sprite.__init__(self)
self.image = font.render(damage, True, color)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.counter = 0
def update(self):
# makes so that the damage display scrolls appropriately
self.rect.x += screen_scroll[0]
self.rect.y += screen_scroll[1]
# moves damage text up
self.rect.y -= 1
# deletes the counter after time
self.counter += 1
if self.counter > 30:
self.kill()
# creates our hero and his weapon!
player = world.player
pistol = Weapon(pistol_image, projectile_image)
# gets the enemy list from the world class
enemy_list = world.all_enemies
# gets the npc list from the world class
npc_list = world.all_npcs
# creates sprite group for projectiles
damage_text_group = pygame.sprite.Group()
projectile_group = pygame.sprite.Group()
item_group = pygame.sprite.Group()
fireball_group = pygame.sprite.Group()
score_coin = Item(constants.SCREEN_WIDTH - 90, 23, 0, coin_frames, True)
item_group.add(score_coin)
# loads all items from the world class
for item in world.all_items:
item_group.add(item)
intro_fade = ScreenFade(1, constants.BLACK, 8)
death_fade = ScreenFade(2, constants.FADE_BLACK, 12)
# ----------------------------------------------------
# | Game Loop |
# ----------------------------------------------------
# creates buttons
start_button = Button(825, 475, start_image)
exit_button = Button(25, 475, exit_image)
restart_button = Button(constants.SCREEN_WIDTH // 2 - restart_image.get_width() // 2, 450, restart_image)
resume_button = Button(constants.SCREEN_WIDTH // 2 - 175, constants.SCREEN_HEIGHT // 2 - 150, resume_image)
# keeps window open till user closes it
run = True
is_cutscene_playing = False
game_won = False
while run:
# sets frame rate and screen background color
clock.tick(constants.FPS)
if not start_game:
screen.blit(intro_image, (0, 0))
if start_button.draw(screen):
start_game = True
start_intro = True
is_cutscene_playing = True
if exit_button.draw(screen):
run = False
elif is_cutscene_playing:
# updates and draws only the cutscene
# screen.fill(constants.BLACK)
cutscene_manager.update()
cutscene_manager.draw(screen)
pygame.display.flip()
# checks that the cutscene is finished
if cutscene_manager.is_finished():
is_cutscene_playing = False
elif game_won:
cutscene_end.update()
cutscene_end.draw(screen)
pygame.display.flip()
else:
if pause_game:
screen.fill(constants.MENU_BACKGROUND)
if resume_button.draw(screen):
pause_game = False
if exit_button.draw(screen):
run = False
else:
screen.fill(constants.BACKGROUND)
# player can't run around after he's dead
if player.alive:
# change in x and y
dx = 0
dy = 0
if moving_right:
dx = constants.PLAYER_SPEED
if moving_left:
dx = -constants.PLAYER_SPEED
if moving_up:
dy = -constants.PLAYER_SPEED
if moving_down:
dy = constants.PLAYER_SPEED
# moves player and stores screen scroll returned value
screen_scroll = player.move(dx, dy, world.wall_tiles)
# ----------------------------------------------------
# | Update Methods |
# ----------------------------------------------------
# level tile set
world.update(screen_scroll)
# enemy animated state
for enemy in enemy_list:
fireball = enemy.ai(player, world.wall_tiles, screen_scroll, fireball_image)
if fireball:
fireball_group.add(fireball)
if enemy.alive:
enemy.update()
# non-playable characters (npcs) such as the Merchant
for npc in npc_list:
npc.ai(player, world.wall_tiles, screen_scroll, fireball_image)
npc.update()
# player animated state
player.update()
# projectile state
projectile = pistol.update(player)
if projectile:
projectile_group.add(projectile)
projectile_fx.play()
# working collision of projectiles with enemies
for projectile in projectile_group:
characters_list = enemy_list + npc_list
damage, damage_pos = projectile.update(screen_scroll, world.wall_tiles, characters_list)
if damage:
damage_text = DamageText(damage_pos.centerx, damage_pos.y, str(damage), constants.RED)
damage_text_group.add(damage_text)
hit_fx.play()
damage_text_group.update()
fireball_group.update(screen_scroll, player)
# scrolls screen while keeping items where they belong
item_group.update(screen_scroll, player, coin_fx, potion_fx)
# ---------------------------------------------------
# | Draw Methods |
# ---------------------------------------------------
# level tile set
world.draw(screen)
# enemy
for enemy in enemy_list:
enemy.draw(screen)
for npc in npc_list:
npc.draw(screen)
# main character, weapon, and items
player.draw(screen)
pistol.draw(screen)
item_group.draw(screen)
# projectiles
for projectile in projectile_group:
projectile.draw(screen)
for fireball in fireball_group:
fireball.draw(screen)
damage_text_group.draw(screen)
# top part of screen with hp, text, coin sprite
game_info()
score_coin.draw(screen)
# ---------------------------------------------------
# | Event Handler |
# ---------------------------------------------------
# checks to see if level is complete
if not level_complete:
varrect = player.get_rect()
if varrect.colliderect(world.ladder_tile[1]):
level_complete = True
if level_complete:
start_intro = True
level += 1
# ends game and plays ending cutscene
if level == 6:
game_won = True
# -----------------------------------------------
# | Recreates the World |
# -----------------------------------------------
else:
# tile set creation for level
world_data = []
# dummy data to populate the list so it is not empty
for row in range(constants.ROWS):
r = [-1] * constants.COLS
world_data.append(r)
# overwrites dummy data with actual level
with open(f"levels/level{level}_data.csv", newline="") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for x, row in enumerate(reader):
for y, tile in enumerate(row):
world_data[x][y] = int(tile)
world = World()
world.process_data(world_data, tile_list, all_items, mob_animations)
# resets player position to the position from the world class
newpos = world.player.get_pos()
player.set_pos(newpos)
# gets the enemy list from the world class
enemy_list = world.all_enemies
# gets the npc list from the world class
npc_list = world.all_npcs
# creates sprite group for projectiles
damage_text_group = pygame.sprite.Group()
projectile_group = pygame.sprite.Group()
item_group = pygame.sprite.Group()
fireball_group = pygame.sprite.Group()
score_coin = Item(constants.SCREEN_WIDTH - 90, 23, 0, coin_frames, True)
item_group.add(score_coin)
# loads all items from the world class
for item in world.all_items:
item_group.add(item)
level_complete = False
# ** can add a probability of being taken to the shop
# displays game intro
if start_intro:
if intro_fade.fade():
start_intro = False
intro_fade.fade_counter = 0
# show death!
if not player.alive:
if death_fade.fade():
# displays game over screen
image_x = (constants.SCREEN_WIDTH - gameover_image.get_width()) // 2
image_y = (constants.SCREEN_HEIGHT - gameover_image.get_height()) // 2
screen.blit(gameover_image, (image_x, image_y))
if restart_button.draw(screen):
death_fade.fade_counter = 0
start_intro = True
level = 1
world_data = reset_level()
with open(f"levels/level{level}_data.csv", newline="") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for x, row in enumerate(reader):
for y, tile in enumerate(row):
world_data[x][y] = int(tile)
world = World()
world.process_data(world_data, tile_list, all_items, mob_animations)
temp_score = player.money
player = world.player
player.money = temp_score
enemy_list = world.all_enemies
score_coin = Item(constants.SCREEN_WIDTH - 115, 23, 0, coin_frames, True)
item_group.add(score_coin)
# add the items from the level data
for item in world.all_items:
item_group.add(item)
for event in pygame.event.get():
# finishes loop when you close the game window
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
# moves right
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
moving_right = True
# moves left
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
moving_left = True
# moves up
if event.key == pygame.K_w or event.key == pygame.K_UP:
moving_up = True
# moves down
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
moving_down = True
if event.key == pygame.K_ESCAPE:
pause_game = True
# stops player movement if key is let go
if event.type == pygame.KEYUP:
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
moving_right = False
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
moving_left = False
if event.key == pygame.K_w or event.key == pygame.K_UP:
moving_up = False
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
moving_down = False
pygame.display.update()
# quits pygame
pygame.quit()