-
Notifications
You must be signed in to change notification settings - Fork 1
/
level1.py
1357 lines (1176 loc) · 55.9 KB
/
level1.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division
import pygame as pg
from .. import setup, tools
from .. import constants as c
from .. import game_sound
from ..components import mario
from ..components import collider
from ..components import bricks
from ..components import coin_box
from ..components import enemies
from ..components import checkpoint
from ..components import flagpole
from ..components import info
from ..components import score
from ..components import castle_flag
class Level1(tools._State):
def __init__(self):
tools._State.__init__(self)
def startup(self, current_time, persist):
"""Called when the State object is created"""
self.game_info = persist
self.persist = self.game_info
self.game_info[c.CURRENT_TIME] = current_time
self.game_info[c.LEVEL_STATE] = c.NOT_FROZEN
self.game_info[c.MARIO_DEAD] = False
self.state = c.NOT_FROZEN
self.death_timer = 0
self.flag_timer = 0
self.flag_score = None
self.flag_score_total = 0
self.moving_score_list = []
self.overhead_info_display = info.OverheadInfo(self.game_info, c.LEVEL)
self.sound_manager = game_sound.Sound(self.overhead_info_display)
self.setup_background()
self.setup_ground()
self.setup_pipes()
self.setup_steps()
self.setup_bricks()
self.setup_coin_boxes()
self.setup_flag_pole()
self.setup_enemies()
self.setup_mario()
self.setup_checkpoints()
self.setup_spritegroups()
def setup_background(self):
"""Sets the background image, rect and scales it to the correct
proportions"""
self.background = setup.GFX['level_1']
self.back_rect = self.background.get_rect()
self.background = pg.transform.scale(self.background,
(int(self.back_rect.width * c.BACKGROUND_MULTIPLER),
int(self.back_rect.height * c.BACKGROUND_MULTIPLER)))
self.back_rect = self.background.get_rect()
width = self.back_rect.width
height = self.back_rect.height
self.level = pg.Surface((width, height)).convert()
self.level_rect = self.level.get_rect()
self.viewport = setup.SCREEN.get_rect(bottom=self.level_rect.bottom)
self.viewport.x = self.game_info[c.CAMERA_START_X]
def setup_ground(self):
"""Creates collideable, invisible rectangles over top of the ground for
sprites to walk on"""
ground_rect1 = collider.Collider(0, c.GROUND_HEIGHT, 2953, 60)
ground_rect2 = collider.Collider(3048, c.GROUND_HEIGHT, 635, 60)
ground_rect3 = collider.Collider(3819, c.GROUND_HEIGHT, 2735, 60)
ground_rect4 = collider.Collider(6647, c.GROUND_HEIGHT, 2300, 60)
self.ground_group = pg.sprite.Group(ground_rect1,
ground_rect2,
ground_rect3,
ground_rect4)
def setup_pipes(self):
"""Create collideable rects for all the pipes"""
pipe1 = collider.Collider(1202, 452, 83, 82)
pipe2 = collider.Collider(1631, 409, 83, 140)
pipe3 = collider.Collider(1973, 366, 83, 170)
pipe4 = collider.Collider(2445, 366, 83, 170)
pipe5 = collider.Collider(6989, 452, 83, 82)
pipe6 = collider.Collider(7675, 452, 83, 82)
self.pipe_group = pg.sprite.Group(pipe1, pipe2,
pipe3, pipe4,
pipe5, pipe6)
def setup_steps(self):
"""Create collideable rects for all the steps"""
step1 = collider.Collider(5745, 495, 40, 44)
step2 = collider.Collider(5788, 452, 40, 44)
step3 = collider.Collider(5831, 409, 40, 44)
step4 = collider.Collider(5874, 366, 40, 176)
step5 = collider.Collider(6001, 366, 40, 176)
step6 = collider.Collider(6044, 408, 40, 40)
step7 = collider.Collider(6087, 452, 40, 40)
step8 = collider.Collider(6130, 495, 40, 40)
step9 = collider.Collider(6345, 495, 40, 40)
step10 = collider.Collider(6388, 452, 40, 40)
step11 = collider.Collider(6431, 409, 40, 40)
step12 = collider.Collider(6474, 366, 40, 40)
step13 = collider.Collider(6517, 366, 40, 176)
step14 = collider.Collider(6644, 366, 40, 176)
step15 = collider.Collider(6687, 408, 40, 40)
step16 = collider.Collider(6728, 452, 40, 40)
step17 = collider.Collider(6771, 495, 40, 40)
step18 = collider.Collider(7760, 495, 40, 40)
step19 = collider.Collider(7803, 452, 40, 40)
step20 = collider.Collider(7845, 409, 40, 40)
step21 = collider.Collider(7888, 366, 40, 40)
step22 = collider.Collider(7931, 323, 40, 40)
step23 = collider.Collider(7974, 280, 40, 40)
step24 = collider.Collider(8017, 237, 40, 40)
step25 = collider.Collider(8060, 194, 40, 40)
step26 = collider.Collider(8103, 194, 40, 360)
step27 = collider.Collider(8488, 495, 40, 40)
self.step_group = pg.sprite.Group(step1, step2,
step3, step4,
step5, step6,
step7, step8,
step9, step10,
step11, step12,
step13, step14,
step15, step16,
step17, step18,
step19, step20,
step21, step22,
step23, step24,
step25, step26,
step27)
def setup_bricks(self):
"""Creates all the breakable bricks for the level. Coin and
powerup groups are created, so they can be passed to bricks."""
self.coin_group = pg.sprite.Group()
self.powerup_group = pg.sprite.Group()
self.brick_pieces_group = pg.sprite.Group()
brick1 = bricks.Brick(858, 365)
brick2 = bricks.Brick(944, 365)
brick3 = bricks.Brick(1030, 365)
brick4 = bricks.Brick(3299, 365)
brick5 = bricks.Brick(3385, 365)
brick6 = bricks.Brick(3430, 193)
brick7 = bricks.Brick(3473, 193)
brick8 = bricks.Brick(3516, 193)
brick9 = bricks.Brick(3559, 193)
brick10 = bricks.Brick(3602, 193)
brick11 = bricks.Brick(3645, 193)
brick12 = bricks.Brick(3688, 193)
brick13 = bricks.Brick(3731, 193)
brick14 = bricks.Brick(3901, 193)
brick15 = bricks.Brick(3944, 193)
brick16 = bricks.Brick(3987, 193)
brick17 = bricks.Brick(4030, 365, c.SIXCOINS, self.coin_group)
brick18 = bricks.Brick(4287, 365)
brick19 = bricks.Brick(4330, 365, c.STAR, self.powerup_group)
brick20 = bricks.Brick(5058, 365)
brick21 = bricks.Brick(5187, 193)
brick22 = bricks.Brick(5230, 193)
brick23 = bricks.Brick(5273, 193)
brick24 = bricks.Brick(5488, 193)
brick25 = bricks.Brick(5574, 193)
brick26 = bricks.Brick(5617, 193)
brick27 = bricks.Brick(5531, 365)
brick28 = bricks.Brick(5574, 365)
brick29 = bricks.Brick(7202, 365)
brick30 = bricks.Brick(7245, 365)
brick31 = bricks.Brick(7331, 365)
self.brick_group = pg.sprite.Group(brick1, brick2,
brick3, brick4,
brick5, brick6,
brick7, brick8,
brick9, brick10,
brick11, brick12,
brick13, brick14,
brick15, brick16,
brick17, brick18,
brick19, brick20,
brick21, brick22,
brick23, brick24,
brick25, brick26,
brick27, brick28,
brick29, brick30,
brick31)
def setup_coin_boxes(self):
"""Creates all the coin boxes and puts them in a sprite group"""
coin_box1 = coin_box.Coin_box(685, 365, c.COIN, self.coin_group)
coin_box2 = coin_box.Coin_box(901, 365, c.MUSHROOM, self.powerup_group)
coin_box3 = coin_box.Coin_box(987, 365, c.COIN, self.coin_group)
coin_box4 = coin_box.Coin_box(943, 193, c.COIN, self.coin_group)
coin_box5 = coin_box.Coin_box(3342, 365, c.MUSHROOM, self.powerup_group)
coin_box6 = coin_box.Coin_box(4030, 193, c.COIN, self.coin_group)
coin_box7 = coin_box.Coin_box(4544, 365, c.COIN, self.coin_group)
coin_box8 = coin_box.Coin_box(4672, 365, c.COIN, self.coin_group)
coin_box9 = coin_box.Coin_box(4672, 193, c.MUSHROOM, self.powerup_group)
coin_box10 = coin_box.Coin_box(4800, 365, c.COIN, self.coin_group)
coin_box11 = coin_box.Coin_box(5531, 193, c.COIN, self.coin_group)
coin_box12 = coin_box.Coin_box(7288, 365, c.COIN, self.coin_group)
self.coin_box_group = pg.sprite.Group(coin_box1, coin_box2,
coin_box3, coin_box4,
coin_box5, coin_box6,
coin_box7, coin_box8,
coin_box9, coin_box10,
coin_box11, coin_box12)
def setup_flag_pole(self):
"""Creates the flag pole at the end of the level"""
self.flag = flagpole.Flag(8505, 100)
pole0 = flagpole.Pole(8505, 97)
pole1 = flagpole.Pole(8505, 137)
pole2 = flagpole.Pole(8505, 177)
pole3 = flagpole.Pole(8505, 217)
pole4 = flagpole.Pole(8505, 257)
pole5 = flagpole.Pole(8505, 297)
pole6 = flagpole.Pole(8505, 337)
pole7 = flagpole.Pole(8505, 377)
pole8 = flagpole.Pole(8505, 417)
pole9 = flagpole.Pole(8505, 450)
finial = flagpole.Finial(8507, 97)
self.flag_pole_group = pg.sprite.Group(self.flag,
finial,
pole0,
pole1,
pole2,
pole3,
pole4,
pole5,
pole6,
pole7,
pole8,
pole9)
def setup_enemies(self):
"""Creates all the enemies and stores them in a list of lists."""
goomba0 = enemies.Goomba()
goomba1 = enemies.Goomba()
goomba2 = enemies.Goomba()
goomba3 = enemies.Goomba()
goomba4 = enemies.Goomba(193)
goomba5 = enemies.Goomba(193)
goomba6 = enemies.Goomba()
goomba7 = enemies.Goomba()
goomba8 = enemies.Goomba()
goomba9 = enemies.Goomba()
goomba10 = enemies.Goomba()
goomba11 = enemies.Goomba()
goomba12 = enemies.Goomba()
goomba13 = enemies.Goomba()
goomba14 = enemies.Goomba()
goomba15 = enemies.Goomba()
koopa0 = enemies.Koopa()
enemy_group1 = pg.sprite.Group(goomba0)
enemy_group2 = pg.sprite.Group(goomba1)
enemy_group3 = pg.sprite.Group(goomba2, goomba3)
enemy_group4 = pg.sprite.Group(goomba4, goomba5)
enemy_group5 = pg.sprite.Group(goomba6, goomba7)
enemy_group6 = pg.sprite.Group(koopa0)
enemy_group7 = pg.sprite.Group(goomba8, goomba9)
enemy_group8 = pg.sprite.Group(goomba10, goomba11)
enemy_group9 = pg.sprite.Group(goomba12, goomba13)
enemy_group10 = pg.sprite.Group(goomba14, goomba15)
self.enemy_group_list = [enemy_group1,
enemy_group2,
enemy_group3,
enemy_group4,
enemy_group5,
enemy_group6,
enemy_group7,
enemy_group8,
enemy_group9,
enemy_group10]
def setup_mario(self):
"""Places Mario at the beginning of the level"""
self.mario = mario.Mario()
self.mario.rect.x = self.viewport.x + 110
self.mario.rect.bottom = c.GROUND_HEIGHT
def setup_checkpoints(self):
"""Creates invisible checkpoints that when collided will trigger
the creation of enemies from the self.enemy_group_list"""
check1 = checkpoint.Checkpoint(510, "1")
check2 = checkpoint.Checkpoint(1400, '2')
check3 = checkpoint.Checkpoint(1740, '3')
check4 = checkpoint.Checkpoint(3080, '4')
check5 = checkpoint.Checkpoint(3750, '5')
check6 = checkpoint.Checkpoint(4150, '6')
check7 = checkpoint.Checkpoint(4470, '7')
check8 = checkpoint.Checkpoint(4950, '8')
check9 = checkpoint.Checkpoint(5100, '9')
check10 = checkpoint.Checkpoint(6800, '10')
check11 = checkpoint.Checkpoint(8504, '11', 5, 6)
check12 = checkpoint.Checkpoint(8775, '12')
check13 = checkpoint.Checkpoint(2740, 'secret_mushroom', 360, 40, 12)
self.check_point_group = pg.sprite.Group(check1, check2, check3,
check4, check5, check6,
check7, check8, check9,
check10, check11, check12,
check13)
def setup_spritegroups(self):
"""Sprite groups created for convenience"""
self.sprites_about_to_die_group = pg.sprite.Group()
self.shell_group = pg.sprite.Group()
self.enemy_group = pg.sprite.Group()
self.ground_step_pipe_group = pg.sprite.Group(self.ground_group,
self.pipe_group,
self.step_group)
self.mario_and_enemy_group = pg.sprite.Group(self.mario,
self.enemy_group)
def update(self, surface, keys, current_time):
"""Updates Entire level using states. Called by the control object"""
self.game_info[c.CURRENT_TIME] = self.current_time = current_time
self.handle_states(keys)
self.check_if_time_out()
self.blit_everything(surface)
self.sound_manager.update(self.game_info, self.mario)
def handle_states(self, keys):
"""If the level is in a FROZEN state, only mario will update"""
if self.state == c.FROZEN:
self.update_during_transition_state(keys)
elif self.state == c.NOT_FROZEN:
self.update_all_sprites(keys)
elif self.state == c.IN_CASTLE:
self.update_while_in_castle()
elif self.state == c.FLAG_AND_FIREWORKS:
self.update_flag_and_fireworks()
def update_during_transition_state(self, keys):
"""Updates mario in a transition state (like becoming big, small,
or dies). Checks if he leaves the transition state or dies to
change the level state back"""
self.mario.update(keys, self.game_info, self.powerup_group)
for score in self.moving_score_list:
score.update(self.moving_score_list, self.game_info)
if self.flag_score:
self.flag_score.update(None, self.game_info)
self.check_to_add_flag_score()
self.coin_box_group.update(self.game_info)
self.flag_pole_group.update(self.game_info)
self.check_if_mario_in_transition_state()
self.check_flag()
self.check_for_mario_death()
self.overhead_info_display.update(self.game_info, self.mario)
def check_if_mario_in_transition_state(self):
"""If mario is in a transition state, the level will be in a FREEZE
state"""
if self.mario.in_transition_state:
self.game_info[c.LEVEL_STATE] = self.state = c.FROZEN
elif not self.mario.in_transition_state:
if self.state == c.FROZEN:
self.game_info[c.LEVEL_STATE] = self.state = c.NOT_FROZEN
def update_all_sprites(self, keys):
"""Updates the location of all sprites on the screen."""
self.mario.update(keys, self.game_info, self.powerup_group)
for score in self.moving_score_list:
score.update(self.moving_score_list, self.game_info)
if self.flag_score:
self.flag_score.update(None, self.game_info)
self.check_to_add_flag_score()
self.flag_pole_group.update()
self.check_points_check()
self.enemy_group.update(self.game_info)
self.sprites_about_to_die_group.update(self.game_info, self.viewport)
self.shell_group.update(self.game_info)
self.brick_group.update()
self.coin_box_group.update(self.game_info)
self.powerup_group.update(self.game_info, self.viewport)
self.coin_group.update(self.game_info, self.viewport)
self.brick_pieces_group.update()
self.adjust_sprite_positions()
self.check_if_mario_in_transition_state()
self.check_for_mario_death()
self.update_viewport()
self.overhead_info_display.update(self.game_info, self.mario)
def check_points_check(self):
"""Detect if checkpoint collision occurs, delete checkpoint,
add enemies to self.enemy_group"""
checkpoint = pg.sprite.spritecollideany(self.mario,
self.check_point_group)
if checkpoint:
checkpoint.kill()
for i in range(1, 11):
if checkpoint.name == str(i):
for index, enemy in enumerate(self.enemy_group_list[i - 1]):
enemy.rect.x = self.viewport.right + (index * 60)
self.enemy_group.add(self.enemy_group_list[i - 1])
if checkpoint.name == '11':
self.mario.state = c.FLAGPOLE
self.mario.invincible = False
self.mario.flag_pole_right = checkpoint.rect.right
if self.mario.rect.bottom < self.flag.rect.y:
self.mario.rect.bottom = self.flag.rect.y
self.flag.state = c.SLIDE_DOWN
self.create_flag_points()
elif checkpoint.name == '12':
self.state = c.IN_CASTLE
self.mario.kill()
self.mario.state == c.STAND
self.mario.in_castle = True
self.overhead_info_display.state = c.FAST_COUNT_DOWN
elif checkpoint.name == 'secret_mushroom' and self.mario.y_vel < 0:
mushroom_box = coin_box.Coin_box(checkpoint.rect.x,
checkpoint.rect.bottom - 40,
'1up_mushroom',
self.powerup_group)
mushroom_box.start_bump(self.moving_score_list)
self.coin_box_group.add(mushroom_box)
self.mario.y_vel = 7
self.mario.rect.y = mushroom_box.rect.bottom
self.mario.state = c.FALL
self.mario_and_enemy_group.add(self.enemy_group)
def create_flag_points(self):
"""Creates the points that appear when Mario touches the
flag pole"""
x = 8518
y = c.GROUND_HEIGHT - 60
mario_bottom = self.mario.rect.bottom
if mario_bottom > (c.GROUND_HEIGHT - 40 - 40):
self.flag_score = score.Score(x, y, 100, True)
self.flag_score_total = 100
elif mario_bottom > (c.GROUND_HEIGHT - 40 - 160):
self.flag_score = score.Score(x, y, 400, True)
self.flag_score_total = 400
elif mario_bottom > (c.GROUND_HEIGHT - 40 - 240):
self.flag_score = score.Score(x, y, 800, True)
self.flag_score_total = 800
elif mario_bottom > (c.GROUND_HEIGHT - 40 - 360):
self.flag_score = score.Score(x, y, 2000, True)
self.flag_score_total = 2000
else:
self.flag_score = score.Score(x, y, 5000, True)
self.flag_score_total = 5000
def adjust_sprite_positions(self):
"""Adjusts sprites by their x and y velocities and collisions"""
self.adjust_mario_position()
self.adjust_enemy_position()
self.adjust_shell_position()
self.adjust_powerup_position()
def adjust_mario_position(self):
"""Adjusts Mario's position based on his x, y velocities and
potential collisions"""
self.last_x_position = self.mario.rect.right
self.mario.rect.x += round(self.mario.x_vel)
self.check_mario_x_collisions()
if not self.mario.in_transition_state:
self.mario.rect.y += round(self.mario.y_vel)
self.check_mario_y_collisions()
if self.mario.rect.x < (self.viewport.x + 5):
self.mario.rect.x = (self.viewport.x + 5)
def check_mario_x_collisions(self):
"""Check for collisions after Mario is moved on the x axis"""
collider = pg.sprite.spritecollideany(self.mario, self.ground_step_pipe_group)
coin_box = pg.sprite.spritecollideany(self.mario, self.coin_box_group)
brick = pg.sprite.spritecollideany(self.mario, self.brick_group)
enemy = pg.sprite.spritecollideany(self.mario, self.enemy_group)
shell = pg.sprite.spritecollideany(self.mario, self.shell_group)
powerup = pg.sprite.spritecollideany(self.mario, self.powerup_group)
if coin_box:
self.adjust_mario_for_x_collisions(coin_box)
elif brick:
self.adjust_mario_for_x_collisions(brick)
elif collider:
self.adjust_mario_for_x_collisions(collider)
elif enemy:
if self.mario.invincible:
setup.SFX['kick'].play()
self.game_info[c.SCORE] += 100
self.moving_score_list.append(
score.Score(self.mario.rect.right - self.viewport.x,
self.mario.rect.y, 100))
enemy.kill()
enemy.start_death_jump(c.RIGHT)
self.sprites_about_to_die_group.add(enemy)
elif self.mario.big:
setup.SFX['pipe'].play()
self.mario.fire = False
self.mario.y_vel = -1
self.mario.state = c.BIG_TO_SMALL
self.convert_fireflowers_to_mushrooms()
elif self.mario.hurt_invincible:
pass
else:
self.mario.start_death_jump(self.game_info)
self.state = c.FROZEN
elif shell:
self.adjust_mario_for_x_shell_collisions(shell)
elif powerup:
if powerup.name == c.STAR:
self.game_info[c.SCORE] += 1000
self.moving_score_list.append(
score.Score(self.mario.rect.centerx - self.viewport.x,
self.mario.rect.y, 1000))
self.mario.invincible = True
self.mario.invincible_start_timer = self.current_time
elif powerup.name == c.MUSHROOM:
setup.SFX['powerup'].play()
self.game_info[c.SCORE] += 1000
self.moving_score_list.append(
score.Score(self.mario.rect.centerx - self.viewport.x,
self.mario.rect.y - 20, 1000))
self.mario.y_vel = -1
self.mario.state = c.SMALL_TO_BIG
self.mario.in_transition_state = True
self.convert_mushrooms_to_fireflowers()
elif powerup.name == c.LIFE_MUSHROOM:
self.moving_score_list.append(
score.Score(powerup.rect.right - self.viewport.x,
powerup.rect.y,
c.ONEUP))
self.game_info[c.LIVES] += 1
setup.SFX['one_up'].play()
elif powerup.name == c.FIREFLOWER:
setup.SFX['powerup'].play()
self.game_info[c.SCORE] += 1000
self.moving_score_list.append(
score.Score(self.mario.rect.centerx - self.viewport.x,
self.mario.rect.y, 1000))
if self.mario.big and self.mario.fire == False:
self.mario.state = c.BIG_TO_FIRE
self.mario.in_transition_state = True
elif not self.mario.big:
self.mario.state = c.SMALL_TO_BIG
self.mario.in_transition_state = True
self.convert_mushrooms_to_fireflowers()
if powerup.name != c.FIREBALL:
powerup.kill()
def convert_mushrooms_to_fireflowers(self):
"""When Mario becomes big, converts all fireflower powerups to
mushroom powerups"""
for brick in self.brick_group:
if brick.contents == c.MUSHROOM:
brick.contents = c.FIREFLOWER
for coin_box in self.coin_box_group:
if coin_box.contents == c.MUSHROOM:
coin_box.contents = c.FIREFLOWER
def convert_fireflowers_to_mushrooms(self):
"""When Mario becomes small, converts all mushroom powerups to
fireflower powerups"""
for brick in self.brick_group:
if brick.contents == c.FIREFLOWER:
brick.contents = c.MUSHROOM
for coin_box in self.coin_box_group:
if coin_box.contents == c.FIREFLOWER:
coin_box.contents = c.MUSHROOM
def adjust_mario_for_x_collisions(self, collider):
"""Puts Mario flush next to the collider after moving on the x-axis"""
if self.mario.rect.x < collider.rect.x:
self.mario.rect.right = collider.rect.left
else:
self.mario.rect.left = collider.rect.right
self.mario.x_vel = 0
def adjust_mario_for_x_shell_collisions(self, shell):
"""Deals with Mario if he hits a shell moving on the x-axis"""
if shell.state == c.JUMPED_ON:
if self.mario.rect.x < shell.rect.x:
self.game_info[c.SCORE] += 400
self.moving_score_list.append(
score.Score(shell.rect.centerx - self.viewport.x,
shell.rect.y,
400))
self.mario.rect.right = shell.rect.left
shell.direction = c.RIGHT
shell.x_vel = 5
shell.rect.x += 5
else:
self.mario.rect.left = shell.rect.right
shell.direction = c.LEFT
shell.x_vel = -5
shell.rect.x += -5
shell.state = c.SHELL_SLIDE
elif shell.state == c.SHELL_SLIDE:
if self.mario.big and not self.mario.invincible:
self.mario.state = c.BIG_TO_SMALL
elif self.mario.invincible:
self.game_info[c.SCORE] += 200
self.moving_score_list.append(
score.Score(shell.rect.right - self.viewport.x,
shell.rect.y, 200))
shell.kill()
self.sprites_about_to_die_group.add(shell)
shell.start_death_jump(c.RIGHT)
else:
if not self.mario.hurt_invincible and not self.mario.invincible:
self.state = c.FROZEN
self.mario.start_death_jump(self.game_info)
def check_mario_y_collisions(self):
"""Checks for collisions when Mario moves along the y-axis"""
ground_step_or_pipe = pg.sprite.spritecollideany(self.mario, self.ground_step_pipe_group)
enemy = pg.sprite.spritecollideany(self.mario, self.enemy_group)
shell = pg.sprite.spritecollideany(self.mario, self.shell_group)
brick = pg.sprite.spritecollideany(self.mario, self.brick_group)
coin_box = pg.sprite.spritecollideany(self.mario, self.coin_box_group)
powerup = pg.sprite.spritecollideany(self.mario, self.powerup_group)
brick, coin_box = self.prevent_collision_conflict(brick, coin_box)
if coin_box:
self.adjust_mario_for_y_coin_box_collisions(coin_box)
elif brick:
self.adjust_mario_for_y_brick_collisions(brick)
elif ground_step_or_pipe:
self.adjust_mario_for_y_ground_pipe_collisions(ground_step_or_pipe)
elif enemy:
if self.mario.invincible:
setup.SFX['kick'].play()
enemy.kill()
self.sprites_about_to_die_group.add(enemy)
enemy.start_death_jump(c.RIGHT)
else:
self.adjust_mario_for_y_enemy_collisions(enemy)
elif shell:
self.adjust_mario_for_y_shell_collisions(shell)
elif powerup:
if powerup.name == c.STAR:
setup.SFX['powerup'].play()
powerup.kill()
self.mario.invincible = True
self.mario.invincible_start_timer = self.current_time
self.test_if_mario_is_falling()
def prevent_collision_conflict(self, obstacle1, obstacle2):
"""Allows collisions only for the item closest to marios centerx"""
if obstacle1 and obstacle2:
obstacle1_distance = self.mario.rect.centerx - obstacle1.rect.centerx
if obstacle1_distance < 0:
obstacle1_distance *= -1
obstacle2_distance = self.mario.rect.centerx - obstacle2.rect.centerx
if obstacle2_distance < 0:
obstacle2_distance *= -1
if obstacle1_distance < obstacle2_distance:
obstacle2 = False
else:
obstacle1 = False
return obstacle1, obstacle2
def adjust_mario_for_y_coin_box_collisions(self, coin_box):
"""Mario collisions with coin boxes on the y-axis"""
if self.mario.rect.y > coin_box.rect.y:
if coin_box.state == c.RESTING:
if coin_box.contents == c.COIN:
self.game_info[c.SCORE] += 200
coin_box.start_bump(self.moving_score_list)
if coin_box.contents == c.COIN:
self.game_info[c.COIN_TOTAL] += 1
else:
coin_box.start_bump(self.moving_score_list)
elif coin_box.state == c.OPENED:
pass
setup.SFX['bump'].play()
self.mario.y_vel = 7
self.mario.rect.y = coin_box.rect.bottom
self.mario.state = c.FALL
else:
self.mario.y_vel = 0
self.mario.rect.bottom = coin_box.rect.top
self.mario.state = c.WALK
def adjust_mario_for_y_brick_collisions(self, brick):
"""Mario collisions with bricks on the y-axis"""
if self.mario.rect.y > brick.rect.y:
if brick.state == c.RESTING:
if self.mario.big and brick.contents is None:
setup.SFX['brick_smash'].play()
self.check_if_enemy_on_brick(brick)
brick.kill()
self.brick_pieces_group.add(
bricks.BrickPiece(brick.rect.x,
brick.rect.y - (brick.rect.height / 2),
-2, -12),
bricks.BrickPiece(brick.rect.right,
brick.rect.y - (brick.rect.height / 2),
2, -12),
bricks.BrickPiece(brick.rect.x,
brick.rect.y,
-2, -6),
bricks.BrickPiece(brick.rect.right,
brick.rect.y,
2, -6))
else:
setup.SFX['bump'].play()
if brick.coin_total > 0:
self.game_info[c.COIN_TOTAL] += 1
self.game_info[c.SCORE] += 200
self.check_if_enemy_on_brick(brick)
brick.start_bump(self.moving_score_list)
elif brick.state == c.OPENED:
setup.SFX['bump'].play()
self.mario.y_vel = 7
self.mario.rect.y = brick.rect.bottom
self.mario.state = c.FALL
else:
self.mario.y_vel = 0
self.mario.rect.bottom = brick.rect.top
self.mario.state = c.WALK
def check_if_enemy_on_brick(self, brick):
"""Kills enemy if on a bumped or broken brick"""
brick.rect.y -= 5
enemy = pg.sprite.spritecollideany(brick, self.enemy_group)
if enemy:
setup.SFX['kick'].play()
self.game_info[c.SCORE] += 100
self.moving_score_list.append(
score.Score(enemy.rect.centerx - self.viewport.x,
enemy.rect.y,
100))
enemy.kill()
self.sprites_about_to_die_group.add(enemy)
if self.mario.rect.centerx > brick.rect.centerx:
enemy.start_death_jump('right')
else:
enemy.start_death_jump('left')
brick.rect.y += 5
def adjust_mario_for_y_ground_pipe_collisions(self, collider):
"""Mario's collisions with pipes on the y-axis"""
if collider.rect.bottom > self.mario.rect.bottom:
self.mario.y_vel = 0
self.mario.rect.bottom = collider.rect.top
if self.mario.state == c.END_OF_LEVEL_FALL:
self.mario.state = c.WALKING_TO_CASTLE
else:
self.mario.state = c.WALK
elif collider.rect.top < self.mario.rect.top:
self.mario.y_vel = 7
self.mario.rect.top = collider.rect.bottom
self.mario.state = c.FALL
def test_if_mario_is_falling(self):
"""Changes Mario to a FALL state if more than a pixel above a pipe,
ground, step or box"""
self.mario.rect.y += 1
test_collide_group = pg.sprite.Group(self.ground_step_pipe_group,
self.brick_group,
self.coin_box_group)
if pg.sprite.spritecollideany(self.mario, test_collide_group) is None:
if self.mario.state != c.JUMP \
and self.mario.state != c.DEATH_JUMP \
and self.mario.state != c.SMALL_TO_BIG \
and self.mario.state != c.BIG_TO_FIRE \
and self.mario.state != c.BIG_TO_SMALL \
and self.mario.state != c.FLAGPOLE \
and self.mario.state != c.WALKING_TO_CASTLE \
and self.mario.state != c.END_OF_LEVEL_FALL:
self.mario.state = c.FALL
elif self.mario.state == c.WALKING_TO_CASTLE or \
self.mario.state == c.END_OF_LEVEL_FALL:
self.mario.state = c.END_OF_LEVEL_FALL
self.mario.rect.y -= 1
def adjust_mario_for_y_enemy_collisions(self, enemy):
"""Mario's collisions with all enemies on the y-axis"""
if self.mario.y_vel > 0:
setup.SFX['stomp'].play()
self.game_info[c.SCORE] += 100
self.moving_score_list.append(
score.Score(enemy.rect.centerx - self.viewport.x,
enemy.rect.y, 100))
enemy.state = c.JUMPED_ON
enemy.kill()
if enemy.name == c.GOOMBA:
enemy.death_timer = self.current_time
self.sprites_about_to_die_group.add(enemy)
elif enemy.name == c.KOOPA:
self.shell_group.add(enemy)
self.mario.rect.bottom = enemy.rect.top
self.mario.state = c.JUMP
self.mario.y_vel = -7
def adjust_mario_for_y_shell_collisions(self, shell):
"""Mario's collisions with Koopas in their shells on the y-axis"""
if self.mario.y_vel > 0:
self.game_info[c.SCORE] += 400
self.moving_score_list.append(
score.Score(self.mario.rect.centerx - self.viewport.x,
self.mario.rect.y, 400))
if shell.state == c.JUMPED_ON:
setup.SFX['kick'].play()
shell.state = c.SHELL_SLIDE
if self.mario.rect.centerx < shell.rect.centerx:
shell.direction = c.RIGHT
shell.rect.left = self.mario.rect.right + 5
else:
shell.direction = c.LEFT
shell.rect.right = self.mario.rect.left - 5
else:
shell.state = c.JUMPED_ON
def adjust_enemy_position(self):
"""Moves all enemies along the x, y axes and check for collisions"""
for enemy in self.enemy_group:
enemy.rect.x += enemy.x_vel
self.check_enemy_x_collisions(enemy)
enemy.rect.y += enemy.y_vel
self.check_enemy_y_collisions(enemy)
self.delete_if_off_screen(enemy)
def check_enemy_x_collisions(self, enemy):
"""Enemy collisions along the x-axis. Removes enemy from enemy group
in order to check against all other enemies then adds it back."""
enemy.kill()
collider = pg.sprite.spritecollideany(enemy, self.ground_step_pipe_group)
enemy_collider = pg.sprite.spritecollideany(enemy, self.enemy_group)
if collider:
if enemy.direction == c.RIGHT:
enemy.rect.right = collider.rect.left
enemy.direction = c.LEFT
enemy.x_vel = -2
elif enemy.direction == c.LEFT:
enemy.rect.left = collider.rect.right
enemy.direction = c.RIGHT
enemy.x_vel = 2
elif enemy_collider:
if enemy.direction == c.RIGHT:
enemy.rect.right = enemy_collider.rect.left
enemy.direction = c.LEFT
enemy_collider.direction = c.RIGHT
enemy.x_vel = -2
enemy_collider.x_vel = 2
elif enemy.direction == c.LEFT:
enemy.rect.left = enemy_collider.rect.right
enemy.direction = c.RIGHT
enemy_collider.direction = c.LEFT
enemy.x_vel = 2
enemy_collider.x_vel = -2
self.enemy_group.add(enemy)
self.mario_and_enemy_group.add(self.enemy_group)
def check_enemy_y_collisions(self, enemy):
"""Enemy collisions on the y-axis"""
collider = pg.sprite.spritecollideany(enemy, self.ground_step_pipe_group)
brick = pg.sprite.spritecollideany(enemy, self.brick_group)
coin_box = pg.sprite.spritecollideany(enemy, self.coin_box_group)
if collider:
if enemy.rect.bottom > collider.rect.bottom:
enemy.y_vel = 7
enemy.rect.top = collider.rect.bottom
enemy.state = c.FALL
elif enemy.rect.bottom < collider.rect.bottom:
enemy.y_vel = 0
enemy.rect.bottom = collider.rect.top
enemy.state = c.WALK
elif brick:
if brick.state == c.BUMPED:
enemy.kill()
self.sprites_about_to_die_group.add(enemy)
if self.mario.rect.centerx > brick.rect.centerx:
enemy.start_death_jump('right')
else:
enemy.start_death_jump('left')
elif enemy.rect.x > brick.rect.x:
enemy.y_vel = 7
enemy.rect.top = brick.rect.bottom
enemy.state = c.FALL
else:
enemy.y_vel = 0
enemy.rect.bottom = brick.rect.top
enemy.state = c.WALK
elif coin_box:
if coin_box.state == c.BUMPED:
self.game_info[c.SCORE] += 100
self.moving_score_list.append(
score.Score(enemy.rect.centerx - self.viewport.x,
enemy.rect.y, 100))
enemy.kill()
self.sprites_about_to_die_group.add(enemy)
if self.mario.rect.centerx > coin_box.rect.centerx:
enemy.start_death_jump('right')
else:
enemy.start_death_jump('left')
elif enemy.rect.x > coin_box.rect.x:
enemy.y_vel = 7
enemy.rect.top = coin_box.rect.bottom
enemy.state = c.FALL
else:
enemy.y_vel = 0
enemy.rect.bottom = coin_box.rect.top
enemy.state = c.WALK
else:
enemy.rect.y += 1
test_group = pg.sprite.Group(self.ground_step_pipe_group,
self.coin_box_group,
self.brick_group)
if pg.sprite.spritecollideany(enemy, test_group) is None:
if enemy.state != c.JUMP:
enemy.state = c.FALL
enemy.rect.y -= 1
def adjust_shell_position(self):
"""Moves any koopa in a shell along the x, y axes and checks for
collisions"""
for shell in self.shell_group:
shell.rect.x += shell.x_vel
self.check_shell_x_collisions(shell)
shell.rect.y += shell.y_vel
self.check_shell_y_collisions(shell)
self.delete_if_off_screen(shell)
def check_shell_x_collisions(self, shell):
"""Shell collisions along the x-axis"""
collider = pg.sprite.spritecollideany(shell, self.ground_step_pipe_group)