-
Notifications
You must be signed in to change notification settings - Fork 0
/
hollowKnight.py
1230 lines (1096 loc) · 49.5 KB
/
hollowKnight.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
# Citations:
# [1]
# Stack Overflow, How to detect when rotated rectangles are colliding each other
# https://stackoverflow.com/questions/62028169/how-to-detect-when-rotated-rectangles-are-colliding-each-other
# I did not use any code from this source; I only used the visual pictures to understand the theory for the separating axis algorithm
# the checkAttackColliding method in the Entity class is based on this algorithm
# [2]
# CS Academy Class Notes F22 Part 4, side scrolling
# https://www.cs.cmu.edu/~112-f22/notes/notes-animations-part4.html
# In my makePlayerVisible function, I directly used code with modifications from part 7, example sidescroller 2
# [3]
# Lecture demos 11/21 on sprite strips
# https://piazza.com/class/lkq6ivek5cg1bc/post/2231
# I created and animated all the sprites in my game using the code on sprite strips in the demos
# Things that I did myself: cropping sprites, using mirror, flip, and size methods in PIL, which I learned through the PIL official documentation
# [3]
# Sprites from the game Hollow Knight by Team Cherry, directly taken from Steam game files
# Includes: player sprites, terrain sprites, health bar sprites, upgrade area and instructions
# [4]
# Background Clouds from Twitter: https://pbs.twimg.com/media/Dv340GcWsAMt33a?format=jpg&name=4096x4096
# [5]
# The Spriter's Resource
# Enemy sprite (crawlid): https://www.spriters-resource.com/pc_computer/hollowknight/sheet/131852/
# Enemy sprite (charger): https://www.spriters-resource.com/pc_computer/hollowknight/sheet/131848
# Enemy sprite (flying): https://www.spriters-resource.com/pc_computer/hollowknight/sheet/133898/
# Enemy sprite (ghost and fireball): https://www.spriters-resource.com/pc_computer/hollowknight/sheet/133898/
# Attack sprites (fire): https://www.spriters-resource.com/pc_computer/rpgmakervxace/sheet/100134
# How to run: use ctrl+b on this file, with the other .py files in the same folder
from random import *
from cmu_graphics import *
from Player import *
from Terrain import *
from Enemy import *
from powerUp import *
from PIL import Image, ImageOps
def onAppStart(app):
app.scrollX = 0
app.scrollMargin = 400
app.stepsPerSecond = 60
app.generalAttackCounter = 0
app.previousAttackTime = 0
app.initialAttackCounter = 0
app.generalCounter = 0
app.generalFreezeCounter = 0
app.initialFreezeCounter = 0
app.generalInvincibleCounter = 0
app.initialInvincibleCounter = 0
app.generalFallingCounter = 0
app.initialFallingCounter = 0
app.generalDashingCounter = 0
app.initialDashingCounter = 0
app.generalFireballCounter = 0
app.initialFireballCounter = 0
app.spriteCounterDash = 0
app.spriteCounterMove = 0
app.spriteCounterEnemy = 0
app.spriteCounterCharger = 0
app.spriteCounterFly = 0
app.spriteCounterFireball = 0
app.stepCounter1 = 0
app.stepCounter2 = 0
app.stepCounter3 = 0
app.stepCounter4 = 0
app.stepCounter5 = 0
app.stepCounter6 = 0
app.stepCounter7 = 0
app.hazardLimit = 700
app.upgradePositions = [(1700, 300), (5000, 300)]
app.upgradeStates = [True, True]
app.powerUpRadius = 100
app.gameStarted = False
app.arrowsUp = True
app.displayTutorial = False
app.polygonX = 475
app.polygonY = 380
app.polygonWidth = 25
app.polygonHeight = 10
app.polygonXdistance = 275
app.polygonX2 = 485
app.polygonY2 = 420
app.polygonXdistance2 = 255
app.staticInstructionx = 350
app.staticInstructiony = 300
app.opacityFadeOut = 0.2 # controls how far it takes for the instructions to fade out
app.backgroundParallax = 5 # how much main game moves relative to background
app.ovalAdjustment = 20
app.dashAdjustment = 18
app.enemyAdjustment = -5
app.attackAdjustment = -150
app.enemyAdjustmentY = -5
app.enemyChargerAdjustmentY = 30
app.terrainAdjustmentY = -2
app.dashOpacity = 20
app.resourceX = 100
app.resourceWidth = 159
app.resourceY = 170
app.resourceHeight = 20
app.threshold = 3
app.respawnUpDistance = 500
createPlayerIdleSprites(app)
createPlayerMovingSprites(app)
createPlayerDashingSprites(app)
createPlayerDashingSpritesFinal(app)
createTerrainSprites(app)
createBackgroundSprites(app)
createEnemySprites(app)
createAttackSprites(app)
createHealthSprites(app)
createInstructionSprites(app)
createUpgradeSprites(app)
createMainMenuSprites(app)
player = Player(350, 0, 40, 50)
flat1 = Terrain(450, 480, 521, 50, 'Rectangle', 'Long') # width 521, height 50
flat2 = Terrain(0, 450, 521, 50, 'Rectangle', 'Long')
flat3 = Terrain(900, 450, 521, 50, 'Rectangle', 'Long')
flat4 = Terrain(1400, 450, 521, 50, 'Rectangle', 'Long')
flat4 = Terrain(1500, 450, 521, 50, 'Rectangle', 'Long')
flat5 = Terrain(2100, 430, 90, 90, 'Rectangle', 'Square') # width 90, height 90
flat6 = Terrain(2300, 460, 90, 90, 'Rectangle', 'Square')
flat7 = Terrain(2500, 450, 90, 90, 'Rectangle', 'Square')
flat8 = Terrain(2700, 420, 90, 90, 'Rectangle', 'Square')
flat9 = Terrain(2900, 450, 521, 50, 'Rectangle', 'Long')
flat10 = Terrain(3300, 450, 521, 50, 'Rectangle', 'Long')
flat11 = Terrain(3700, 450, 521, 50, 'Rectangle', 'Long')
flat12 = Terrain(2900, 370, 90, 90, 'Rectangle', 'Square')
flat13 = Terrain(4000, 370, 90, 90, 'Rectangle', 'Square')
flat14 = Terrain(4000, 450, 521, 50, 'Rectangle', 'Long')
flat15 = Terrain(4500, 450, 521, 50, 'Rectangle', 'Long')
flat16 = Terrain(5100, 430, 90, 90, 'Rectangle', 'Square')
flat17 = Terrain(5300, 460, 90, 90, 'Rectangle', 'Square')
flat18 = Terrain(5500, 450, 90, 90, 'Rectangle', 'Square')
flat19 = Terrain(5700, 420, 90, 90, 'Rectangle', 'Square')
flat20 = Terrain(5900, 450, 521, 50, 'Rectangle', 'Long')
flat21 = Terrain(6400, 450, 521, 50, 'Rectangle', 'Long')
oval1 = Terrain(1500, 500, 300, 150, 'outerOval', 'Oval')
oval2 = Terrain(1300, 550, 300, 150, 'outerOval', 'Oval')
oval3 = Terrain(1700, 550, 300, 150, 'outerOval', 'Oval')
crawlid1 = GroundEnemy(700, -300, 50, 30, 50, 'crawlid', None)
crawlid2 = GroundEnemy(800, -300, 50, 30, 50, 'crawlid', None)
crawlid3 = GroundEnemy(1500, -300, 50, 30, 50, 'crawlid', None)
charger1 = GroundEnemy(3500, 100, 60, 74, 50, 'charger', None)
fly1 = FlyEnemy(1000, -200, 50, 60, 50, 'fly', None)
fly2 = FlyEnemy(5500, -200, 50, 60, 50, 'fly', None)
fly3 = FlyEnemy(5700, -200, 50, 60, 50, 'fly', None)
ghost1 = FlyEnemy(4000, 200, 100, 100, 50, 'ghost', None)
terrainsList = [flat1, flat2, flat3, flat4, flat5, flat6, flat7, flat8, flat9, flat10,
flat11, flat12, flat13, flat14, flat15, flat16, flat17, flat18, flat19, flat20,
flat21,
oval2, oval3, oval1]
enemyList = [crawlid1, crawlid2, crawlid3, charger1, fly1, fly2, fly3, ghost1]
# crawlid: basic ground enemy that moves left and right, reverses when hits a wall
# charger: ground enemy that charges towards player when in line of sight
# fly: basic flying enemy that follows the player within a certain radius, decelerates as it gets closer
# ghost: hitting it causes it to randomly teleport, shoots fireballs that follows player in a line
def redrawAll(app):
player.getPlayerVertices()
drawBackground(app)
drawInstructions(app)
sideScroll(app)
drawTerrain(app)
drawEnemies(app)
drawTestVectors(app)
drawPlayer(app)
recordPreviousPositions(app)
drawAttacks(app)
drawTestVertices(app)
drawHealth(app)
drawUpgradeAreas(app)
if app.gameStarted == False:
drawMainMenu(app)
# functions that start with "create": imports the sprites into a list
# functions that start with "draw": draws the sprites
def drawMainMenu(app):
sprite = app.mainMenuSprite[0]
drawImage(sprite, 0, 0)
if app.arrowsUp == True:
drawPolygon(app.polygonX, app.polygonY, app.polygonX, app.polygonY+app.polygonHeight*2,
app.polygonX+app.polygonWidth, app.polygonY+app.polygonHeight, fill = 'white')
drawPolygon(app.polygonX+app.polygonXdistance, app.polygonY, app.polygonX+app.polygonXdistance, app.polygonY+app.polygonHeight*2,
app.polygonX+app.polygonXdistance-app.polygonWidth, app.polygonY+app.polygonHeight, fill = 'white')
if app.arrowsUp == False:
drawPolygon(app.polygonX2, app.polygonY2, app.polygonX2, app.polygonY2+app.polygonHeight*2,
app.polygonX2+app.polygonWidth, app.polygonY2+app.polygonHeight, fill = 'white')
drawPolygon(app.polygonX2+app.polygonXdistance2, app.polygonY2, app.polygonX2+app.polygonXdistance2, app.polygonY2+app.polygonHeight*2,
app.polygonX2+app.polygonXdistance2-app.polygonWidth, app.polygonY2+app.polygonHeight, fill = 'white')
if app.displayTutorial == True:
sprite = app.mainMenuSprite[1]
drawImage(sprite, 0, 0)
if player.isKilled == True:
sprite = app.mainMenuSprite[2]
drawImage(sprite, 0, 0)
def drawInstructions(app):
for i in range(len(app.upgradePositions)):
x, y = app.upgradePositions[i]
if app.upgradeStates[i] == False:
drawStaticInstructions(app, x, y, app.instructionSprites[i+1])
drawStaticInstructions(app, app.staticInstructionx-player.totalScrollX, app.staticInstructiony, app.instructionSprites[0])
def drawStaticInstructions(app, x, y, sprite):
opacityFirst = 100-abs((player.x-(x)))*app.opacityFadeOut
if opacityFirst > 100:
opacityFirst = 100
elif opacityFirst < 0:
opacityFirst = 0
drawImage(sprite, x, y, opacity = opacityFirst, align='center')
def drawBackground(app):
sprite = app.backgroundSprites[0]
drawImage(sprite, player.backgroundX, 0)
def sideScroll(app): #
if player.moving or player.dashing:
for enemy in enemyList:
enemy.x -= app.scrollX
for terrain in terrainsList:
terrain.x -= app.scrollX
player.x -= app.scrollX
player.totalScrollX += app.scrollX
if player.totalScrollX > 0:
player.backgroundX -= app.scrollX/app.backgroundParallax
for i in range(len(app.upgradePositions)):
x, y = app.upgradePositions[i]
x -= app.scrollX
app.upgradePositions[i] = x, y
def makePlayerVisible(app):
player.getPlayerVertices()
if (player.middleX < app.scrollX + app.scrollMargin):
app.scrollX = player.middleX - app.scrollMargin
return True
elif (player.middleX > app.scrollX + app.width - app.scrollMargin):
app.scrollX = player.middleX - (app.width - app.scrollMargin)
return True
else:
app.scrollX = 0
return False
def drawTestVertices(app):
if player.test == True and player.isAttacking == True:
for (x, y) in player.cornersAttack:
drawCircle(x, y, 2)
for (x, y) in player.cornersEnemy:
drawCircle(x, y, 2)
for (x, y) in player.fourPointsAttack1:
drawCircle(x, y, 2, fill='white')
for (x, y) in player.fourPointsAttack2:
drawCircle(x, y, 2, fill='white')
for (x, y) in player.fourPointsEnemy1:
drawCircle(x, y, 2, fill = 'white')
for (x, y) in player.fourPointsEnemy2:
drawCircle(x, y, 2, fill = 'white')
for (x, y) in player.twoPointsAttack1:
drawCircle(x, y, 2, fill='yellow')
for (x, y) in player.twoPointsAttack2:
drawCircle(x, y, 2, fill='yellow')
for (x, y) in player.twoPointsEnemy1:
drawCircle(x, y, 2, fill = 'blue')
for (x, y) in player.twoPointsEnemy2:
drawCircle(x, y, 2, fill = 'blue')
def drawTestVectors(app):
if player.test == True and player.isAttacking == True:
for vector in [player.vectorAttackLeftX,
player.vectorAttackRightX,
player.vectorAttackRightY,
player.vectorAttackLeftY,
player.vectorEnemyLeftX,
player.vectorEnemyRightX,
player.vectorEnemyLeftY,
player.vectorEnemyRightY]:
(x, y, x1, y2) = vector.draw()
drawLine(x, y, x1, y2)
(x, y, x1, y2) = player.vectorAttackX.draw()
drawLine(x, y, x1, y2, fill = 'red')
(x, y, x1, y2) = player.vectorAttackY.draw()
drawLine(x, y, x1, y2, fill = 'red')
(x, y, x1, y2) = player.vectorEnemyX.draw()
drawLine(x, y, x1, y2, fill = 'red')
(x, y, x1, y2) = player.vectorEnemyY.draw()
drawLine(x, y, x1, y2, fill = 'red')
def drawHealth(app):
for i in range(len(player.healthList)):
if player.healthList[i] == True:
sprite = app.healthSprites[0]
drawImage(sprite, player.healthX+player.healthXInterval*i, player.healthY)
# drawCircle(player.healthX+player.healthXInterval*i, player.healthY, player.healthRadius, fill=player.yesHealthColor)
else:
sprite = app.healthSprites[1]
drawImage(sprite, player.healthX+player.healthXInterval*i, player.healthY)
if player.resource > 0:
if player.resource >= player.resourceMax/2:
color1 = 'white'
else:
color1 = 'grey'
drawRect(app.resourceX, app.resourceY,
int(app.resourceWidth*(player.resource/(player.resourceMax/2))), app.resourceHeight, fill=color1)
if player.resource > player.resourceMax/2:
if player.resource == player.resourceMax:
color2 = 'white'
else:
color2 = 'grey'
drawRect(app.resourceX+app.resourceWidth, app.resourceY,
int(app.resourceWidth*((player.resource-(player.resourceMax/2))/(player.resourceMax/2))), app.resourceHeight, fill=color2)
drawRect(app.resourceX, app.resourceY, app.resourceWidth, app.resourceHeight, fill=None, border='black')
drawRect(app.resourceX+app.resourceWidth, app.resourceY, app.resourceWidth, app.resourceHeight, fill=None, border='black')
def drawPlayer(app):
if player.dashing == True:
player.dashingPositions.append((player.x, player.y, player.rotateAngle))
elif player.dashing == False:
player.dashingPositions = []
# if player.dashing == False:
# if player.direction == 'left':
# sprite = app.idleSprites[1]
# elif player.direction == 'right':
# sprite = app.idleSprites[0]
for (x, y, angle) in player.dashingPositions:
if player.direction == 'left':
sprite = app.dashSpritesFlipped[app.spriteCounterDash]
drawImage(sprite, x-player.spriteX, -y+app.dashAdjustment, rotateAngle = angle, align = 'left', opacity = app.dashOpacity)
elif player.direction == 'right':
sprite = app.dashSprites[app.spriteCounterDash]
drawImage(sprite, x-player.spriteX, -y+app.dashAdjustment, rotateAngle = angle, align = 'left', opacity = app.dashOpacity)
# drawRect(player.x, -player.y, player.width, player.height, fill = 'black', rotateAngle = player.rotateAngle)
# drawCircle(player.orientationX, player.orientationY, 3, fill='red')
if player.dashing == True:
if player.direction == 'right':
sprite = app.dashSpritesFinal[1]
elif player.direction == 'left':
sprite = app.dashSpritesFinal[0]
drawImage(sprite, player.x-player.spriteX, -player.y+app.dashAdjustment, align = 'left')
if player.moving == False and player.dashing == False:
if player.direction == 'left':
sprite = app.idleSprites[1]
drawImage(sprite, player.x-player.spriteX, -player.y+app.dashAdjustment, rotateAngle = player.rotateAngle, align = 'left')
elif player.direction == 'right':
sprite = app.idleSprites[0]
drawImage(sprite, player.x-player.spriteX, -player.y+app.dashAdjustment, rotateAngle = player.rotateAngle, align = 'left')
elif player.moving == True and player.dashing == False:
if player.direction == 'right':
sprite = app.moveSprites[app.spriteCounterMove]
drawImage(sprite, player.x-player.spriteX, -player.y+app.dashAdjustment, rotateAngle = player.rotateAngle, align = 'left')
elif player.direction == 'left':
sprite = app.moveSpritesFlipped[app.spriteCounterMove]
drawImage(sprite, player.x-player.spriteX, -player.y+app.dashAdjustment, rotateAngle = player.rotateAngle, align = 'left')
def createMainMenuSprites(app):
spritestrip = Image.open('Images\\mainMenu.png')
app.mainMenuSprite = []
width, height = spritestrip.size
frame = spritestrip.resize((int(width*0.75), int(height*0.75)))
sprite = CMUImage(frame)
app.mainMenuSprite.append(sprite)
spritestrip = Image.open('Images\\Tutorial.png')
width, height = spritestrip.size
frame = spritestrip.resize((int(width*0.7), int(height*0.7)))
sprite = CMUImage(frame)
app.mainMenuSprite.append(sprite)
spritestrip = Image.open('Images\\Death.png')
width, height = spritestrip.size
frame = spritestrip.resize((int(width*0.7), int(height*0.7)))
sprite = CMUImage(frame)
app.mainMenuSprite.append(sprite)
def drawUpgradeAreas(app):
sprite = app.upgradeSprites[0]
for i in range(len(app.upgradePositions)):
x, y = app.upgradePositions[i]
if app.upgradeStates[i] == True:
drawImage(sprite, x, y, align = 'center')
# drawCircle(x, y, app.powerUpRadius, fill = 'red')
def createUpgradeSprites(app):
spritestrip = Image.open('Images\\powerup.png')
app.upgradeSprites = []
width, height = spritestrip.size
frame = spritestrip.resize((int(width*0.8), int(height*0.8)))
sprite = CMUImage(frame)
app.upgradeSprites.append(sprite)
def createBackgroundSprites(app):
spritestrip = Image.open('Images\\background.png')
app.backgroundSprites = []
width, height = spritestrip.size
frame = spritestrip.resize((int(width*0.8), int(height*0.8)))
sprite = CMUImage(frame)
app.backgroundSprites.append(sprite)
def createEnemySprites(app):
app.enemySprites = []
spritestrip = Image.open('Images\crawlid.png')
frame = spritestrip.crop((5, 24, 118, 106))
width, height = frame.size
frame = frame.resize((int(width*0.5), int(height*0.5)))
sprite = CMUImage(frame)
app.enemySprites.append(sprite)
frame = spritestrip.crop((5, 24, 118, 106))
width, height = frame.size
frame = frame.resize((int(width*0.5), int(height*0.5)))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.enemySprites.append(sprite)
frame = spritestrip.crop((5, 376, 118, 465))
width, height = frame.size
frame = frame.resize((int(width*0.5), int(height*0.5)))
sprite = CMUImage(frame)
app.enemySprites.append(sprite)
spritestrip = Image.open('Images\charger.png')
for i in range(7):
frame = spritestrip.crop((2+i+63*i, 110, 63+63*i, 189))
sprite = CMUImage(frame)
app.enemySprites.append(sprite)
for i in range(7):
frame = spritestrip.crop((2+i+63*i, 110, 63+63*i, 189))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.enemySprites.append(sprite)
app.chargingSprites = []
for i in range(4):
frame = spritestrip.crop((i/3+2+82*i, 393, 81-i+82*i, 442))
sprite = CMUImage(frame)
app.chargingSprites.append(sprite)
for i in range(4):
frame = spritestrip.crop((i/3+2+82*i, 393, 81-i+82*i, 442))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.chargingSprites.append(sprite)
app.flySprites = []
spritestrip = Image.open('Images\\fly.png')
for i in range(5):
frame = spritestrip.crop((5+168*i, 8, 168+168*i-5, 197))
width, height = frame.size
frame = frame.resize((int(width/3), int(height/3)))
sprite = CMUImage(frame)
app.flySprites.append(sprite)
app.ghostSprites = []
frame = Image.open('Images\ghost.png')
width, height = frame.size
frame = frame.resize((int(width/2), int(height/2)))
sprite = CMUImage(frame)
app.ghostSprites.append(sprite)
spritestrip = Image.open('Images\enemyFireball.png')
app.fireballSprites = [ ]
for i in range(4):
frame = spritestrip.crop((10+i+136*i, 6, 136+130*i, 120))
width, height = frame.size
frame = frame.resize((int(width/2), int(height/2)))
sprite = CMUImage(frame)
app.fireballSprites.append(sprite)
def createAttackSprites(app):
app.attackSprites = []
spritestrip = Image.open('Images\playerAttacksLeftRight.png')
for i in range(4):
frame = spritestrip.crop((-300, 360*i, 1600, 360+360*i))
width, height = frame.size
frame = frame.resize((int(width/3), int(height/3)))
sprite = CMUImage(frame)
app.attackSprites.append(sprite)
app.attackSprites.append(sprite)
for i in range(4):
frame = spritestrip.crop((-300, 360*i, 1600, 360+360*i))
width, height = frame.size
frame = frame.resize((int(width/3), int(height/3)))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.attackSprites.append(sprite)
app.attackSprites.append(sprite)
app.attackUpDownSprites = []
frame = Image.open('Images\playerAttackDownwards.png')
app.sprites = [ ]
width, height = frame.size
frame = frame.resize((int(width/5), int(height/5)))
sprite = CMUImage(frame)
app.attackUpDownSprites.append(sprite)
frameFlipped = ImageOps.flip(frame)
sprite = CMUImage(frameFlipped)
app.attackUpDownSprites.append(sprite)
def createPlayerIdleSprites(app):
app.idleSprites = []
spritestrip = Image.open('Images\playerSprites.png')
i = 0
frame = spritestrip.crop((0+79*i, 2, 79+79*i, 79))
app.spriteWidth, app.spriteHeight = frame.size
sprite = CMUImage(frame)
app.idleSprites.append(sprite)
spritestrip = Image.open('Images\playerSprites.png')
i = 0
frame = spritestrip.crop((0+79*i, 2, 79+79*i, 79))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.idleSprites.append(sprite)
def createPlayerMovingSprites(app):
spritestrip = Image.open('Images\playerSprites.png')
app.moveSprites = []
app.moveSpritesFlipped = []
for i in range(1, 9):
frame = spritestrip.crop((0+79*i, 2, 79+79*i, 79))
sprite = CMUImage(frame)
app.moveSprites.append(sprite)
for i in range(1, 9):
frame = spritestrip.crop((0+79*i, 2, 79+79*i, 79))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.moveSpritesFlipped.append(sprite)
def createPlayerDashingSprites(app):
spritestrip = Image.open('Images\dashSprites.png')
app.dashSprites = []
for i in range(5):
frame = spritestrip.crop((0+130*i, 520, 130+130*i, 640))
frame = frame.resize((app.spriteWidth, app.spriteHeight))
sprite = CMUImage(frame)
app.dashSprites.append(sprite)
app.dashSpritesFlipped = []
for i in range(5):
frame = spritestrip.crop((0+130*i, 520, 130+130*i, 640))
frame = frame.resize((app.spriteWidth, app.spriteHeight))
frameFlipped = ImageOps.mirror(frame)
sprite = CMUImage(frameFlipped)
app.dashSpritesFlipped.append(sprite)
def createPlayerDashingSpritesFinal(app):
app.dashSpritesFinal = []
spritestrip = Image.open('Images\Knight.png')
frame = spritestrip.crop((2624, 1550, 2624+164, 1664))
width, height = frame.size
factor = app.spriteHeight/height
frame = frame.resize((int(width*factor), int(height*factor)))
sprite = CMUImage(frame)
app.dashSpritesFinal.append(sprite)
frame = ImageOps.mirror(frame)
sprite = CMUImage(frame)
app.dashSpritesFinal.append(sprite)
def createHealthSprites(app):
app.healthSprites = []
frame = Image.open('Images\health1.png')
width, height = frame.size
factor = 0.20
frame = frame.resize((int(width*factor), int(height*factor)))
sprite = CMUImage(frame)
app.healthSprites.append(sprite)
frame = Image.open('Images\health2.png')
width, height = frame.size
factor = 0.20
frame = frame.resize((int(width*factor), int(height*factor)))
sprite = CMUImage(frame)
app.healthSprites.append(sprite)
def createInstructionSprites(app):
app.instructionSprites = []
frame = Image.open('Images\Instructions1.png')
width, height = frame.size
factor = 0.10
frame = frame.resize((int(width*factor), int(height*factor)))
sprite = CMUImage(frame)
app.instructionSprites.append(sprite)
frame = Image.open('Images\Instructions2.png')
width, height = frame.size
factor = 0.10
frame = frame.resize((int(width*factor), int(height*factor)))
sprite = CMUImage(frame)
app.instructionSprites.append(sprite)
frame = Image.open('Images\Instructions3.png')
width, height = frame.size
factor = 0.10
frame = frame.resize((int(width*factor), int(height*factor)))
sprite = CMUImage(frame)
app.instructionSprites.append(sprite)
def drawTerrain(app):
for terrain in terrainsList:
if terrain.type == 'Rectangle':
if terrain.subtype == 'Long':
sprite = app.terrainSprites[0]
# drawRect(terrain.x, terrain.y, terrain.width, terrain.height, fill = 'green')
drawImage(sprite, terrain.x, terrain.y+app.terrainAdjustmentY, align = 'top-left')
elif terrain.subtype == 'Square':
sprite = app.terrainSprites[1]
# drawRect(terrain.x, terrain.y, terrain.width, terrain.height, fill = 'green')
drawImage(sprite, terrain.x, terrain.y+app.terrainAdjustmentY, align = 'top-left')
elif terrain.type == 'outerOval':
sprite = app.terrainSprites[2]
# drawOval(terrain.x, terrain.y, terrain.width, terrain.height, fill = 'green')
drawImage(sprite, terrain.x+app.ovalAdjustment, terrain.y+app.terrainAdjustmentY, align = 'center')
def createTerrainSprites(app):
app.terrainSprites = []
spritestrip = Image.open('Images\ground1.png')
width, height = spritestrip.size
frame = spritestrip.resize((int(width/3), int(height/3)))
sprite = CMUImage(frame)
app.terrainSprites.append(sprite)
spritestrip = Image.open('Images\squareGround.png')
width, height = spritestrip.size
frame = spritestrip.resize((int(width/5), int(height/5)))
sprite = CMUImage(frame)
app.terrainSprites.append(sprite)
spritestrip = Image.open('Images\ovalGround2.png')
width, height = spritestrip.size
frame = spritestrip.resize((int(width/6), int(height/6)))
sprite = CMUImage(frame)
app.terrainSprites.append(sprite)
def moveSprites(app):
app.stepCounter1 += 1
app.stepCounter2 += 1
app.stepCounter3 += 1
app.stepCounter4 += 1
app.stepCounter5 += 1
app.stepCounter6 += 1
app.stepCounter7 += 1
if app.stepCounter1>= app.threshold:
app.spriteCounterMove = (1 + app.spriteCounterMove) % len(app.moveSprites)
app.stepCounter1 = 0
if app.stepCounter2>= app.threshold:
app.spriteCounterDash = (1 + app.spriteCounterDash) % len(app.dashSprites)
app.stepCounter2 = 0
if app.stepCounter3 >= app.threshold:
app.spriteCounterEnemy = (1 + app.spriteCounterEnemy) % (len(app.enemySprites)-3-7) # 3 for crawlid, 7 for left direction
app.stepCounter3 = 0
if app.stepCounter4 >= app.threshold:
app.spriteCounterCharger = (1 + app.spriteCounterCharger) % (len(app.chargingSprites)-4)
app.stepCounter4 = 0
if app.stepCounter5 >= app.threshold:
app.spriteCounterFly = (1 + app.spriteCounterFly) % (len(app.flySprites))
app.stepCounter5 = 0
if app.stepCounter7 >= app.threshold:
app.spriteCounterFireball = (1 + app.spriteCounterFireball) % (len( app.fireballSprites))
app.stepCounter7 = 0
def recordPreviousPositions(app):
player.previousPositions.append((player.x, -player.y))
if len(player.previousPositions) > 5:
player.previousPositions = player.previousPositions[2:]
for enemy in enemyList:
enemy.previousPositions.append((enemy.x, -enemy.y))
if len(enemy.previousPositions) > 5:
enemy.previousPositions = enemy.previousPositions[2:]
def drawAttacks(app):
if player.isAttacking == True or player.looksAttacking == True:
if player.attackDirection == 'right':
if player.spriteCounterAttack < 4:
# drawRect(player.attackX, -player.attackY, player.attackWidth, player.attackHeight, fill='red', rotateAngle=player.rotateAngle)
sprite = app.attackSprites[player.spriteCounterAttack]
drawImage(sprite, player.attackX+app.attackAdjustment, -player.attackY,rotateAngle=player.rotateAngle)
player.spriteCounterAttack += 1
player.isAttacking = False
else:
player.spriteCounterAttack = 0
sprite = app.attackSprites[4]
drawImage(sprite, player.attackX+app.attackAdjustment, -player.attackY,rotateAngle=player.rotateAngle)
player.isAttacking = False
elif player.attackDirection == 'left':
if player.spriteCounterAttack < 4:
sprite = app.attackSprites[player.spriteCounterAttack+5]
drawImage(sprite, player.attackX+app.attackAdjustment, -player.attackY,rotateAngle=player.rotateAngle)
player.spriteCounterAttack += 1
player.isAttacking = False
else:
player.spriteCounterAttack = 0
sprite = app.attackSprites[4+5]
drawImage(sprite, player.attackX+app.attackAdjustment, -player.attackY,rotateAngle=player.rotateAngle)
player.isAttacking = False
elif player.attackDirection == 'up':
sprite = app.attackUpDownSprites[1]
drawImage(sprite, player.attackX, -player.attackY,rotateAngle=player.rotateAngle)
player.isAttacking = False
elif player.attackDirection == 'down':
sprite = app.attackUpDownSprites[0]
# drawRect(player.attackX, -player.attackY, player.attackWidth, player.attackHeight, fill='red', rotateAngle=player.rotateAngle)
drawImage(sprite, player.attackX, -player.attackY,rotateAngle=player.rotateAngle)
player.isAttacking = False
def drawEnemies(app):
for enemy in enemyList:
if enemy.isKilled == False:
if enemy.type == 'crawlid':
if enemy.direction == -1:
# drawRect(enemy.x, -enemy.y, enemy.width, enemy.height, rotateAngle = enemy.rotateAngle, fill = 'white')
sprite = app.enemySprites[0]
drawImage(sprite, enemy.x, -enemy.y+app.enemyAdjustmentY, rotateAngle = enemy.rotateAngle)
elif enemy.direction == 1:
sprite = app.enemySprites[1]
drawImage(sprite, enemy.x, -enemy.y+app.enemyAdjustmentY, rotateAngle = enemy.rotateAngle)
elif enemy.type == 'charger':
if enemy.isCharging == True:
if enemy.direction == -1:
sprite = app.chargingSprites[app.spriteCounterCharger]
drawImage(sprite, enemy.x, -enemy.y+app.enemyChargerAdjustmentY, rotateAngle = enemy.rotateAngle)
elif enemy.direction == 1:
sprite = app.chargingSprites[app.spriteCounterCharger+4]
drawImage(sprite, enemy.x, -enemy.y+app.enemyChargerAdjustmentY, rotateAngle = enemy.rotateAngle)
elif enemy.isCharging == False:
if enemy.direction == -1:
sprite = app.enemySprites[app.spriteCounterEnemy+3]
# drawRect(enemy.x, -enemy.y, enemy.width, enemy.height, rotateAngle = enemy.rotateAngle, fill = 'white')
drawImage(sprite, enemy.x, -enemy.y, rotateAngle = enemy.rotateAngle)
elif enemy.direction == 1:
sprite = app.enemySprites[app.spriteCounterEnemy+3+7]
# drawRect(enemy.x, -enemy.y, enemy.width, enemy.height, rotateAngle = enemy.rotateAngle, fill = 'white')
drawImage(sprite, enemy.x, -enemy.y, rotateAngle = enemy.rotateAngle)
elif enemy.type == 'fly':
sprite = app.flySprites[app.spriteCounterFly]
# drawRect(enemy.x, -enemy.y, enemy.width, enemy.height, fill='red')
drawImage(sprite, enemy.x, -enemy.y)
elif enemy.type == 'ghost':
if enemy.startShootFireball == True and distance(enemy.x, enemy.y, player.x, player.y) <= enemy.ghostSpawnIn:
drawCircle(enemy.fireballX, -enemy.fireballY, enemy.fireballRadius, fill='red')
sprite = app.fireballSprites[app.spriteCounterFireball]
drawImage(sprite, enemy.fireballX, -enemy.fireballY, align = 'center')
sprite = app.ghostSprites[0]
drawImage(sprite, enemy.x, -enemy.y)
else:
# enemyList.remove(enemy)
enemy.direction = 0
if enemy.type == 'crawlid':
sprite = app.enemySprites[2]
drawImage(sprite, enemy.x, -enemy.y+app.enemyAdjustment, rotateAngle = enemy.rotateAngle)
def onKeyPress(app, key):
if app.displayTutorial == True and key == 'p':
app.displayTutorial = False
return
if app.displayTutorial == False:
navigateMainMenu(app, key)
if app.gameStarted == True:
if player.freezeEverything == False:
navigateMainGame(app, key)
def navigateMainMenu(app, key):
if key == 'w':
app.arrowsUp = True
elif key == 's':
app.arrowsUp = False
if key == 'p' and app.arrowsUp == True:
app.gameStarted = True
if key == 'p' and app.arrowsUp == False:
app.displayTutorial = True
def navigateMainGame(app, key):
if key == 'space':
player.level = 3
if key == 'a':
player.direction = 'left'
player.move(-1)
for terrain in terrainsList:
implementLeftRightCollisions(player, terrain)
elif key == 'd':
player.direction = 'right'
player.move(+1)
for terrain in terrainsList:
implementLeftRightCollisions(player, terrain)
if key == 'o' and (player.jumping == False and player.isPogoing == False and player.dashing == False):
player.jumping = True
if key == 'o' and player.isCollidingWithAnything == False:
player.doubleJumping = True
if key == 'j' and player.dashing == False:
dash(app, key)
if key == 'i' and player.dashesLeft > 0 and player.level > 0:
player.dashing = True
if key == 'k' and player.resource >= player.resourceCost:
player.updateHealth(+1)
player.resource -= player.resourceCost
# if key == 'p':
# app.stepsPerSecond = 0.01
# if key == 'space':
# player.test = True
def dash(app, key):
if player.holdingUp == True:
player.attack(upwards=True)
player.holdingUp = False
elif player.holdingDown == True:
player.attack(downwards=True)
player.holdingDown = False
else:
player.attack()
for enemy in enemyList:
if player.isAttacking == True and player.checkAttackColliding(enemy) == True and not enemy.isKilled:
enemy.takeDamageEnemy(player.playerAttackDamage)
if enemy.type == 'ghost':
enemy.teleport(player)
player.attackKnockBack(enemy)
if player.resource < player.resourceMax-player.resourceGain:
player.resource += player.resourceGain
elif player.resource >= player.resourceMax-player.resourceGain:
player.resource = player.resourceMax
if enemy.type == 'charger':
attackDirection = None
if player.attackDirection == 'left':
attackDirection = -1
elif player.attackDirection == 'right':
attackDirection = +1
if attackDirection == enemy.direction:
enemy.direction *= -1
def onKeyHold(app, key):
if app.gameStarted == True:
if player.freezeEverything == False:
if 'a' in key:
player.move(-1)
player.direction = 'left'
for terrain in terrainsList:
implementLeftRightCollisions(player, terrain)
elif 'd' in key:
player.direction = 'right'
player.move(+1)
for terrain in terrainsList:
implementLeftRightCollisions(player, terrain)
if 'w' in key:
player.holdingUp = True
else:
player.holdingUp = False
if 's' in key:
player.holdingDown = True
else:
player.holdingDown = False
if 'j' in key and player.dashing == False:
if 'w' in key:
player.attack(upwards=True)
elif 's' in key:
player.attack(downwards=True)
else:
player.attack()
for enemy in enemyList:
if player.isAttacking == True and player.checkAttackColliding(enemy) == True:
enemy.takeDamageEnemy(player.playerAttackDamage)
player.attackKnockBack(enemy)
def onKeyRelease(app, key):
if app.gameStarted == True:
if key == 'd' or key == 'a':
player.moving = False
if key == 'w':
player.holdingUp = False
elif key == 's':
player.holdingDown = False
def implementRespawn(app):
if -player.y >= app.hazardLimit:
app.respawnPoints = []
player.updateHealth(-1)
for terrain in terrainsList:
if terrain.type == 'Rectangle':
terrain.getTerrainVertices()
x, y = (terrain.leftX + terrain.rightX)/2, terrain.topY
app.respawnPoints.append((x, -y+app.respawnUpDistance))
respawnDistance = []
for (x, y) in app.respawnPoints:
respawnDistance.append(distance(x, y, player.x, player.y))
minimumDistance = min(respawnDistance)
index = respawnDistance.index(minimumDistance)
player.x, player.y = app.respawnPoints[index]
def deleteFallenEnemies(app):
for enemy in enemyList:
if enemy.y >= app.hazardLimit:
del enemy
for enemy in enemyList:
if enemy.type == 'ghost' and enemy.isAttacking:
if distance(enemy.fireballX, enemy.fireballY, player.x, player.y) <= enemy.fireballRadius + player.width: # compensates for player width
player.updateHealth(-1)
def stopFreeze(app):
app.generalFreezeCounter += 1
if app.generalFreezeCounter - app.initialFreezeCounter > player.freezeDuration:
app.initialFreezeCounter = app.generalFreezeCounter
player.freezeEverything = False
player.stopFreeze = True
def checkPowerUps(app):
for i in range(len(app.upgradePositions)):
if app.upgradeStates[i] == True:
x, y = app.upgradePositions[i]
if distance(player.x, -player.y, x, y) <= app.powerUpRadius+player.width: # compensates for player width
app.upgradeStates[i] = False
player.level += 1
def implementCharger(app):
for enemy in enemyList:
if enemy.type == 'charger':
chargeLeft, chargeRight = False, False
if enemy.direction == -1:
if enemy.x - player.x > 0 and enemy.x - player.x < enemy.sightRange:
enemy.charge()
chargeLeft = True
elif enemy.direction == +1:
if player.x - enemy.x > 0 and player.x - enemy.x < enemy.sightRange:
enemy.charge()
chargeRight = True
if chargeLeft == False and chargeRight == False:
enemy.isCharging = False
def implementTimers(app):
app.generalCounter += 1
for enemy in enemyList:
(isColliding, direction, reference) = player.isCollidingRect(enemy)
if isColliding == True and not enemy.isKilled:
player.updateHealth(-1)
player.enemyCollisionDirection = direction
player.collidedEnemy = enemy
if player.stopFreeze == True:
player.knockBack(player.enemyCollisionDirection)
player.stopFreeze = False
if player.falling == False:
app.generalFallingCounter += 1
if app.generalFallingCounter - app.initialFallingCounter > player.startFallDuration:
app.initialFallingCounter = app.generalFallingCounter
player.falling = True
if enemy.startShootFireball == True:
app.generalFireballCounter += 1
if app.generalFireballCounter - app.initialFireballCounter > 90:
app.initialFireballCounter = app.generalFireballCounter
enemy.startShootFireball = False
if player.isInvincible == True:
app.generalInvincibleCounter += 1
if app.generalInvincibleCounter - app.initialInvincibleCounter > player.invincibleDuration:
app.initialInvincibleCounter = app.generalInvincibleCounter
player.isInvincible = False