-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
1528 lines (1396 loc) · 65.9 KB
/
run.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, math, random, string
from scripts.pygamegame import PygameGame
from scripts.sketchedObjects import Stick, Bomb, Block
from scripts.charSprites import Field, CharSprites, RedBlob, Obstacles, BlueBlob, Dragon
size = width, height = 1024, 768
screen = pygame.display.set_mode(size) # my surface
allMonsters = pygame.sprite.Group()
allObstacles = pygame.sprite.Group()
blueBlobs = pygame.sprite.Group()
# for survival
monstersSpawned = pygame.sprite.Group()
obstaclesSpawned = pygame.sprite.Group()
# for stage editing
obstaclesAdded = pygame.sprite.Group()
monstersAdded = pygame.sprite.Group()
# for stage playing
allObstacles2 = pygame.sprite.Group()
allMonsters2 = pygame.sprite.Group()
black = 0, 0, 0
white = 255, 255, 255
yellow = 255, 255, 0
gravity = 8
pygame.mixer.init(48000, -16, 1, 1024)
# initializing sound, shamelessly stolen from stackoverflow
# please play with music!!
font = pygame.font.SysFont("arial", 36)
font2 = pygame.font.SysFont("arial", 72)
pygame.init()
def getDistance(point1, point2):
return ((point1[0] - point2[0])**2 + (point1[1] - point2[1])**2)**0.5
def isCircle(tempPoints):
# check for
if len(tempPoints) <= 10: # drawing circles make a lot more points
return [False]
count = 0
for i in range(len(tempPoints)):
for j in range(len(tempPoints)):
if abs(i - j) <= 10:
# each point compared must be at least 11 points apart
continue
elif abs(tempPoints[i][0] - tempPoints[j][0]) <= 40 \
and abs(tempPoints[i][1] - tempPoints[j][1]) <= 40:
count += 1
if count >= 6:
# at least 10 such points must be close to one another
return [True, tempPoints[i]]
return [False]
def isCorner(tempPoints):
# this function doesn't actually count the number of corners
# but if numCorner is high, it is likely that the
# shape drawn has many corners
numCorner = 0
for i in range(len(tempPoints)):
for j in range(len(tempPoints)):
if abs(i - j) >= 3 or (i - j) == 0:
# each point compared must be at most 3 points apart
continue
elif abs(tempPoints[i][0] - tempPoints[j][0]) <= 5:
# straight vertical line section of block
try:
if abs(tempPoints[i][0] - tempPoints[j + 3][0]) <= 20:
# corner
numCorner += 1
except:
pass
elif abs(tempPoints[i][1] - tempPoints[j][1]) <= 5:
try:
if abs(tempPoints[i][1] - tempPoints[j + 3][1]) <= 20:
# corner
numCorner += 1
except:
pass
return numCorner
def isBlock(tempPoints):
if len(tempPoints) < 20:
return [False]
if isCorner(tempPoints) < 40: # blocks have a lot of "numCorners"
return [False]
else:
# tempPoints[0], (tempPoints[0] + 100, tempPoints[1] + 300)]
return [True, tempPoints[0]]
def withinBounds(tempPoints):
for i in range(len(tempPoints)):
if tempPoints[i][1] > 600:
return False
return True
def saveStage():
f = open("states/saveState.txt", "w")
f.write("#obstacles\n")
for obstacle in obstaclesAdded:
f.write(str((obstacle.rect.left, obstacle.rect.top)) + "\n")
f.write("#blueblobs\n")
for monster in monstersAdded:
if isinstance(monster, BlueBlob):
f.write(str([(monster.rect.center[0], 555), monster.radius,
monster.bulletVelocity, "play stage"]) + "\n")
f.write("#redblobs\n")
for monster in monstersAdded:
if not isinstance(monster, BlueBlob):
f.write(str([(monster.rect.left, 499), monster.radius,
monster.velocityX, "play stage"]) + "\n")
f.close()
def overwrite(filename):
if filename == "states/highscore.txt":
f = open(filename, "w")
for i in range(4):
f.write("0\n")
f.close()
def convertToLst(s):
# used to convert the strings in .txt files back to list
lst = []
for stuff in s.split(","):
lst += [stuff]
item = lst[0] + "," + lst[1]
location = convertToTuple(item)
radius = int(lst[2])
velocity = int(lst[3])
stage = lst[4][:-2] # ignore the ] and \n
return [location, radius, velocity, stage]
def convertToTuple(s):
# used to convert strings in .txt files back into a tuple
lst = []
for stuff in s.split(","):
lst += [stuff]
item1 = lst[0]
item2 = lst[1]
newItem = ""
for letters in item1:
if letters in string.digits:
newItem += letters
item1 = newItem
if item2[-2:-1] == ")":
item2 = item2[:-1]
return int(item1), int(item2[1:-1])
class SketchyQuest(PygameGame):
def __init__(self):
# screen
self.title = "A Sketchy Quest"
self.screen = pygame.display.set_mode(size) # my surface
self.screenWidth, self.screenHeight = 1024, 768
self.fps = 50
self.timerCalls = 0
self.maxDistance = 0
self.music = 0
# stage
self.colour = black
self.tempPoints = [] # shows sketch
self.scrollX = 0
self.scrollSpeed = 0
self.rubble = Obstacles("sprites/rubble.png", (11000, 210))
allObstacles.add(self.rubble)
# main character
self.ground = 465
self.mainChar = CharSprites("sprites/main_character.png",
(21, self.ground))
# monsterA - can only move around
# stage 1
self.redblob1 = RedBlob("sprites/monster1.png",
(2000, 499), 600, -5, "stage1")
self.redblob2 = RedBlob("sprites/monster1.png",
(4000, 499), 600, 5, "stage1")
self.redblob3 = RedBlob("sprites/monster1.png",
(6000, 499), 600, 10, "stage1")
self.redblob4 = RedBlob("sprites/monster1.png",
(9000, 499), 600, 10, "stage1")
# stage 2
self.redblob5 = RedBlob("sprites/monster1.png",
(2000, 499), 600, -10, "stage2")
self.redblob6 = RedBlob("sprites/monster1.png",
(4000, 499), 600, 10, "stage2")
allMonsters.add(self.redblob1, self.redblob2,
self.redblob3, self.redblob4,
self.redblob5, self.redblob6)
# monsterB - can attack with projectiles
self.blueRight = pygame.image.load("sprites/monster2a.png")
# stage 1
self.blueblob1 = BlueBlob("sprites/monster2.png",
(5000, 499), 1500, 25, "stage1")
self.blueblob2 = BlueBlob("sprites/monster2.png",
(8000, 499), 2000, 30, "stage1")
self.blueblob3 = BlueBlob("sprites/monster2.png",
(9500, 499), 1500, 30, "stage1")
# stage 2
self.blueblob4 = BlueBlob("sprites/monster2.png",
(5000, 499), 1500, 30, "stage2")
self.blueblob5 = BlueBlob("sprites/monster2.png",
(7000, 499), 1500, 35, "stage2")
blueBlobs.add(self.blueblob1, self.blueblob2, self.blueblob3,
self.blueblob4, self.blueblob5)
allMonsters.add(self.blueblob1, self.blueblob2, self.blueblob3,
self.blueblob4, self.blueblob5)
# boss - can breathe fire
self.boss = Dragon("sprites/dragon.png", (550, 0), "boss1")
# image from mega man
# sticks
self.stickFallen = False # if stick has reached the ground
self.stickPoints = [] # actual stick drawn
self.stickLen = 0 # length of each stick
self.stick = Stick(self.stickPoints, self.stickLen, self.stickFallen)
# bombs
self.bomb = Bomb((0, 0), (0, 0), (0, 0), 1, 1) # initializing
self.exploded = False
self.explosionDelay = 0
self.explosionPoint = self.bomb.bombPoints
# blocks
self.block = Block((-1, -1), (-1, -1))
# states
self.gameState = "start" # starts at the title state
# title states
self.title1 = True
self.title2 = False
self.title21 = False # for the second screen after
self.title22 = False
self.title23 = False # back button
self.title3 = False
self.title31 = False
self.title32 = False
self.title33 = False # back button
self.titleScreen = pygame.image.load("stages/aSketchyQuestTitleScreen.png")
self.titleScreenRect = self.titleScreen.get_rect()
self.titleScreen2 = pygame.image.load("stages/aSketchyQuestTitleScreen2.png")
self.titleScreen2Rect = self.titleScreen.get_rect()
self.titleScreen21 = pygame.image.load("stages/aSketchyQuestTitleScreen21.png")
self.titleScreen21Rect = self.titleScreen.get_rect()
self.titleScreen22 = pygame.image.load("stages/aSketchyQuestTitleScreen22.png")
self.titleScreen22Rect = self.titleScreen.get_rect()
self.titleScreen23 = pygame.image.load("stages/aSketchyQuestTitleScreen23.png")
self.titleScreen23Rect = self.titleScreen.get_rect()
self.titleScreen3 = pygame.image.load("stages/aSketchyQuestTitleScreen3.png")
self.titleScreen3Rect = self.titleScreen.get_rect()
self.titleScreen31 = pygame.image.load("stages/aSketchyQuestTitleScreen31.png")
self.titleScreen31Rect = self.titleScreen.get_rect()
self.titleScreen32 = pygame.image.load("stages/aSketchyQuestTitleScreen32.png")
self.titleScreen32Rect = self.titleScreen.get_rect()
self.titleScreen33 = pygame.image.load("stages/aSketchyQuestTitleScreen33.png")
self.titleScreen33Rect = self.titleScreen.get_rect()
# game states
self.gameScreen = pygame.image.load("stages/stage1.png")
self.gameScreenRect = self.gameScreen.get_rect()
self.gameScreenLen = 12026
self.stage2Screen = pygame.image.load("stages/stage2.png")
self.stage2ScreenRect = self.gameScreenRect
self.bossScreen = pygame.image.load("stages/BossStage.png")
self.bossScreenRect = self.bossScreen.get_rect()
# credits
self.credits = False
self.creditsScreen = pygame.image.load("stages/credits.png")
self.creditsScreenRect = self.creditsScreen.get_rect()
# survival
self.survivalScreen = pygame.image.load("stages/survival.png")
self.survivalScreenRect = self.survivalScreen.get_rect()
self.dragon = Dragon("sprites/dragon.png", (2000, 0), "survival")
self.monstersKilled = 0
self.overlapCount = 0
self.redKills = 0
self.blueKills = 0
self.dragonKills = 0
self.healRadius = 8000
self.healZone = Field(8000, (self.gameScreenLen/2, 465))
# stage builder
self.stageBuildScreen = pygame.image.load("stages/stage_builder.png")
self.stageBuildScreenRect = self.stageBuildScreen.get_rect()
self.monster1 = pygame.image.load("sprites/monster1.png")
self.monster1pos = (20, 10)
self.monster1Held = False
self.monster2 = pygame.image.load("sprites/monster2.png")
self.monster2pos = (160, 10)
self.monster2Held = False
self.penDown = False
# game over state
self.gameOverScreen = pygame.image.load("stages/game_over.png")
self.gameOverScreenRect = self.gameOverScreen.get_rect()
# for resetting after game over
def reset(self):
allMonsters.empty()
allObstacles.empty()
self.__init__()
self.tempPoints = []
def mousePressed(self, x, y):
# no mousePressed is needed for title screen
if self.gameState != "start" and self.gameState != "high scores" and \
self.gameState != "edit stage":
# exit button
if self.gameState != "play stage":
if 9 <= x < 109 and 700 <= y < 750:
if self.gameState == "survival":
self.reset()
self.title1 = False
self.title21 = True
else:
self.reset()
return
# drawing
self.tempPoints += [(x, y)]
if self.gameState == "play stage":
# exit!
if 9 <= x < 109 and 700 <= y < 750:
allObstacles2.empty()
allMonsters2.empty()
self.title31 = True
self.gameState = "start"
self.music = 1
self.mainChar.rect.left = 21
self.mainChar.tempRect = 21
self.scrollX = 0
self.mainChar.health = 3
self.tempPoints = []
self.mainChar.equipped = False
self.mainChar.weapon = []
self.mainChar.weaponRight = []
self.mainChar.weaponLeft = []
self.bomb = Bomb((0, 0), (0, 0), (0, 0), 1, 1)
self.exploded = False
self.explosionDelay = 0
self.block = Block((-1, -1), (-1, -1))
elif self.gameState == "high scores":
if 890 <= x < 1005 and 695 <= y < 755:
overwrite("states/highscore.txt")
else:
self.title22 = True
self.gameState = "start"
# stage editor
elif self.gameState == "edit stage":
# print("Pen Down: ", self.penDown)
# print("Monster 2 Held: ", self.monster2Held)
# print("Monster 1 Held: ", self.monster1Held)
# red blob
if not self.monster2Held and 4 <= x < 131 and 2 <= y < 131:
self.monster1pos = (x, y)
self.monster1Held = True
# blue blob
elif not self.monster1Held and 145 <= x < 276 and 2 <= y < 131:
self.monster2pos = (x, y)
self.monster2Held = True
# save!
elif not self.penDown and not self.monster2Held and \
not self.monster1Held and 557 <= x < 773 and 11 <= y < 121:
saveStage()
# clear!
elif not self.penDown and not self.monster2Held and \
not self.monster1Held and 779 <= x < 995 and 11 <= y < 121:
obstaclesAdded.empty()
monstersAdded.empty()
# exit!
elif not self.monster2Held and \
not self.monster1Held and 9 <= x < 109 and 700 <= y < 750:
obstaclesAdded.empty()
monstersAdded.empty()
self.title32 = True
self.gameState = "start"
self.music = 1
self.mainChar.rect.left = 21
self.scrollX = 0
else:
if not self.monster1Held and not self.monster2Held:
self.tempPoints += [(x, y)]
self.penDown = True
if self.monster1Held:
self.monster1pos = (x - 45, y - 45)
elif self.monster2Held:
self.monster2pos = (x - 45, y - 45)
def mouseReleased(self):
if self.gameState == "stage1" or \
self.gameState == "stage2" or self.gameState == "boss1" \
or self.gameState == "survival" or \
self.gameState == "play stage":
# print("Length: ", len(self.tempPoints)) for debugging
# print("Corners: ", isCorner(self.tempPoints))
if withinBounds(self.tempPoints):
try:
# blocks
if isBlock(self.tempPoints)[0]:
points = isBlock(self.tempPoints)[1]
self.block = Block(points,
(points[0] + self.scrollX,
points[1]))
if self.block.rect.top + self.block.height >= 610:
self.block = Block((-1, -1), (-1, -1))
self.tempPoints = []
else:
self.block.colour = self.colour
self.block.timer = 150
self.block.destroyed = False
self.tempPoints = []
# bombs
elif isCircle(self.tempPoints)[0]:
# isCircle returns a list of truth value and a point
centreX = isCircle(self.tempPoints)[1][0]
centreY = isCircle(self.tempPoints)[1][1]
bombPoints = (centreX, centreY)
radius = 40
explosionRadius = 200
self.tempPoints = []
self.bomb = Bomb((centreX - radius, centreY - radius),
bombPoints,
(bombPoints[0] + self.scrollX,
bombPoints[1]),
radius, explosionRadius)
self.bomb.colour = self.colour
self.bomb.fallen = False
self.bomb.fuse = 0
# sticks
else:
# adding a permanent stick
self.stickPoints = [self.tempPoints[0],
self.tempPoints[-1]]
self.stickLen = getDistance(self.stickPoints[0],
self.stickPoints[1])
self.tempPoints = [] # removing sketches
self.stickFallen = False
self.stick = Stick(self.stickPoints,
self.stickLen, self.stickFallen)
self.stick.colour = self.colour
if self.mainChar.equipped:
self.mainChar.equipped = False
self.mainChar.weapon = []
self.mainChar.weaponLeft = []
self.mainChar.weaponRight = []
except:
pass
else:
self.tempPoints = []
elif self.gameState == "edit stage":
self.penDown = False
if not self.monster1Held and not self.monster2Held and \
len(self.tempPoints) >= 40 \
and self.tempPoints[0][1] <= 250:
rubble = Obstacles("sprites/rubble.png",
(self.tempPoints[0][0]
- 300 + self.scrollX, # to offset rect.left
self.tempPoints[0][1]))
obstaclesAdded.add(rubble)
self.tempPoints = []
else:
self.tempPoints = []
if self.monster1Held:
self.monster1Held = False
if 4 <= self.monster1pos[0] < 131 and \
2 <= self.monster1pos[1] < 131:
self.monster1pos = (20, 10)
else:
radius = random.randint(200,
int(self.gameScreenLen / 3))
velocity = random.randint(-10, 10)
try:
monster1 = RedBlob("sprites/monster1.png", (self.monster1pos[0]
+ self.scrollX,
self.monster1pos[1]
+ 10),
radius, velocity, "edit stage")
monstersAdded.add(monster1)
self.monster1pos = (20, 10)
except:
pass
elif self.monster2Held:
self.monster2Held = False
if 145 <= self.monster2pos[0] < 276 \
and 2 <= self.monster2pos[1] < 131:
self.monster2pos = (160, 10)
else:
radius = random.randint(200,
int(self.gameScreenLen / 4))
try:
monster2 = BlueBlob("sprites/monster2.png", (self.monster2pos[0]
+ 45
+ self.scrollX,
self.monster2pos[1]
+ 10),
radius, 20, "edit stage")
monstersAdded.add(monster2)
except:
pass
self.monster2pos = (160, 10)
def keyPressed(self, currKey):
# for title screen
if self.gameState == "start":
if self.title1:
if currKey == "return":
self.gameState = "stage1"
self.title1 = False
self.title2 = False
self.title3 = False
elif currKey == "s" or currKey == "down":
self.title1 = False
self.title2 = True
self.title3 = False
elif currKey == "w" or currKey == "up":
self.title1 = False
self.title2 = False
self.title3 = True
elif self.title2:
if currKey == "return":
self.title1 = False
self.title2 = False
self.title21 = True
self.title3 = False
elif currKey == "w" or currKey == "up":
self.title2 = False
self.title1 = True
elif currKey == "s" or currKey == "down":
self.title2 = False
self.title3 = True
elif self.title21:
if currKey == "return":
self.gameState = "survival"
self.mainChar.health = 10 # you get 10 health for survival
self.title21 = False
self.title22 = False
self.title23 = False
elif currKey == "s" or currKey == "down":
self.title21 = False
self.title22 = True
self.title23 = False
elif self.title22:
if currKey == "return":
self.gameState = "high scores"
self.title22 = False
self.title21 = False
self.title23 = False
elif currKey == "w" or currKey == "up":
self.title22 = False
self.title21 = True
self.title23 = False
elif currKey == "s" or currKey == "down":
self.title21 = False
self.title22 = False
self.title23 = True
elif self.title23:
if currKey == "return":
self.title21 = False
self.title22 = False
self.title23 = False
self.title2 = True
elif currKey == "w" or currKey == "up":
self.title21 = False
self.title22 = True
self.title23 = False
elif self.title3:
if currKey == "return":
self.title3 = False
self.title31 = True
self.title32 = False
self.title33 = False
elif currKey == "w" or currKey == "up":
self.title3 = False
self.title2 = True
self.title1 = False
elif currKey == "s" or currKey == "down":
self.title1 = True
self.title2 = False
self.title3 = False
elif self.title31:
if currKey == "return":
self.gameState = "play stage"
self.mainChar.stage = "play stage"
self.title31 = False
elif currKey == "s" or currKey == "down":
self.title31 = False
self.title32 = True
self.title33 = False
elif self.title32:
if currKey == "return":
self.gameState = "edit stage"
self.title32 = False
elif currKey == "w" or currKey == "up":
self.title31 = True
self.title32 = False
self.title33 = False
elif currKey == "s" or currKey == "down":
self.title31 = False
self.title32 = False
self.title33 = True
elif self.title33:
if currKey == "return":
self.title33 = False
self.title3 = True
elif currKey == "w" or currKey == "up":
self.title31 = False
self.title32 = True
self.title33 = False
elif self.gameState == "high scores":
if currKey == "return":
self.gameState = "start"
self.title22 = True
elif self.gameState == "Game Over" or self.gameState == "dead":
if currKey == "r":
self.reset()
# for the game screen
elif self.gameState != "title" and self.gameState != "high scores" \
and self.gameState != "edit stage":
# move left and right has been implemented in the run function
if currKey == "d":
self.mainChar.weapon = self.mainChar.weaponRight
elif currKey == "a":
self.mainChar.weapon = self.mainChar.weaponLeft
elif currKey == "space" or currKey == "left shift":
self.mainChar.jump()
elif not self.mainChar.jumpState and currKey == "w": # equip sticks
# can only equip if near and stick has fallen
if len(self.stick.stickPoints) == 2 and \
abs(self.mainChar.tempRect - self.stick.stickPoints[0][0])\
<= 200 and self.stick.fallen:
self.mainChar.equip(self.stick)
self.stick = Stick([], 0, False)
elif currKey == "q":
# cheat code
self.mainChar.health = 300
print(self.boss.health)
print(self.mainChar.rect.left)
# equipped state
if self.mainChar.equipped:
if not self.mainChar.attackState and currKey == "s": # attack
self.mainChar.attackState = True
self.mainChar.angle = 0
self.mainChar.weaponDown = False
if self.gameState != "survival" and \
self.gameState != "play stage":
self.mainChar.attack(allMonsters, self.boss)
elif self.gameState == "play stage":
self.mainChar.attack(allMonsters2, self.boss)
else:
self.mainChar.attack(monstersSpawned, self.dragon)
if self.dragon.health == 0:
self.mainChar.health += 5
self.monstersKilled += 1
self.dragonKills += 1
if self.dragonKills <= 4:
self.healRadius -= 2000
self.healZone = Field(self.healRadius,
(self.gameScreenLen / 2,
465))
else:
self.healRadius = 0
self.healZone = Field(0, (0, 0))
self.dragon.health = 5
self.dragon.alive = False
def loading(self, TID):
# not currently used...
self.screen.blit(self.titleScreen33, self.titleScreen33Rect)
time.sleep(2)
while (TID.is_alive()):
pass
return
def timerFired(self, dt):
if self.gameState == "stage1" or \
self.gameState == "stage2" or self.gameState == "boss1" or \
self.gameState == "survival" or self.gameState == "play stage":
self.timerCalls += 1
# bombs
if self.bomb.fallen:
self.bomb.fuse += 1 # start counting after bomb has fallen
if self.bomb.fuse % 50 == 0:
self.explosionPoint = (self.bomb.bombPoints[0] - 200,
self.bomb.bombPoints[1] - 200)
if self.gameState == "survival":
self.bomb.explode(monstersSpawned,
obstaclesSpawned,
self.mainChar, self.dragon)
self.exploded = True
elif self.gameState == "play stage":
self.bomb.explode(allMonsters2,
allObstacles2,
self.mainChar, self.boss)
self.exploded = True
else:
self.bomb.explode(allMonsters,
allObstacles,
self.mainChar, self.boss)
self.exploded = True
if self.gameState == "stage1" or \
self.gameState == "stage2" or self.gameState == "boss1":
for monster in allMonsters:
if monster.stage == self.gameState:
monster.update()
for dudes in blueBlobs:
if dudes.alive:
dudes.fire(self.mainChar) # sends out the projectiles
if self.gameState != "survival" and self.gameState != "play stage":
self.mainChar.update(allMonsters, self.boss.fire,
self.boss, allObstacles,
self.block, self.gameState)
if self.exploded:
self.explosionDelay += 1
if self.explosionDelay % 20 == 0:
self.exploded = False
self.explosionDelay = 0
self.block.time()
if self.gameState == "boss1":
self.boss.fly()
self.boss.attack(0)
if self.boss.attacking:
self.boss.fire.update(self.block,
self.mainChar, self.gameState)
if self.credits:
self.mainChar.creditsDelay -= 1
# survival mode
if self.gameState == "survival":
self.mainChar.update(monstersSpawned, self.dragon.fire,
self.dragon, obstaclesSpawned,
self.block, self.gameState)
# updating monsters
for monster in monstersSpawned:
if monster.alive:
monster.update()
if isinstance(monster, BlueBlob):
monster.fire(self.mainChar)
# spawns red blobs
if not self.dragon.alive and self.timerCalls % 150 == 0:
location = random.randint(200, self.gameScreenLen - 200)
if abs(location - self.mainChar.rect.left) <= 200:
if location + 1000 >= self.gameScreenLen:
location = \
random.randint(0, self.mainChar.rect.left - 200)
else:
location = \
random.randint(self.mainChar.rect.left + 200,
self.gameScreenLen - 200)
radius = random.randint(200,
int(self.gameScreenLen / 3))
tempV = 5 + int(0.4 * self.monstersKilled)
if tempV >= self.mainChar.velocityX:
tempV = self.mainChar.velocityX - 1
velocity = random.choice([str(-tempV), str(tempV)])
monster = RedBlob("sprites/monster1.png", (location, 499), radius,
int(velocity), "survival")
monstersSpawned.add(monster)
# spawns blue blobs
if not self.dragon.alive and \
self.timerCalls % 200 == 0 and \
self.monstersKilled >= 10:
location = random.randint(200, self.gameScreenLen - 200)
if abs(location - self.mainChar.rect.left) <= 200:
if location + 1000 >= self.gameScreenLen:
location = \
random.randint(0, self.mainChar.rect.left - 200)
else:
location = \
random.randint(self.mainChar.rect.left + 200,
self.gameScreenLen - 200)
radius = 1500 + int(10 * self.monstersKilled)
velocity = 20 + int(0.4 * self.monstersKilled)
monster2 = BlueBlob("sprites/monster2.png", (location, 499), radius,
int(velocity), "survival")
monstersSpawned.add(monster2)
# spawn dragon boss!
if self.monstersKilled >= 20 and self.monstersKilled % 10 == 0 \
and not self.dragon.alive:
monstersSpawned.empty()
self.dragon.alive = True
location = self.mainChar.rect.left + 400
if location >= self.gameScreenLen:
location = self.mainChar.rect.left - 400
self.dragon = Dragon("sprites/dragon.png", (location, 0),
self.gameState)
else:
self.dragon = Dragon("sprites/dragon_right.png", (location, 0),
self.gameState)
if self.dragon.alive:
self.dragon.fly()
self.dragon.attack(self.scrollX)
if self.dragon.attacking:
self.dragon.fire.update(self.block,
self.mainChar,
self.gameState)
if self.timerCalls % 700 == 0 and \
pygame.sprite.collide_rect(self.healZone,
self.mainChar):
if self.mainChar.health < 10:
self.mainChar.health += 1
# play stage
elif self.gameState == "play stage":
self.mainChar.update(allMonsters2, self.dragon.fire,
self.dragon, allObstacles2,
self.block, self.gameState)
# updating monsters
for monster in allMonsters2:
if monster.alive and monster.rect.top == 499:
monster.update()
if isinstance(monster, BlueBlob):
monster.tempCenterY = 499
monster.fire(self.mainChar)
def redrawAll(self, screen):
# title screen
if self.title1:
self.screen.blit(self.titleScreen, self.titleScreenRect)
elif self.title2:
self.screen.blit(self.titleScreen2, self.titleScreen2Rect)
elif self.title21:
self.screen.blit(self.titleScreen21, self.titleScreen21Rect)
elif self.title22:
self.screen.blit(self.titleScreen22, self.titleScreen22Rect)
elif self.title23:
self.screen.blit(self.titleScreen23, self.titleScreen23Rect)
elif self.title3:
self.screen.blit(self.titleScreen3, self.titleScreen3Rect)
elif self.title31:
self.screen.blit(self.titleScreen31, self.titleScreen31Rect)
elif self.title32:
self.screen.blit(self.titleScreen32, self.titleScreen32Rect)
elif self.title33:
self.screen.blit(self.titleScreen33, self.titleScreen33Rect)
# music
if self.gameState == "start":
if self.music == 0:
# pygame.mixer.music.load("titleTheme.wav")
# from wii sports resort
# pygame.mixer.music.play(-1)
self.music = 1
# game screen
elif self.gameState == "stage1" or \
self.gameState == "stage2" or self.gameState == "boss1":
# music
if self.gameState == "stage1" and self.music == 1:
self.mainChar.stage = "stage1"
# pygame.mixer.music.load("kirby.wav") # from kirby
# pygame.mixer.music.play(-1)
self.music = 2
# music + init again
if self.gameState == "stage2" and self.music == 2:
# main character
self.mainChar.stage = "stage2"
self.mainChar.rect.left = 100
self.mainChar.tempRect = 100
self.mainChar.equipped = False
self.mainChar.weapon = []
self.mainChar.weaponRight = []
self.mainChar.weaponLeft = []
self.bomb = Bomb((0, 0), (0, 0), (0, 0), 1, 1)
self.exploded = False
self.explosionDelay = 0
self.block = Block((-1, -1), (-1, -1))
# monsters
for monster in allMonsters:
if isinstance(monster, BlueBlob):
monster.bullets.empty()
# other stuff
self.colour = white
self.scrollX = 0
# pygame.mixer.music.load("stage2.wav") # from final fantasy VI
# pygame.mixer.music.play(-1)
self.music = 3
if self.gameState == "boss1" and self.music == 3:
self.mainChar.stage = "boss1"
self.mainChar.rect.left = 100
self.mainChar.tempRect = 100
self.mainChar.equipped = False
self.mainChar.weapon = []
self.mainChar.weaponRight = []
self.mainChar.weaponLeft = []
self.bomb = Bomb((0, 0), (0, 0), (0, 0), 1, 1)
self.exploded = False
self.explosionDelay = 0
self.block = Block((-1, -1), (-1, -1))
self.colour = black
self.scrollX = 0
# pygame.mixer.music.load("Boss Theme.wav")
# from final fantasy VII
# pygame.mixer.music.play(-1)
self.music = 4
if self.gameState == "stage1":
pygame.Surface.blit(self.screen,
self.gameScreen, (0 - self.scrollX, 0))
elif self.gameState == "stage2":
pygame.Surface.blit(self.screen,
self.stage2Screen, (0 - self.scrollX, 0))
elif self.gameState == "boss1":
pygame.Surface.blit(self.screen,
self.bossScreen, self.bossScreenRect)
# obstacles
for obstacle in allObstacles:
if not obstacle.destroyed:
screen.blit(obstacle.image,
(obstacle.rect.left - self.scrollX,
obstacle.rect.top))
# monsters
# monsterA
for monster in allMonsters:
if monster.stage == self.gameState:
if monster.alive and not isinstance(monster, BlueBlob):
monster.checkCollision(self.block)
screen.blit(monster.image,
(monster.rect.left - self.scrollX,
monster.locationY))
elif monster.alive and isinstance(monster, BlueBlob):
if monster.right and not monster.left:
screen.blit(self.blueRight,
(monster.rect.left - self.scrollX,
monster.locationY))
else:
screen.blit(monster.image,
(monster.rect.left - self.scrollX,
monster.locationY))
if isinstance(monster, BlueBlob) and \
len(monster.bullets) >= 1:
for bullet in monster.bullets:
bullet.gameState = self.gameState
bullet.update()
screen.blit(bullet.image,
(bullet.rect.left - self.scrollX,
bullet.rect.top))
try:
if bullet.outOfBounds:
monster.bullets.remove(bullet)
except:
pass
# for final boss only
if not self.credits and self.gameState == "boss1":
self.boss.update()
if self.boss.alive and not self.boss.attacking:
screen.blit(self.boss.image, self.boss.rect)
elif self.boss.alive and self.boss.attacking:
self.boss.fire.draw()
screen.blit(self.boss.attackStance, self.boss.rect)
elif not self.boss.alive and self.music == 4:
self.music = 6
# pygame.mixer.music.load("victory!.wav")
# from Dragon Quest VIII
# pygame.mixer.music.play(1)
self.credits = True
# sketches