-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1202 lines (1049 loc) · 45.8 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
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
import pygame
from pygame.locals import *
from time import time
from datetime import datetime, timedelta
import json
import sys
# Define FPS
FPS = 60 # Cap at 60 FPS
# Define screen dimensions
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
# Define physics values
ACC = 0.6 # Set player acceleration constant
FRIC = -0.12 # Set friction constant
GRAV = 0.45 # Set gravitational acceleration constant
JUMP = -12 # Set jump velocity constant
JUMP_MIN = -3 # Set minimum jump velocity
JUMP_WINDOW = 6 # Set frame window where jump can be performed after falling off platform
MAX_FALL_SPEED = 14 # This prevents a glitch where player moves through floor before check
# Define RGB color primitives
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Define global arrays
TileArray = [] # Define array that stores all bounding objects
HazardArray = [] # Define array that stores objects dangerous to player
StageExitArray = [] # Define array that stores stage_exit objects
CollectibleArray = [] # Define array that stores collectible objects
RespawnPointArray = [] # Define array that stores respawn point objects
SpikeArray = [] # Define array that stores spikes that are harmful to falling player
SlowArray = [] # Define array that stores objects that slow the player
DecorativeArray = [] # Define array that stores decorative objects
LockArray = []
LockLeafArray = []
KeyArray = []
SpecialKeyArray = []
SecretDoorArray = []
TombstoneArray = []
GutsArray = []
ChestArray = []
ReturnArray = []
FloatArray = []
EyeArray = []
RingArray = []
SwordArray = []
FinalDoorArray = []
# Definitions for convenience
vec = pygame.math.Vector2 # Defining simple reference to Vector2
SK1_COINS = 35
SK2_COINS = 94
FINAL_COINS = 158
has_sk1 = False
has_sk2 = False
has_ring = False
has_sword = False
has_xcancel = False
returned = False
# Overriding sprite class to make other classes more atomic
class Sprite(pygame.sprite.Sprite):
def __init__(self, image, spawn_x, spawn_y):
super().__init__()
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.center = [spawn_x, spawn_y]
self.num_jumps = 0
def update(self):
pass
def draw(self, screen):
screen.blit(self.image, self.rect)
class Player(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/player.bmp", spawn_x, spawn_y)
self.pos = vec((spawn_x, spawn_y))
self.vel = vec((0, 0))
self.acc = vec((0, 0))
self.air = 0 # Manual frame timer for forgiving jump mechanic [see: JUMP_WINDOW]
def move(self):
self.acc = vec(0, GRAV)
# Check for LEFT / RIGHT key presses
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.acc.x = -ACC
if keys[K_RIGHT]:
self.acc.x = ACC
if keys[K_DOWN]:
if has_xcancel:
self.vel.x = 0
# Handle movement and gravity
self.acc.x += self.vel.x * FRIC
self.vel += self.acc
self.pos.x += self.vel.x + 0.5 * self.acc.x
self.pos.y += self.vel.y + 0.5 * self.acc.y
# Handle bounds checking and screen wrap
if self.pos.x + 24 > SCREEN_WIDTH:
self.pos.x = SCREEN_WIDTH - 24
if self.pos.x < 24:
self.pos.x = 24
if self.pos.y < 32:
self.pos.y = 32
self.vel.y = 0
# Ensure rect is synced with player pos
self.rect.midbottom = self.pos
def jump(self, jump_fx):
if self.air < JUMP_WINDOW: # No double jumps
self.vel.y = JUMP
jump_fx.play()
self.num_jumps += 1
def cancel_jump(self):
if self.air > 0:
if self.vel.y < JUMP_MIN: # Cap vertical velocity to cancel jump
self.vel.y = JUMP_MIN
def update(self, obstacles, hazards, stage_exit, collectibles, respawn_point,
spikes, slow, keys, skeys, sdoors, chests, returns, rings, swords, fdoors):
if self.vel.y > MAX_FALL_SPEED:
self.vel.y = MAX_FALL_SPEED
self.move()
on_ground = False
# Handle jumping vertical collision detection
hits = pygame.sprite.spritecollide(self, obstacles, False) # Get all collisions (player->wall)
if self.vel.y > 0: # If player is falling
if hits: # && if collision is detected
if self.pos.y < hits[0].rect.bottom: # && player is beneath top edge of wall
self.pos.y = hits[0].rect.top + 1 # Move player above wall
self.vel.y = 0 # Set player vertical velocity to zero
self.air = 0 # Specify that player is no longer jumping
on_ground = True
if not on_ground: # Add to airtime frame counter
self.air += 1
hazard_hit = pygame.sprite.spritecollideany(self, hazards)
if hazard_hit:
return 1
exit_hit = pygame.sprite.spritecollideany(self, stage_exit)
if exit_hit:
return 2
coin_hit = pygame.sprite.spritecollide(self, collectibles, True)
if coin_hit:
return 3
respawn_hit = pygame.sprite.spritecollide(self, respawn_point, True)
if respawn_hit:
return 4
spike_hit = pygame.sprite.spritecollide(self, spikes, False)
if self.vel.y > 0:
if spike_hit:
return 1
slow_hit = pygame.sprite.spritecollide(self, slow, False)
if slow_hit:
return 5
key_hit = pygame.sprite.spritecollide(self, keys, True)
if key_hit:
return 6
skey_hit = pygame.sprite.spritecollide(self, skeys, True)
if skey_hit:
return 7
chest_hit = pygame.sprite.spritecollide(self, chests, True)
if chest_hit:
return 8
sdoor_hit = pygame.sprite.spritecollide(self, sdoors, False)
if sdoor_hit:
return 9
return_hit = pygame.sprite.spritecollide(self, returns, False)
if return_hit:
return 10
ring_hit = pygame.sprite.spritecollide(self, rings, True)
if ring_hit:
return 11
sword_hit = pygame.sprite.spritecollide(self, swords, True)
if sword_hit:
return 12
fdoor_hit = pygame.sprite.spritecollide(self, fdoors, False)
if fdoor_hit and has_sword:
return 13
return 0
class Wall(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/wall.bmp", spawn_x, spawn_y)
class Wall2(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/wall2.bmp", spawn_x, spawn_y)
class Wall3(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/wall3.bmp", spawn_x, spawn_y)
class Platform(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/platform.bmp", spawn_x, spawn_y)
class BadLeaf(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/badleaf.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 30 frames
if self.frame_timer == 30:
self.frame_timer = 0
self.ticker += 1
if self.ticker % 2 == 0:
self.image = pygame.image.load("res/img/badleaf.bmp")
if self.ticker % 2 == 1:
self.image = pygame.image.load("res/img/badleaf2.bmp")
if self.ticker == 1000: # Lazy insurance against overflow exception
self.ticker = 0
class Exit(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/exit.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 15 frames
if self.frame_timer == 30:
self.frame_timer = 0
self.ticker += 1
if self.ticker == 1:
self.image = pygame.image.load("res/img/exit.bmp")
if self.ticker == 8:
self.image = pygame.image.load("res/img/exit2.bmp")
self.ticker = 0
class Coin(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/coin1.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 5 frames
if self.frame_timer == 5:
self.frame_timer = 0
self.ticker += 1
if self.ticker > 4: self.ticker = 1
self.image = pygame.image.load(f"res/img/coin{self.ticker}.bmp")
class Respawn(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/respawn1.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 10 frames
if self.frame_timer == 10:
self.frame_timer = 0
self.ticker += 1
if self.ticker > 4: self.ticker = 1
self.image = pygame.image.load(f"res/img/respawn{self.ticker}.bmp")
class Grass(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/grass.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 15 frames
if self.frame_timer == 15:
self.frame_timer = 0
self.ticker += 1
if self.ticker % 2 == 0:
self.image = pygame.image.load("res/img/grass.bmp")
if self.ticker % 2 == 1:
self.image = pygame.image.load("res/img/grass2.bmp")
if self.ticker == 1000: # Lazy insurance against overflow exception
self.ticker = 0
class Bush(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/bush.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 30 frames
if self.frame_timer == 30:
self.frame_timer = 0
self.ticker += 1
if self.ticker % 2 == 0:
self.image = pygame.image.load("res/img/bush.bmp")
if self.ticker % 2 == 1:
self.image = pygame.image.load("res/img/bush2.bmp")
if self.ticker == 1000: # Lazy insurance against overflow exception
self.ticker = 0
class Spike(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/spike2.bmp", spawn_x, spawn_y)
class LockLeaf(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/lockleaf.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 30 frames
if self.frame_timer == 30:
self.frame_timer = 0
self.ticker += 1
if self.ticker % 2 == 0:
self.image = pygame.image.load("res/img/lockleaf.bmp")
if self.ticker % 2 == 1:
self.image = pygame.image.load("res/img/lockleaf2.bmp")
if self.ticker == 1000: # Lazy insurance against overflow exception
self.ticker = 0
class Lock(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/lock.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 30 frames
if self.frame_timer == 30:
self.frame_timer = 0
self.ticker += 1
if self.ticker % 2 == 0:
self.image = pygame.image.load("res/img/lock.bmp")
if self.ticker % 2 == 1:
self.image = pygame.image.load("res/img/lock2.bmp")
if self.ticker == 1000: # Lazy insurance against overflow exception
self.ticker = 0
class Key(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/key1.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 10 frames
if self.frame_timer == 10:
self.frame_timer = 0
self.ticker += 1
if self.ticker > 5: self.ticker = 1
self.image = pygame.image.load(f"res/img/key{self.ticker}.bmp")
class SecretDoor(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/secretdoorclosed.bmp", spawn_x, spawn_y)
def update(self):
if has_sk1:
self.image = pygame.image.load("res/img/secretdooropen.bmp")
class SecretDoor2(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/secretdoorclosed.bmp", spawn_x, spawn_y)
def update(self):
if has_sk2:
self.image = pygame.image.load("res/img/secretdooropen.bmp")
class SpecialKey(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/specialkey1.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 10 frames
if self.frame_timer == 10:
self.frame_timer = 0
self.ticker += 1
if self.ticker > 4: self.ticker = 1
self.image = pygame.image.load(f"res/img/specialkey{self.ticker}.bmp")
class SpecialKey2(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/specialkey1.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 10 frames
if self.frame_timer == 10:
self.frame_timer = 0
self.ticker += 1
if self.ticker > 4: self.ticker = 1
self.image = pygame.image.load(f"res/img/specialkey{self.ticker}.bmp")
class Chest(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/chest.bmp", spawn_x, spawn_y)
class Tombstone(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/rip.bmp", spawn_x, spawn_y)
class BrokenTombstone(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/broken.bmp", spawn_x, spawn_y)
class Guts(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/guts.bmp", spawn_x, spawn_y)
class ReturnDoor(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/returndoor.bmp", spawn_x, spawn_y)
class SWall(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/swall.bmp", spawn_x, spawn_y)
class FloatingPlatform(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/float.bmp", spawn_x, spawn_y)
self.x = spawn_x
self.y = spawn_y
self.speed = 2
def update(self):
self.rect.x += self.speed
if self.rect.x < 32:
self.speed *= -1
if self.rect.x > SCREEN_WIDTH - 48:
self.speed *= -1
class EyeEnemy(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/eye.bmp", spawn_x, spawn_y)
self.speed = 3
def update(self):
self.rect.y += self.speed
if self.rect.y > SCREEN_HEIGHT - 32:
self.rect.y = SCREEN_HEIGHT / 2
class EyeEnemyInvert(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/eye.bmp", spawn_x, spawn_y)
self.speed = 3
def update(self):
self.rect.y -= self.speed
if self.rect.y < 16:
self.rect.y = SCREEN_HEIGHT - 32
class Ring(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/ring1.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 10 frames
if self.frame_timer == 10:
self.frame_timer = 0
self.ticker += 1
if self.ticker > 3: self.ticker = 1
self.image = pygame.image.load(f"res/img/ring{self.ticker}.bmp")
class Sword(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/sword.bmp", spawn_x, spawn_y)
self.frame_timer = 0
self.ticker = 0
def update(self):
self.frame_timer += 1 # Simple animation by swapping image every 20 frames
if self.frame_timer == 20:
self.frame_timer = 0
self.ticker += 1
if self.ticker % 2 == 0:
self.image = pygame.image.load("res/img/sword.bmp")
if self.ticker % 2 == 1:
self.image = pygame.image.load("res/img/sword2.bmp")
if self.ticker == 1000: # Lazy insurance against overflow exception
self.ticker = 0
class FinalDoor(Sprite):
def __init__(self, spawn_x, spawn_y):
super().__init__("res/img/fdoorclosed.bmp", spawn_x, spawn_y)
def update(self):
if has_sword:
self.image = pygame.image.load("res/img/fdoor3.bmp")
def load_names():
with open("res/levels/names.json") as json_file:
data = json.load(json_file)
return data
def load_stage(filename, obstacles, hazards, stage_exit, collectibles, respawn,
spikes, slow, decorative, locks, lockleafs, keys, skeys, sdoors,
tombstones, chests, returns, floats, coins, current_stage, rings,
swords, fdoors):
with open(filename, 'r') as file: # Open specified text file
stage = file.read()
x_count = 0 # Create coordinates to traverse screen
y_count = 0
x_pos_str = stage[0:3]
y_pos_str = stage[3:6]
global returned
if stage[7] == 'x':
return vec(-1, -1)
else:
for tile in stage[7:]: # Step through file and interpret delimiters
if tile == ',':
x_count += 16
if x_count >= SCREEN_WIDTH:
x_count = 0
if tile == '\n':
y_count += 16
if tile == '1': # Create wall at current (x,y) location
if current_stage < 10:
new_wall = Wall(x_count + 8, y_count + 8)
elif current_stage < 18:
new_wall = Wall2(x_count + 8, y_count + 8)
else:
new_wall = Wall3(x_count + 8, y_count + 8)
obstacles.append(new_wall)
if tile == '2': # Create platform at current (x,y) location
new_platform = Platform(x_count + 8, y_count + 8)
obstacles.append(new_platform)
if tile == '3': # Create hazard at current (x,y) location
new_leaf = BadLeaf(x_count + 8, y_count + 8)
hazards.append(new_leaf)
if tile == '4': # Create stage exit at current (x,y) location
new_exit = Exit(x_count + 8, y_count + 8)
stage_exit.append(new_exit)
if tile == '5' and not returned: # Create coin at current (x,y) location
new_coin = Coin(x_count + 8, y_count + 8)
collectibles.append(new_coin)
if tile == '6': # Create respawn point at current (x,y) location
new_respawn = Respawn(x_count + 8, y_count + 8)
respawn.append(new_respawn)
if tile == '7': # Create grass at current (x,y) location
new_grass = Grass(x_count + 8, y_count + 8)
decorative.append(new_grass)
if tile == '8': # Create bush at current (x,y) location
new_bush = Bush(x_count + 8, y_count + 8)
slow.append(new_bush)
if tile == '9':
new_spike = Spike(x_count + 8, y_count + 8)
spikes.append(new_spike)
if tile == 'L':
new_lock = Lock(x_count + 8, y_count + 8)
locks.append(new_lock)
if tile == 'l':
new_lockleaf = LockLeaf(x_count + 8, y_count + 8)
lockleafs.append(new_lockleaf)
if tile == 'K':
new_key = Key(x_count + 8, y_count + 8)
keys.append(new_key)
if tile == 's':
if current_stage == 6 and coins >= SK1_COINS:
new_skey = SpecialKey(x_count + 8, y_count + 8)
skeys.append(new_skey)
if current_stage > 6 and coins >= SK2_COINS:
new_skey = SpecialKey2(x_count + 8, y_count + 8)
skeys.append(new_skey)
if tile == 'D':
if current_stage == 7:
new_sdoor = SecretDoor(x_count + 8, y_count + 8)
sdoors.append(new_sdoor)
if current_stage > 7:
new_sdoor = SecretDoor2(x_count + 8, y_count + 8)
sdoors.append(new_sdoor)
if tile == 'r':
new_rip = Tombstone(x_count + 8, y_count + 8)
tombstones.append(new_rip)
if tile == 'G':
new_guts = Guts(x_count + 8, y_count + 8)
slow.append(new_guts)
if tile == 'C':
new_chest = Chest(x_count + 8, y_count + 8)
chests.append(new_chest)
if tile == 'B':
new_return = ReturnDoor(x_count + 8, y_count + 8)
returns.append(new_return)
if tile == 'w':
new_swall = SWall(x_count + 8, y_count + 8)
obstacles.append(new_swall)
if tile == 'F':
new_float = FloatingPlatform(x_count + 8, y_count + 8)
floats.append(new_float)
if tile == 'R':
new_ring = Ring(x_count + 8, y_count + 8)
rings.append(new_ring)
if tile == 'E':
new_eye = EyeEnemy(x_count + 8, y_count + 8)
hazards.append(new_eye)
if tile == 'I':
new_eye = EyeEnemyInvert(x_count + 8, y_count + 8)
hazards.append(new_eye)
if tile == 'b':
new_brip = BrokenTombstone(x_count + 8, y_count + 8)
tombstones.append(new_brip)
if tile == 'S':
if has_ring:
new_sword = Sword(x_count + 8, y_count + 8)
swords.append(new_sword)
if tile == 'f':
new_fdoor = FinalDoor(x_count + 16, y_count + 24)
fdoors.append(new_fdoor)
return vec(int(x_pos_str), int(y_pos_str))
def main():
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
pygame.HWSURFACE | pygame.DOUBLEBUF, vsync=1) # | pygame.SCALED
bigscreen = False
pygame.display.set_caption("Lymynal Labrynthe") # Assign name to window
game_icon = pygame.image.load("res/img/icon.png")
pygame.display.set_icon(game_icon)
clock = pygame.time.Clock() # Clock for syncing updates to frame rate
stage_loaded = False # Defining bool to ensure stage is loaded
current_stage = 0 # Defining int to keep track of current stage
player_deaths = 0 # Defining int to keep track of player deaths
coins = 0 # Defining int to keep track of collected coins
total_jumps = 0
spawn = vec(0, 0) # Set default spawn
respawn = vec(0, 0) # Set default respawn
screenshot_num = 0
player = Player(spawn.x, spawn.y) # Spawn player
sprites = pygame.sprite.Group() # Create group for all game objects
sprites.add(player) # Add player to master sprite group
obstacles = pygame.sprite.Group() # Create group for decorative and platform objects
hazards = pygame.sprite.Group() # Create group for objects dangerous to player
stage_exit = pygame.sprite.Group() # Create group for stage exit objects
collectibles = pygame.sprite.Group() # Create group for collectible game objects
respawn_point = pygame.sprite.Group() # Create group for respawn objects
spikes = pygame.sprite.Group() # Create group for spike objects
slow = pygame.sprite.Group() # Create group for objects that slow the player
decorative = pygame.sprite.Group() # Create group for decorative objects
lockleafs = pygame.sprite.Group()
keys = pygame.sprite.Group()
skeys = pygame.sprite.Group()
sdoors = pygame.sprite.Group()
chests = pygame.sprite.Group()
returns = pygame.sprite.Group()
floats = pygame.sprite.Group()
rings = pygame.sprite.Group()
swords = pygame.sprite.Group()
fdoors = pygame.sprite.Group()
special_timer = 0
special_ticker = 1
special_timer2 = 0
special_ticker2 = 1
global has_sk1
global has_sk2
global has_ring
global has_sword
global has_xcancel
global returned
font_color = WHITE
font = pygame.font.Font("res/misc/Bitmgothic.ttf", 24)
stage_names = load_names()
start_time = time()
ambient_fx = pygame.mixer.Sound("res/audio/background.ogg")
ambient_fx.set_volume(0.6)
title_fx = pygame.mixer.Sound("res/audio/title.wav")
title_fx.set_volume(0.8)
select_fx = pygame.mixer.Sound("res/audio/title2.wav")
select_fx.set_volume(0.8)
jump_fx = pygame.mixer.Sound("res/audio/jump.flac")
jump_fx.set_volume(0.2)
coin_fx = pygame.mixer.Sound("res/audio/coin.wav")
coin_fx.set_volume(0.4)
next_fx = pygame.mixer.Sound("res/audio/next.flac")
next_fx.set_volume(1.0)
death_fx = pygame.mixer.Sound("res/audio/death.wav")
death_fx.set_volume(0.4)
respawn_fx = pygame.mixer.Sound("res/audio/respawn.wav")
respawn_fx.set_volume(1.0)
chest_fx = pygame.mixer.Sound("res/audio/chest.wav")
chest_fx.set_volume(0.3)
skey1_fx = pygame.mixer.Sound("res/audio/specialkey1.wav")
skey1_fx.set_volume(0.3)
skey2_fx = pygame.mixer.Sound("res/audio/specialkey1.wav")
skey2_fx.set_volume(0.3)
sdoor_fx = pygame.mixer.Sound("res/audio/specialdoor.wav")
sdoor_fx.set_volume(0.2)
return_fx = pygame.mixer.Sound("res/audio/return.wav")
return_fx.set_volume(0.2)
open_fx = pygame.mixer.Sound("res/audio/open.wav")
open_fx.set_volume(0.2)
partial_fx = pygame.mixer.Sound("res/audio/partial.wav")
partial_fx.set_volume(0.2)
sbkgd1_fx = pygame.mixer.Sound("res/audio/sbkgd1.ogg")
sbkgd1_fx.set_volume(0.5)
sbkgd2_fx = pygame.mixer.Sound("res/audio/sbkgd2.ogg")
sbkgd2_fx.set_volume(0.6)
sbkgd3_fx = pygame.mixer.Sound("res/audio/sbkgd3.ogg")
sbkgd3_fx.set_volume(0.5)
ring_fx = pygame.mixer.Sound("res/audio/chest.wav") # PUT A DIFFERENT SOUND HERE
ring_fx.set_volume(0.3)
sword_fx = pygame.mixer.Sound("res/audio/sword.wav")
sword_fx.set_volume(0.3)
title_fx.play()
title = True
quit_from_title = False
while title:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Handle window exit gracefully
title = False
quit_from_title = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
title = False
if event.key == pygame.K_f:
if bigscreen:
bigscreen = False
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
pygame.HWSURFACE | pygame.DOUBLEBUF, vsync=1)
else:
bigscreen = True
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.SCALED, vsync=1)
if event.key == pygame.K_ESCAPE:
title = False
quit_from_title = True
title_image = pygame.image.load("res/img/title.bmp")
screen.blit(title_image, title_image.get_rect())
pygame.display.flip()
select_fx.play()
ambient_fx.play(-1)
GameOver = False
running = True
while running and not quit_from_title:
for event in pygame.event.get():
if event.type == pygame.QUIT: # Handle window exit gracefully
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP: # Begin jump logic process
player.jump(jump_fx)
if event.key == pygame.K_s:
pygame.image.save(screen, f"screenshot{screenshot_num}.jpeg")
screenshot_num += 1
if event.key == pygame.K_f:
if bigscreen:
bigscreen = False
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
pygame.HWSURFACE | pygame.DOUBLEBUF, vsync=1)
else:
bigscreen = True
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT),
pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.SCALED, vsync=1)
if event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP: # End jump logic process
player.cancel_jump()
if not stage_loaded: # Load stage if not loaded
spawn = load_stage(f"res/levels/level{current_stage}.txt",
TileArray, HazardArray, StageExitArray,
CollectibleArray, RespawnPointArray,
SpikeArray, SlowArray, DecorativeArray,
LockArray, LockLeafArray, KeyArray,
SpecialKeyArray, SecretDoorArray,
TombstoneArray, ChestArray, ReturnArray,
FloatArray, coins, current_stage, RingArray,
SwordArray, FinalDoorArray)
if spawn == vec(-1, -1):
end_time = time()
GameOver = True
running = False
else:
pygame.display.set_caption(f"Lymynal Labrynthe - {stage_names[str(current_stage)]}")
if current_stage == 13:
sbkgd2_fx.play()
sprites.empty() # Clear sprites, obstacles to make room for new stage
obstacles.empty()
for obj in TileArray: # Load new obstacles
obstacles.add(obj)
sprites.add(obj)
TileArray.clear() # Clear array used for loading obstacles for next cycle
hazards.empty() # Clear hazards
for obj in HazardArray: # Load new hazards
hazards.add(obj)
sprites.add(obj)
HazardArray.clear() # Clear array used for loading hazards for next cycle
stage_exit.empty() # Clear stage_exit
for obj in StageExitArray: # Load new stage_exit objects
stage_exit.add(obj)
sprites.add(obj)
StageExitArray.clear() # Clear array used for loading stage exit for next cycle
collectibles.empty() # Clear collectibles
for obj in CollectibleArray: # Load new collectibles
collectibles.add(obj)
sprites.add(obj)
CollectibleArray.clear() # Clear array used for loading collectibles for next cycle
respawn_point.empty()
if RespawnPointArray:
for obj in RespawnPointArray: # Load stage respawn point if one exists
respawn = vec(obj.rect.x + 8, obj.rect.y - 8)
respawn_point.add(obj)
sprites.add(obj)
RespawnPointArray.clear() # Clear array used for loading respawn points for next cycle
spikes.empty()
if SpikeArray:
for obj in SpikeArray:
spikes.add(obj)
sprites.add(obj)
SpikeArray.clear()
slow.empty()
if SlowArray:
for obj in SlowArray:
slow.add(obj)
sprites.add(obj)
SlowArray.clear()
decorative.empty()
if DecorativeArray:
for obj in DecorativeArray:
decorative.add(obj)
sprites.add(obj)
DecorativeArray.clear()
lockleafs.empty()
if LockArray:
for obj in LockArray:
lockleafs.add(obj)
hazards.add(obj)
sprites.add(obj)
LockArray.clear()
if LockLeafArray:
for obj in LockLeafArray:
lockleafs.add(obj)
hazards.add(obj)
sprites.add(obj)
LockLeafArray.clear()
keys.empty()
if KeyArray:
for obj in KeyArray:
keys.add(obj)
sprites.add(obj)
KeyArray.clear()
skeys.empty()
if SpecialKeyArray:
for obj in SpecialKeyArray:
skeys.add(obj)
sprites.add(obj)
SpecialKeyArray.clear()
sdoors.empty()
if SecretDoorArray:
for obj in SecretDoorArray:
sdoors.add(obj)
sprites.add(obj)
SecretDoorArray.clear()
if TombstoneArray:
for obj in TombstoneArray:
decorative.add(obj)
sprites.add(obj)
TombstoneArray.clear()
chests.empty()
if ChestArray:
for obj in ChestArray:
chests.add(obj)
sprites.add(obj)
ChestArray.clear()
returns.empty()
if ReturnArray:
for obj in ReturnArray:
returns.add(obj)
sprites.add(obj)
ReturnArray.clear()
floats.empty()
if FloatArray:
for obj in FloatArray:
floats.add(obj)
obstacles.add(obj)
sprites.add(obj)
FloatArray.clear()
rings.empty()
if RingArray:
for obj in RingArray:
rings.add(obj)
sprites.add(obj)
RingArray.clear()
swords.empty()
if SwordArray:
for obj in SwordArray:
swords.add(obj)
sprites.add(obj)
SwordArray.clear()
fdoors.empty()
if FinalDoorArray:
for obj in FinalDoorArray:
fdoors.add(obj)
sprites.add(obj)
FinalDoorArray.clear()
total_jumps += player.num_jumps
player.kill() # Remove current instance of player
player = Player(spawn.x, spawn.y) # Spawn new player at spawn location specified for new stage
sprites.add(player)
stage_loaded = True # Confirm loading of stage
pygame.event.pump() # Update current event log
if current_stage == 8: # Handle special stage bg and animation
special_image = pygame.image.load(f"res/img/special{special_ticker}.bmp")
special_timer += 1
if special_timer == 10:
special_timer = 0
special_ticker += 1
if special_ticker > 3: special_ticker = 1
screen.blit(special_image, special_image.get_rect())
elif current_stage == 17:
special_image2 = pygame.image.load(f"res/img/specialsecond{special_ticker}.bmp")
special_timer2 += 1
if special_timer2 == 10:
special_timer2 = 0
special_ticker2 += 1
if special_ticker2 > 3: special_ticker2 = 1
screen.blit(special_image2, special_image2.get_rect())
elif current_stage == 20:
special_image3 = pygame.image.load(f"res/img/win.bmp")