-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
789 lines (605 loc) · 25 KB
/
game.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
import pygame
import button
from os import listdir
from os.path import isfile, join
pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Riomo")
WIDTH, HEIGHT = 1000, 800
FPS = 60
PLAYER_VEL = 5
window = pygame.display.set_mode((WIDTH, HEIGHT))
HEART = pygame.image.load('heart.png')
user_character = "MaskDude"
block_choice = 2
music_choice = "1"
background_choice = "Blue.png"
main_music_path = join("assets", "Audio", "cottagecore-17463.mp3")
main_music = pygame.mixer.Sound(main_music_path)
pygame.mixer.Sound.play(main_music)
def play_jump_music():
jump_music_path = join("assets", "Audio", "cartoon-jump-6462.mp3")
pygame.mixer.music.load(jump_music_path)
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play()
def play_damage_music():
damage_music_path = join("assets", "Audio", "2G7CF5V-gamers-fail-game.mp3")
pygame.mixer.music.load(damage_music_path)
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play()
def play_shift_music():
damage_music_path = join("assets", "Audio", "teleport-14639.mp3")
pygame.mixer.music.load(damage_music_path)
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play()
def flip(sprites):
return [pygame.transform.flip(sprite, True, False) for sprite in sprites]
def load_sprite_sheets(dir1, dir2, width, height, direction=False):
path = join("assets", dir1, dir2)
images = [f for f in listdir(path) if isfile(join(path, f))]
all_sprites = {}
for image in images:
sprite_sheet = pygame.image.load(join(path, image)).convert_alpha()
sprites = []
for i in range(sprite_sheet.get_width() // width):
surface = pygame.Surface((width, height), pygame.SRCALPHA, 32)
rect = pygame.Rect(i * width, 0, width, height)
surface.blit(sprite_sheet, (0, 0), rect)
sprites.append(pygame.transform.scale2x(surface))
if direction:
all_sprites[image.replace(".png", "") + "_right"] = sprites
all_sprites[image.replace(".png", "") + "_left"] = flip(sprites)
else:
all_sprites[image.replace(".png", "")] = sprites
return all_sprites
class OptionBox():
def __init__(self, x, y, w, h, color, highlight_color, font, option_list, selected = 0):
self.color = color
self.highlight_color = highlight_color
self.rect = pygame.Rect(x, y, w, h)
self.font = font
self.option_list = option_list
self.selected = selected
self.draw_menu = False
self.menu_active = False
self.active_option = -1
def draw(self, surf):
pygame.draw.rect(surf, self.highlight_color if self.menu_active else self.color, self.rect)
pygame.draw.rect(surf, (0, 0, 0), self.rect, 2)
msg = self.font.render(self.option_list[self.selected], 1, (0, 0, 0))
surf.blit(msg, msg.get_rect(center = self.rect.center))
if self.draw_menu:
for i, text in enumerate(self.option_list):
rect = self.rect.copy()
rect.y += (i+1) * self.rect.height
pygame.draw.rect(surf, self.highlight_color if i == self.active_option else self.color, rect)
msg = self.font.render(text, 1, (0, 0, 0))
surf.blit(msg, msg.get_rect(center = rect.center))
outer_rect = (self.rect.x, self.rect.y + self.rect.height, self.rect.width, self.rect.height * len(self.option_list))
pygame.draw.rect(surf, (0, 0, 0), outer_rect, 2)
def update(self, event_list):
mpos = pygame.mouse.get_pos()
self.menu_active = self.rect.collidepoint(mpos)
self.active_option = -1
for i in range(len(self.option_list)):
rect = self.rect.copy()
rect.y += (i+1) * self.rect.height
if rect.collidepoint(mpos):
self.active_option = i
break
if not self.menu_active and self.active_option == -1:
self.draw_menu = False
for event in event_list:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
if self.menu_active:
self.draw_menu = not self.draw_menu
elif self.draw_menu and self.active_option >= 0:
self.selected = self.active_option
self.draw_menu = False
return self.active_option
return -1
character_options = OptionBox(
300, 265, 160, 40, (150, 150, 150), (100, 200, 255), pygame.font.SysFont(None, 30),
["MaskDude", "NinjaFrog", "PinkMan", "VirtualGuy"])
block_options = OptionBox(
300, 460, 160, 40, (150, 150, 150), (100, 200, 255), pygame.font.SysFont(None, 30),
["1", "2", "3", "4", "5", "6"])
music_options = OptionBox(
700, 265, 160, 40, (150, 150, 150), (100, 200, 255), pygame.font.SysFont(None, 30),
["on", "off"])
background_options = OptionBox(
700, 460, 160, 40, (150, 150, 150), (100, 200, 255), pygame.font.SysFont(None, 30),
["Blue", "Brown", "Gray", "Green", "Pink", "Purple", "Yellow"])
def setting(window):
global user_character
global block_choice
global music_choice
global background_choice
clock = pygame.time.Clock()
background, bg_image = get_background(background_choice)
back_img_path = join("assets", "Menu", "Buttons", "Back.png")
back_img = pygame.image.load(back_img_path).convert_alpha()
back_button = button.Button(925, 20, back_img, 3.5)
run_setting = True
while run_setting:
clock.tick(FPS)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run_setting = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run_setting = False
menu(window)
selected_character_option = character_options.update(event_list)
if selected_character_option >= 0:
if selected_character_option == 0:
user_character = "MaskDude"
elif selected_character_option == 1:
user_character = "NinjaFrog"
elif selected_character_option == 2:
user_character = "PinkMan"
elif selected_character_option == 3:
user_character = "VirtualGuy"
selected_block_option = block_options.update(event_list)
if selected_block_option >= 0:
if selected_block_option == 0:
block_choice = 1
elif selected_block_option == 1:
block_choice = 2
elif selected_block_option == 2:
block_choice = 3
elif selected_block_option == 3:
block_choice = 4
elif selected_block_option == 4:
block_choice = 5
elif selected_block_option == 5:
block_choice = 6
selected_background_option = background_options.update(event_list)
if selected_background_option >= 0:
if selected_background_option == 0:
background_choice = "Blue.png"
elif selected_background_option == 1:
background_choice = "Brown.png"
elif selected_background_option == 2:
background_choice = "Gray.png"
elif selected_background_option == 3:
background_choice = "Green.png"
elif selected_background_option == 4:
background_choice = "Pink.png"
elif selected_background_option == 5:
background_choice = "Purple.png"
elif selected_background_option == 6:
background_choice = "Yellow.png"
draw_setting(window, background, bg_image, back_button)
selected_music_option = music_options.update(event_list)
if selected_music_option >= 0:
if selected_music_option == 0:
music_choice = "1"
pygame.mixer.Sound.play(main_music)
elif selected_music_option == 1:
music_choice = "2"
pygame.mixer.Sound.stop(main_music)
pygame.display.update()
pygame.quit()
quit()
def get_block(size):
if block_choice == 1:
block_x, block_y = 0, 0
elif block_choice == 2:
block_x, block_y = 96, 0
elif block_choice == 3:
block_x, block_y = 0, 64
elif block_choice == 4:
block_x, block_y = 96, 64
elif block_choice == 5:
block_x, block_y = 0, 128
elif block_choice == 6:
block_x, block_y = 96, 128
path = join("assets", "Terrain", "Terrain.png")
image = pygame.image.load(path).convert_alpha()
surface = pygame.Surface((size, size), pygame.SRCALPHA, 32)
rect = pygame.Rect(block_x, block_y, size, size)
surface.blit(image, (0, 0), rect)
return pygame.transform.scale2x(surface)
class Player(pygame.sprite.Sprite):
COLOR = (255, 0, 0)
GRAVITY = 1
SPRITES = load_sprite_sheets("MainCharacters", user_character, 32, 32, True)
ANIMATION_DELAY = 3
def __init__(self, x, y, width, height):
super().__init__()
self.rect = pygame.Rect(x, y, width, height)
self.x_vel = 0
self.y_vel = 0
self.mask = None
self.direction = "left"
self.animation_count = 0
self.fall_count = 0
self.jump_count = 0
self.hit = False
self.hit_count = 0
self.hearts = 3
self.shift = 2
def jump(self):
if music_choice == "1":
play_jump_music()
self.y_vel = -self.GRAVITY * 8
self.animation_count = 0
self.jump_count += 1
if self.jump_count == 1:
self.fall_count = 0
def move(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
def make_hit(self):
self.hit = True
def move_left(self, vel):
self.x_vel = -vel
if self.direction != "left":
self.direction = "left"
self.animation_count = 0
def move_right(self, vel):
self.x_vel = vel
if self.direction != "right":
self.direction = "right"
self.animation_count = 0
def move_right_shift(self, vel):
self.x_vel = vel + 184
play_shift_music()
if self.direction != "right":
self.direction = "right"
self.animation_count = 0
def loop(self, fps):
self.y_vel += min(1, (self.fall_count / fps) * self.GRAVITY)
self.move(self.x_vel, self.y_vel)
if self.hit:
self.hit_count += 1
if self.hit_count > fps * 2:
self.hit = False
self.hit_count = 0
self.hearts -= 1
if music_choice == "1":
play_damage_music()
self.fall_count += 1
self.update_sprite()
def landed(self):
self.fall_count = 0
self.y_vel = 0
self.jump_count = 0
def hit_head(self):
self.count = 0
self.y_vel *= -1
def update_sprite(self):
sprite_sheet = "idle"
if self.hit:
sprite_sheet = "hit"
elif self.y_vel < 0:
if self.jump_count == 1:
sprite_sheet = "jump"
elif self.jump_count == 2:
sprite_sheet = "double_jump"
elif self.y_vel > self.GRAVITY * 2:
sprite_sheet = "fall"
elif self.x_vel != 0:
sprite_sheet = "run"
Player.SPRITES = load_sprite_sheets("MainCharacters", user_character, 32, 32, True)
sprite_sheet_name = sprite_sheet + "_" + self.direction
sprites = self.SPRITES[sprite_sheet_name]
sprite_index = (self.animation_count //
self.ANIMATION_DELAY) % len(sprites)
self.sprite = sprites[sprite_index]
self.animation_count += 1
self.update()
def update(self):
self.rect = self.sprite.get_rect(topleft=(self.rect.x, self.rect.y))
self.mask = pygame.mask.from_surface(self.sprite)
def draw(self, win, offset_x, HEART_1, HEART_2, HEART_3):
win.blit(self.sprite, (self.rect.x - offset_x, self.rect.y))
if self.hearts == 3:
win.blit(HEART_1, (WIDTH // 2 + 100, 50))
if self.hearts >= 2:
win.blit(HEART_2, (WIDTH // 2 , 50))
if self.hearts >= 1:
win.blit(HEART_3, (WIDTH // 2 - 100, 50))
if self.hearts <= 0:
run = False
menu(window)
self.hearts = 3
class Object(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, name=None):
super().__init__()
self.rect = pygame.Rect(x, y, width, height)
self.image = pygame.Surface((width, height), pygame.SRCALPHA)
self.width = width
self.height = height
self.name = name
def draw(self, win, offset_x):
win.blit(self.image, (self.rect.x - offset_x, self.rect.y))
class Block(Object):
def __init__(self, x, y, size):
super().__init__(x, y, size, size)
block = get_block(size)
self.image.blit(block, (0, 0))
self.mask = pygame.mask.from_surface(self.image)
class Fire(Object):
ANIMATION_DELAY = 3
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height, "fire")
self.fire = load_sprite_sheets("Traps", "Fire", width, height)
self.image = self.fire["off"][0]
self.mask = pygame.mask.from_surface(self.image)
self.animation_count = 0
self.animation_name = "off"
def on(self):
self.animation_name = "on"
def off(self):
self.animation_name = "off"
def loop(self):
sprites = self.fire[self.animation_name]
sprite_index = (self.animation_count //
self.ANIMATION_DELAY) % len(sprites)
self.image = sprites[sprite_index]
self.animation_count += 1
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
self.mask = pygame.mask.from_surface(self.image)
if self.animation_count // self.ANIMATION_DELAY > len(sprites):
self.animation_count = 0
class Apple(Object):
ANIMATION_DELAY = 10
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height, "apple")
self.apple = load_sprite_sheets("Items", "Fruits", 22, 21)
self.image = self.apple["Apple"][0]
self.mask = pygame.mask.from_surface(self.image)
self.animation_count = 0
self.animation_name = "Apple"
def loop(self):
sprites = self.apple[self.animation_name]
sprite_index = (self.animation_count //self.ANIMATION_DELAY) % len(sprites)
self.image = sprites[0]
self.animation_count += 1
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
self.mask = pygame.mask.from_surface(self.image)
if self.animation_count // self.ANIMATION_DELAY > len(sprites):
self.animation_count = 0
class Checkpoint(Object):
ANIMATION_DELAY = 3
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height, "flag")
self.flag = load_sprite_sheets("Items", "Checkpoints/checkpoint", width, height)
self.image = self.flag["on"][0]
self.mask = pygame.mask.from_surface(self.image)
self.animation_count = 0
self.animation_name = "on"
def loop(self):
sprites = self.flag[self.animation_name]
sprite_index = 0
self.image = sprites[sprite_index]
self.animation_count += 1
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
self.mask = pygame.mask.from_surface(self.image)
if self.animation_count // self.ANIMATION_DELAY > len(sprites):
self.animation_count = 0
def get_background(name):
image = pygame.image.load(join("assets", "Background", name))
_, _, width, height = image.get_rect()
tiles = []
for i in range(WIDTH // width + 1):
for j in range(HEIGHT // height + 1):
pos = (i * width, j * height)
tiles.append(pos)
return tiles, image
def draw_setting(window, background, bg_image, back_button):
for tile in background:
window.blit(bg_image, tile)
font = pygame.font.Font('Caprasimo-Regular.ttf', 40)
font_2 = pygame.font.Font('Caprasimo-Regular.ttf', 70)
text = font_2.render('Settings', True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (WIDTH // 2, 100)
window.blit(text, textRect)
text = font.render('Character', True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (120, 280)
window.blit(text, textRect)
text_2 = font.render('Block', True, (0, 0, 0))
textRect_2 = text_2.get_rect()
textRect_2.center = (100, 475)
window.blit(text_2, textRect_2)
text_2 = font.render('Music', True, (0, 0, 0))
textRect_2 = text_2.get_rect()
textRect_2.center = (600, 280)
window.blit(text_2, textRect_2)
text_3 = font.render('Sky', True, (0, 0, 0))
textRect_3 = text_3.get_rect()
textRect_3.center = (600, 475)
window.blit(text_3, textRect_3)
if back_button.draw(window):
run_setting = False
menu(window)
character_options.draw(window)
block_options.draw(window)
music_options.draw(window)
background_options.draw(window)
pygame.display.update()
def draw_menu(window, background, bg_image, start_button, setting_button, exit_button):
red = (255, 0, 0)
for tile in background:
window.blit(bg_image, tile)
if start_button.draw(window):
run_menu = False
main(window)
if setting_button.draw(window):
run_menu = False
setting(window)
if exit_button.draw(window):
pygame.quit()
quit()
font = pygame.font.Font('Caprasimo-Regular.ttf', 70)
text = font.render('RIOMO', True, red)
textRect = text.get_rect()
textRect.center = (WIDTH // 2, HEIGHT // 2.8)
window.blit(text, textRect)
pygame.display.update()
def draw(window, background, bg_image, player, objects, offset_x):
HEART_1 = pygame.transform.scale(HEART, (50, 50))
HEART_2 = pygame.transform.scale(HEART, (50, 50))
HEART_3 = pygame.transform.scale(HEART, (50, 50))
for tile in background:
window.blit(bg_image, tile)
font = pygame.font.Font('Caprasimo-Regular.ttf', 30)
text = font.render(f"Shift: {player.shift}", True, (225, 0, 0))
textRect = text.get_rect()
textRect.center = (900, 70)
window.blit(text, textRect)
for obj in objects:
obj.draw(window, offset_x)
player.draw(window, offset_x, HEART_1, HEART_2, HEART_3)
pygame.display.update()
def handle_vertical_collision(player, objects, dy):
collided_objects = []
for obj in objects:
if pygame.sprite.collide_mask(player, obj):
if dy > 0:
player.rect.bottom = obj.rect.top
player.landed()
elif dy < 0:
player.rect.top = obj.rect.bottom
player.hit_head()
collided_objects.append(obj)
return collided_objects
def collide(player, objects, dx):
player.move(dx, 0)
player.update()
collided_object = None
for obj in objects:
if pygame.sprite.collide_mask(player, obj):
collided_object = obj
break
player.move(-dx, 0)
player.update()
return collided_object
def handle_move(player, objects):
keys = pygame.key.get_pressed()
player.x_vel = 0
collide_left = collide(player, objects, -PLAYER_VEL * 2)
collide_right = collide(player, objects, PLAYER_VEL * 2)
if keys[pygame.K_LEFT] and not collide_left:
player.move_left(PLAYER_VEL)
if keys[pygame.K_RIGHT] and not collide_right:
player.move_right(PLAYER_VEL)
if (keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) and not collide_right and (player.shift >= 1):
player.shift -= 1
player.move_right_shift(PLAYER_VEL)
vertical_collide = handle_vertical_collision(player, objects, player.y_vel)
to_check = [collide_left, collide_right, *vertical_collide]
for obj in to_check:
if obj and obj.name == "fire":
player.make_hit()
if obj and obj.name == "flag":
run = False
menu(window)
def menu(window):
clock = pygame.time.Clock()
background, bg_image = get_background(background_choice)
setting_img_path = join("assets", "Menu", "Buttons", "Settings.png")
start_img = pygame.image.load('start_btn.png').convert_alpha()
exit_img = pygame.image.load('exit_btn.png').convert_alpha()
setting_img = pygame.image.load(setting_img_path).convert_alpha()
start_button = button.Button(300, 400, start_img, 0.5)
exit_button = button.Button(600, 400, exit_img, 0.5)
setting_button = button.Button(925, 20, setting_img, 2.5)
run_menu = True
while run_menu:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_menu = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_KP_ENTER or event.key == pygame.K_RETURN:
run_menu = False
main(window)
draw_menu(window, background, bg_image, start_button, setting_button, exit_button)
pygame.display.update()
pygame.quit()
quit()
def main(window):
clock = pygame.time.Clock()
background, bg_image = get_background(background_choice)
block_size = 96
player = Player(block_size, HEIGHT - block_size, 50, 50)
apple = [Apple(block_size * 2, HEIGHT - block_size * 3 - 41, 22, 22),
Apple(block_size * 1, HEIGHT - block_size * 3 - 41, 22, 22)]
fire = [Fire(block_size * 3, HEIGHT - block_size - 64, 16, 32),
Fire(block_size * 6 + 70, HEIGHT - block_size * 4 - 64, 16, 32),
Fire(block_size * 13, HEIGHT - block_size - 64, 16, 32),
Fire(block_size * 16, HEIGHT - block_size - 64, 16, 32)]
checkpoint = [Checkpoint(block_size * 19, HEIGHT - block_size * 2 - 32, 50, 64)]
blocks = [Block(0, HEIGHT - block_size * 2, block_size),
Block(0, HEIGHT - block_size * 3, block_size),
Block(0, HEIGHT - block_size * 4, block_size),
Block(0, HEIGHT - block_size * 5, block_size),
Block(0, HEIGHT - block_size * 6, block_size),
Block(block_size * 4, HEIGHT - block_size * 3, block_size),
Block(block_size * 4, HEIGHT - block_size * 2, block_size),
Block(block_size * 6, HEIGHT - block_size * 4, block_size),
Block(block_size * 7, HEIGHT - block_size * 4, block_size),
Block(block_size * 7, HEIGHT - block_size * 4, block_size),
Block(block_size * 10, HEIGHT - block_size * 2, block_size),
Block(block_size * 10, HEIGHT - block_size * 3, block_size),
Block(block_size * 10, HEIGHT - block_size * 4, block_size),
Block(block_size * 10, HEIGHT - block_size * 5, block_size),
Block(block_size * 13, HEIGHT - block_size * 4, block_size),
Block(block_size * 13, HEIGHT - block_size * 5, block_size),
Block(block_size * 13, HEIGHT - block_size * 6, block_size),
Block(block_size * 13, HEIGHT - block_size * 7, block_size),
Block(block_size * 13, HEIGHT - block_size * 8, block_size)]
for i in range(len(fire)):
fire[i].on()
floor = [Block(i * block_size, HEIGHT - block_size, block_size)
for i in range(0, (WIDTH * 3) // block_size)]
offset_x = 0
scroll_area_width = 200
run = True
while run:
clock.tick(FPS)
objects = [*floor,
*blocks,
*fire,
*checkpoint,
*apple]
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and player.jump_count < 2:
player.jump()
if event.key == pygame.K_ESCAPE :
run = False
menu(window)
player.loop(FPS)
for i in range(len(fire)):
fire[i].loop()
for i in range(len(checkpoint)):
checkpoint[i].loop()
apples_to_remove = []
for i in range(len(apple)):
apple[i].loop()
if pygame.sprite.collide_mask(player, apple[i]):
apples_to_remove.append(apple[i])
for apple_obj in apples_to_remove:
apple.remove(apple_obj)
player.shift += 1
handle_move(player, objects)
draw(window, background, bg_image, player, objects, offset_x)
if ((player.rect.right - offset_x >= WIDTH - scroll_area_width) and player.x_vel > 0) or (
(player.rect.left - offset_x <= scroll_area_width) and player.x_vel < 0):
offset_x += player.x_vel
pygame.quit()
quit()
if __name__ == "__main__":
menu(window)