-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeter.py
797 lines (728 loc) · 29.4 KB
/
peter.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
# PyGame Pac-Man Like Game "Peter the Eater" #
# Jeffrey Dickson #
# Sep 2022 #
#Initialize Modules
import pygame, sys, random, time, peterConfig
from PeterTemplates import *
#Define some variables
scale = peterConfig.screenScale
cellX = 29
cellY = 32
cellPx = int(15 + (scale * 3)) #int(screenHeight/cellY)
screenWidth = int((cellPx * cellX) + (cellPx * 15)) #=1200
screenHeight = int((cellPx * cellY) + cellPx) #=600
peterDir = 0 # 0 left 1 right 2 up 3 down
setDir = 0
manStartX = cellPx * 14
manStartY = cellPx * 24
manX = manStartX
manY = manStartY
bCanContinue = True
score = 0
currentFood = 0
level = 0
clockTickSet = int(20 + (scale * 6))
paused = False
doorsOpen = False
lives = 3
waitTime = 1
scatterTime = 1000
enemyTargX = 0
enemyTargY = 0
trackList = ["sound/Leap.wav","sound/Turns.wav","sound/nightshift.wav","sound/Kalimba.wav","sound/Spooky.wav"]
#Good: "sound/Leap.wav","sound/Turns.wav","sound/nightshift.wav","sound/Kalimba.wav","sound/Spooky.wav"
cutSceneLock = True
nVolume = 0.5
print(str(screenWidth) + " x " + str(screenHeight))
#Initialize some title changes
pygame.display.set_caption('Pickaxe Pete / F3~F4 Scale game / F5~F6 Sound level / F11 Pause')
gameIcon = pygame.image.load('img/pick.png')
pygame.display.set_icon(gameIcon)
#Classes used
class Wall(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, color):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
def update(self, color):
self.image.fill(color)
class Food(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, color):
super().__init__()
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("img/pill.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.getSound = pygame.mixer.Sound("sound/get.wav")
def update(self):
blockingHit = pygame.sprite.spritecollide(man, foodGroup, True)
for block in blockingHit:
self.getSound.set_volume(nVolume)
pygame.mixer.Sound.play(self.getSound)
addScore(10)
removeFood(1)
class Pick(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y):
super().__init__()
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("img/pick.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.getSound = pygame.mixer.Sound("sound/pickPickup.wav")
def update(self):
blockingHit = pygame.sprite.spritecollide(man, pickGroup, True)
for block in blockingHit:
self.getSound.set_volume(nVolume)
pygame.mixer.Sound.play(self.getSound)
addScore(100)
#### Man Sprite and sprite sheet ####
manSprites = ["img/peter.png", "img/peter1.png", "img/peter2.png", "img/peter3.png", "img/peter4.png", "img/peter5.png"]
crabSprites = ["img/crab1.png", "img/crab2.png"]
nagaSprites = ["img/snake0.png", "img/snake1.png"]
class Man(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, color):
super().__init__()
self.image = pygame.Surface([width,height])
self.image = pygame.image.load("img/peter.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
#self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
def update(self):
self.rect.topleft = [manX , manY] #- int((cellPx * 1.5)/4)
def animUpdate(self, direction, frame):
if direction == 1 or direction == 3:
self.image = pygame.image.load(manSprites[frame % 3])
else:
self.image = pygame.image.load(manSprites[(frame % 3) + 3])
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
class Enemy(pygame.sprite.Sprite): #Enemy(cellPx, cellPx, xP * cellPx, yP * cellPx, setColor)
def __init__(self, width, height, pos_x, pos_y, color):
super().__init__()
self.image = pygame.Surface([width,height])
self.image = pygame.image.load("img/crab1.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.eDirection = 0
self.eI = 0
self.ePosX = pos_x
self.ePosY = pos_y
self.eGridX = 0
self.eGridY = 0
self.getSound = pygame.mixer.Sound("sound/hit.wav")
def update(self):
blockingHit = pygame.sprite.spritecollide(man, enemyGroup, False)
for block in blockingHit: #Collisions with player cause death to player
self.getSound.set_volume(nVolume)
pygame.mixer.Sound.play(self.getSound)
removeLife(1)
if int(self.ePosX % cellPx) == 0 and int(self.ePosY % cellPx) == 0: #When aligned with a grid location
#Code to find direction should go here
self.eGridX = (self.ePosX)/cellPx
self.eGridY = (self.ePosY)/cellPx
if self.ePosX > enemyTargX and levelCollideCheck(self.eGridX, self.eGridY, 0) == False and self.eDirection != 1: #Set direction towards the target
self.eDirection = 0
elif self.ePosX < enemyTargX and levelCollideCheck(self.eGridX, self.eGridY, 1) == False and self.eDirection != 0:
self.eDirection = 1
elif self.ePosY > enemyTargY and levelCollideCheck(self.eGridX, self.eGridY, 2) == False and self.eDirection != 3:
self.eDirection = 2
elif self.ePosY < enemyTargY and levelCollideCheck(self.eGridX, self.eGridY, 3) == False and self.eDirection != 2:
self.eDirection = 3
else:
while levelCollideCheck(self.eGridX, self.eGridY, self.eDirection) == True:
self.eDirection = random.randint(0,3)
match self.eDirection:
case 0: #Left
self.ePosX = self.ePosX - 3
case 1: #Right
self.ePosX = self.ePosX + 3
case 2: #Up
self.ePosY = self.ePosY - 3
case 3: #Down
self.ePosY = self.ePosY + 3
case 4:
print("Can't move!")
self.rect.topleft = [self.ePosX, self.ePosY]
def animUpdateEnemy(self, frame):
self.image = pygame.image.load(crabSprites[frame % 2])
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
class NagaEnemy(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, color):
super().__init__()
self.image = pygame.Surface([width,height])
self.image = pygame.image.load("img/snake0.png")
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.eDirection = 0
self.eI = 0
self.ePosX = pos_x
self.ePosY = pos_y
self.eGridX = 0
self.eGridY = 0
self.nagID = pos_x + pos_y
self.getSound = pygame.mixer.Sound("sound/hit.wav")
def update(self):
blockingHit = pygame.sprite.spritecollide(man, nagaGroup, False)
for block in blockingHit: #Collisions with player cause death to player
self.getSound.set_volume(nVolume)
pygame.mixer.Sound.play(self.getSound)
removeLife(1)
if int(self.ePosX % cellPx) == 0 and int(self.ePosY % cellPx) == 0: #When aligned with a grid location
#Code to find direction should go here
self.eGridX = (self.ePosX)/cellPx
self.eGridY = (self.ePosY)/cellPx
if self.ePosX > enemyTargX and levelCollideCheck(self.eGridX, self.eGridY, 0) == False and self.eDirection != 1: #Set direction towards the target
self.eDirection = 0
elif self.ePosX < enemyTargX and levelCollideCheck(self.eGridX, self.eGridY, 1) == False and self.eDirection != 0:
self.eDirection = 1
elif self.ePosY > enemyTargY and levelCollideCheck(self.eGridX, self.eGridY, 2) == False and self.eDirection != 3:
self.eDirection = 2
elif self.ePosY < enemyTargY and levelCollideCheck(self.eGridX, self.eGridY, 3) == False and self.eDirection != 2:
self.eDirection = 3
else:
while levelCollideCheck(self.eGridX, self.eGridY, self.eDirection) == True:
self.eDirection = random.randint(0,3)
#Draw "rays"
setupTemp(0, 0, True, 0) #Clear old ones
nagX = self.eGridX #Check these X grid positions
nagB = True #Boolean
nagCW = False #Check Wall
for nagXP in range(20): #check 10 squares away from ourselves in both directions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You are here!
if nagB is True:
nagX = nagX + nagXP
nagB = False
else:
nagX = nagX - nagXP
nagB = True
if levelList[level][int(self.eGridY * cellX + nagX)] != "w" and nagCW == False:
setupTemp(nagX, self.eGridY, False, self.nagID)
else:
nagCW = True
nagY = self.eGridY #Check these X grid positions
nagB = True #Boolean
nagCW = False #Check Wall
for nagYP in range(20):
if nagB is True:
nagY = nagY + nagYP
nagB = False
else:
nagY = nagY - nagYP
nagB = True
if levelList[level][clamp(int(nagY * cellX + self.eGridX), 0, cellX * cellY - 1)] != "w" and nagCW == False:
setupTemp(self.eGridX, nagY, False, self.nagID)
else:
nagCW = True
match self.eDirection: #The naga moves a 1px per update as opposed to the canary which moves at the same speed as the player
case 0: #Left
self.ePosX = self.ePosX - 1
case 1: #Right
self.ePosX = self.ePosX + 1
case 2: #Up
self.ePosY = self.ePosY - 1
case 3: #Down
self.ePosY = self.ePosY + 1
case 4:
print("Can't move!")
self.rect.topleft = [self.ePosX, self.ePosY]
def fireNagas(self, ID):
if ID == self.nagID:
print("fire thing at guy")
def animUpdateEnemy(self, frame):
self.image = pygame.image.load(nagaSprites[frame % 2])
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
#TEMPORARY hit detectors
class tempSprite(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, ID):
super().__init__()
self.image = pygame.Surface([width,height])
self.image.fill((100,100,100))
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.tempID = ID
def update(self):
blockingHit = pygame.sprite.spritecollide(man, nagaIndicatorGroup, False)
for block in blockingHit: #Collisions with player cause death to player
print("OBLITERATE HIM..." + str(self.tempID))
#TEMPORARY hit detector group
nagaIndicatorGroup = pygame.sprite.Group()
def setupTemp(xLoc, yLoc, empty, ID):
global nagaIndicatorGroup
if empty == True:
pygame.sprite.Group.empty(nagaIndicatorGroup)
else:
newTemp = tempSprite(cellPx, cellPx, xLoc * cellPx, yLoc * cellPx, ID)
nagaIndicatorGroup.add(newTemp)
class Door(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y):
super().__init__()
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("img/Door.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.xLoc = pos_x
self.yLoc = pos_y
def update(self):
if doorsOpen == True:
self.image = pygame.image.load("img/DoorOpen.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [self.xLoc, self.yLoc]
class Switch(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y):
super().__init__()
self.image = pygame.Surface([width, height])
self.image = pygame.image.load("img/Switch.png")
self.getSound = pygame.mixer.Sound("sound/switch.wav")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [pos_x, pos_y]
self.xLoc = pos_x
self.yLoc = pos_y
def update(self):
blockingHit = pygame.sprite.spritecollide(man, switchGroup, False)
for block in blockingHit:
if doorsOpen == False:
self.getSound.set_volume(nVolume)
pygame.mixer.Sound.play(self.getSound)
openDoors(True)
if doorsOpen == True:
self.image = pygame.image.load("img/SwitchFlip.png")
self.image = pygame.transform.scale(self.image, (cellPx, cellPx))
self.rect = self.image.get_rect()
self.rect.topleft = [self.xLoc, self.yLoc]
def levelCollideCheck(currentX, currentY, askDirection): #takes Coords in grid space not pixel space
currentX = int(currentX)
currentY = int(currentY)
match askDirection: #Check if we can change our existing movement
case 0: #Left
if levelList[level][currentY * cellX + clamp(currentX - 1, 0 , cellX)] == "w":
return True
elif doorsOpen == False and levelList[level][currentY * cellX + clamp(currentX - 1, 0 , cellX)] == "g":
return True
case 1: #Right
if levelList[level][currentY * cellX + clamp(currentX + 1, 0 , cellX)] == "w":
return True
elif doorsOpen == False and levelList[level][currentY * cellX + clamp(currentX + 1, 0 , cellX)] == "g":
return True
case 2: #Up
if levelList[level][clamp((currentY - 1) * cellX, 0, cellY*cellX) + currentX] == "w":
return True
elif doorsOpen == False and levelList[level][clamp((currentY - 1) * cellX, 0, cellY*cellX) + currentX] == "g":
return True
case 3: #Down
if levelList[level][ clamp((currentY + 1) * cellX, 0, cellY*cellX) + currentX ] == "w":
return True
elif doorsOpen == False and levelList[level][ clamp((currentY + 1) * cellX, 0, cellY*cellX) + currentX ] == "g":
return True
return False
#Functions used
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
def ManGridCheckpoint(locX, locY): #location X and location Y of the player character
#We are on the grid coords make x and y values in grid values not pixels
gridX = int(locX/cellPx)
gridY = int(locY/cellPx)
global bCanContinue
global peterDir
global setDir
bCanContinue = True
if setDir == 0 and peterDir == 1:
peterDir = setDir
if setDir == 1 and peterDir == 0:
peterDir = setDir
if setDir == 2 and peterDir == 3:
peterDir = setDir
if setDir == 3 and peterDir == 2:
peterDir = setDir
if int(manX % cellPx) == 0 and int(manY % cellPx) == 0 : #Check if we align on the grid coords in pixels
#First Check if we're on a food
newFood.update()
newSwitch.update()
newPick.update()
match setDir:#Check if we can change our existing movement
case 0: #Left
if levelCollideCheck(gridX, gridY, 0) == False:
peterDir = setDir
case 1: #Right
if levelCollideCheck(gridX, gridY, 1) == False:
peterDir = setDir
case 2: #Up
if levelCollideCheck(gridX, gridY, 2) == False:
peterDir = setDir
case 3: #Down
if levelCollideCheck(gridX, gridY, 3) == False :
peterDir = setDir
match peterDir:#Check if we can change our existing movement
case 0: #Left
if levelCollideCheck(gridX, gridY, 0) == True:
bCanContinue = False
case 1: #Right
if levelCollideCheck(gridX, gridY, 1) == True:
bCanContinue = False
case 2: #Up
if levelCollideCheck(gridX, gridY, 2) == True:
bCanContinue = False
case 3: #Down
if levelCollideCheck(gridX, gridY, 3) == True:
bCanContinue = False
def addScore(amount):
global score
score += amount
def removeFood(amount):
global currentFood
currentFood -= amount
def addLevel(amount):
global level
level = (level + amount) % len(levelList)
def openDoors(boolIn):
global doorsOpen
doorsOpen = boolIn
#print(doorsOpen)
#pygame.sprite.Group.empty(switchGroup)
#pygame.sprite.Group.empty(doorGroup)
def cutSceneLockSet(boolIn):
global cutSceneLock
cutSceneLock = boolIn
def removeLife(amount):
global lives
lives -= amount
time.sleep(.5)
setupLevel(levelList[level])
setupEnemy(levelList[level])
setupEnemyNaga(levelList[level])
setupDoors(levelList[level])
setupSwitch(levelList[level])
setManLocation(manStartX, manStartY)
def resetLife(amount):
global lives
lives = amount
def setManLocation(pos_x, pos_y):
global manX
global manY
manX = pos_x
manY = pos_y
def setEnemyTarg(pos_x, pos_y):
global enemyTargX
global enemyTargY
enemyTargX = pos_x
enemyTargY = pos_y
def setWaitTime(amount):
global waitTime
waitTime = time.process_time() + amount
def setNewScale(amount):
global scale
global screenWidth
global screenHeight
global cellPx
global manStartX
global manStartY
global manX
global manY
global screen
global background
scale += amount
cellPx = int(15 + (scale * 3))
screenWidth = int((cellPx * cellX) + (cellPx * 15)) #=1200
screenHeight = int((cellPx * cellY) + cellPx) #=600
manStartX = cellPx * 14
manStartY = cellPx * 24
manX = manStartX
manY = manStartY
DEFAULT_BACKGROUND_SIZE = (screenWidth, screenHeight)
background = pygame.transform.scale(background, DEFAULT_BACKGROUND_SIZE)
screen = pygame.display.set_mode((screenWidth, screenHeight), pygame.RESIZABLE)
#General Setup and backdrop
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screenWidth, screenHeight), pygame.RESIZABLE)
screen.fill((14,14,14))
#set up background as group
wallGroup = pygame.sprite.Group()
def setupLevel(level):
global wallGroup
pygame.mixer.music.stop()
pygame.mixer.music.load(trackList[random.randint(0,len(trackList) - 1)])
pygame.mixer.music.play(-1)
pygame.sprite.Group.empty(wallGroup)
xP = 0
yP = 0
setColor = (255,255,255)
wallColor = (random.randint(14,123),random.randint(14,123),random.randint(14,123))
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == "w":
setColor = (wallColor)
else:
setColor = (14,14,14)
newWall = Wall(cellPx, cellPx, xP * cellPx, yP * cellPx, setColor)
wallGroup.add(newWall)
#Set up Doors
doorGroup = pygame.sprite.Group()
def setupDoors(level):
global doorGroup
global newDoor
openDoors(False)
pygame.sprite.Group.empty(doorGroup)
xP=0
yP=0
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == "g":
newDoor = Door(cellPx, cellPx, xP * cellPx, yP * cellPx)
doorGroup.add(newDoor)
#set up pellet group
foodGroup = pygame.sprite.Group()
def setupFood(level):
global foodGroup
global newFood
pygame.sprite.Group.empty(foodGroup)
xP = 0
yP = 0
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == ".":
setColor = (123,123,123)
newFood = Food(cellPx, cellPx, xP * cellPx, yP * cellPx, setColor)
foodGroup.add(newFood)
removeFood(-1)
#Set up switches
switchGroup = pygame.sprite.Group()
def setupSwitch(level):
global switchGroup
global newSwitch
pygame.sprite.Group.empty(switchGroup)
xP=0
yP=0
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == "s":
newSwitch = Switch(cellPx, cellPx, xP * cellPx, yP * cellPx)
switchGroup.add(newSwitch)
#set up canary enemy group
enemyGroup = pygame.sprite.Group()
def setupEnemy(level):
global enemyGroup
global newEnemy
pygame.sprite.Group.empty(enemyGroup)
xP = 0
yP = 0
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == "e":
setColor = (123,46,46)
newEnemy = Enemy(cellPx, cellPx, xP * cellPx, yP * cellPx, setColor)
enemyGroup.add(newEnemy)
#set up naga enemy group
nagaGroup = pygame.sprite.Group()
def setupEnemyNaga(level):
global nagaGroup
global newNaga
pygame.sprite.Group.empty(nagaGroup)
xP = 0
yP = 0
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == "n":
setColor = (46,123,46)
newNaga = NagaEnemy(cellPx, cellPx, xP * cellPx, yP * cellPx, setColor)
nagaGroup.add(newNaga)
#set up pellet group
pickGroup = pygame.sprite.Group()
def setupPick(level):
global pickGroup
global newPick
pygame.sprite.Group.empty(pickGroup)
xP = 0
yP = 0
for xP in range(0, cellX):
for yP in range(0, cellY):
if level[(yP * cellX) + xP] == "p":
newPick = Pick(cellPx, cellPx, xP * cellPx, yP * cellPx)
pickGroup.add(newPick)
#set up Peter group
manGroup = pygame.sprite.Group()
font = pygame.font.SysFont(None, 35 + scale*3)
background = pygame.image.load("img/Mountain.png")
DEFAULT_BACKGROUND_SIZE = (screenWidth, screenHeight)
background = pygame.transform.scale(background, DEFAULT_BACKGROUND_SIZE)
while True:
#### Main Menu ####
mainMenu = True
pygame.mixer.music.set_volume(0.5)
while mainMenu:
pygame.mixer.music.stop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
mainMenu = False
elif event.key == pygame.K_F3:
setNewScale(-1)
elif event.key == pygame.K_F4:
setNewScale(1)
elif event.key == pygame.K_ESCAPE:
exit()
pygame.display.flip()
screen.blit(background,(0,0))
pygame.display.flip()
clock.tick(10)
#### Reset Relevant Variables ####
mainMenu = True
resetLife(3)
removeFood(currentFood)
addLevel(-level)
addScore(-score)
openDoors(False)
#### Primary Game ####
#Prelevel Setup
manX = manStartX
manY = manStartY
pygame.sprite.Group.empty(manGroup)
man = Man(cellPx, cellPx, manStartX, manStartY, (255, 212, 123))
manGroup.add(man)
setupLevel(levelList[0])
setupFood(levelList[0])
setupEnemy(levelList[0])
setupEnemyNaga(levelList[0])
setupDoors(levelList[0])
setupSwitch(levelList[0])
setupPick(levelList[0])
manAnimFrame = 0
#optional wait between levels
setWaitTime(.05)
#Primary Game Loop
while lives > 0:########################################################################################################################
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
setDir = 0
elif event.key == pygame.K_RIGHT:
setDir = 1
elif event.key == pygame.K_UP:
setDir = 2
elif event.key == pygame.K_DOWN:
setDir = 3
elif event.key == pygame.K_F11:
if paused:
paused = False
else:
paused = True
print("Pause Game")
elif event.key == pygame.K_F2:
addLevel(1)
removeFood(currentFood)
setupLevel(levelList[level])
setupFood(levelList[level])
setupEnemy(levelList[level])
setupEnemyNaga(levelList[level])
setupSwitch(levelList[level])
setupDoors(levelList[level])
setupPick(levelList[level])
manX = manStartX
manY = manStartY
cutSceneLockSet(True)
setWaitTime(.2)
print("Next Level")
elif event.key == pygame.K_F5:
nVolume = clamp(nVolume - .1, 0 ,1)
pygame.mixer.music.set_volume(nVolume)
elif event.key == pygame.K_F6:
nVolume = clamp(nVolume + .1, 0 ,1)
pygame.mixer.music.set_volume(nVolume)
elif event.key == pygame.K_ESCAPE:
lives = 0
elif event.key == pygame.K_r:
setupTemp(0, 0, True, 0)
ManGridCheckpoint(manX, manY) #Primary Movement for player character
manAnimFrame += 1
if manAnimFrame > 300000:
manAnimFrame = 0
#Toggle Scatter Mode for enemies
if scatterTime > 300:
setEnemyTarg(manX, manY)
elif scatterTime <= 300:
setEnemyTarg(random.randint(0, cellX) * cellPx,random.randint(0, cellY) * cellPx)
if scatterTime <= 0:
scatterTime = 1000
if bCanContinue == True and not paused and not cutSceneLock:
match peterDir:
case 0: #Left
manX = manX - 3
case 1: #Right
manX = manX + 3
case 2: #Up
manY = manY - 3
case 3: #Down
manY = manY + 3
man.animUpdate(peterDir, manAnimFrame) #update animation frames
#Draw Background
pygame.display.flip()
screen.fill((14,14,14))
#screen.blit(background, (0,0))
wallGroup.draw(screen)
#Draw TEMPORARY sprites
nagaIndicatorGroup.draw(screen)
foodGroup.draw(screen)
switchGroup.draw(screen)
doorGroup.draw(screen)
pickGroup.draw(screen)
#Draw Enemies
enemyGroup.draw(screen)
nagaGroup.draw(screen)
#Draw Peter
manGroup.draw(screen)
#Draw Score
screenText = font.render("Score: " + str(score), True, (123,123,123))
screen.blit(screenText, (screenWidth * .75, screenHeight * .05))
screenText = font.render("Lives: " + str(lives), True, (123,123,123))
screen.blit(screenText, (screenWidth * .75, screenHeight * .1))
#Update
if not paused and not cutSceneLock:
man.update()
enemyGroup.update()
nagaGroup.update()
doorGroup.update()
switchGroup.update()
pickGroup.update()
nagaIndicatorGroup.update()
for newEnemy in enemyGroup:
#enemyGroup.newEnemy.animUpdateEnemy(manAnimFrame)
newEnemy.animUpdateEnemy(manAnimFrame)
for nagaEnemy in nagaGroup:
#enemyGroup.newEnemy.animUpdateEnemy(manAnimFrame)
nagaEnemy.animUpdateEnemy(manAnimFrame)
scatterTime -= 1
#Wait if needed
#### Game Loop Clock ####
#Check for level completion
if (currentFood <= 0):
addLevel(1)
setupLevel(levelList[level])
setupFood(levelList[level])
setupEnemy(levelList[level])
setupEnemyNaga(levelList[level])
setupDoors(levelList[level])
setupSwitch(levelList[level])
setupPick(levelList[level])
setupTemp(0, 0, True, 0)
cutSceneLockSet(True)
setWaitTime(.01)
manX = manStartX
manY = manStartY
man.update()
if time.process_time() > waitTime:
cutSceneLockSet(False)
clock.tick(clockTickSet)
setWaitTime(1)
cutSceneLockSet(True)