-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaigame.py
1040 lines (875 loc) · 56.5 KB
/
aigame.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
"""
Project: Pac-Man A8 in CSCI3100, CUHK
Program: Game engine for classic mode game
Main Contributors: Haoyuan YUE, Jianqiang LI, Yuan BAI
Created: March 28, 2023
Last Modified: May 5, 2023
Github Access: https://github.com/JackonLI/Pac-Man_3100_A8
Description:
This program serves as the game engine of the AI mode game. It creates and runs a new AI game, and then returns the new high score of an AI player when finished.
Classes:
- Game, which is the class for the whole game process;
- PerpetualTimer, which is a helper class for game status transition.
Dependencies:
- pygame and tkinter
- aimaze.py
How to use it:
# start the game
# newGame = Game(500) //create object with highscore
# print("Game over! Your record: {}".format(newGame.run())) //call run() to run the game; return value is the new highscore
# print("Your score: {}".format(newGame.statusScore)) //access the related attributes
Known issues:
During the game process, directly click the x button at the right-upper corner may cause abnormal exit due to the feature of tkinter. Please always use Esc to safely exit.
Acknowledgement and References:
Bandai Namco Entertainment America Inc: https://www.bandainamcoent.com/games/pac-man
Github project: https://github.com/greyblue9/pacman-python
Github project: https://github.com/lylant/PacMan-Pygame
"""
from tkinter import Tk, Label, Entry, Button, PhotoImage, messagebox, END, Canvas
from threading import Timer
from random import *
import aimaze
import os
import pygame
import time
class Game(object):
def __init__(self, highscore = 0):
# initialize tkinter window parameters
self.root = Tk()
self.root.title("Pac-Man-A8")
self.root.geometry("800x640")
self.root.resizable(0, 0)
# initialize some engine variables
self.currentLv = 1 # start from level 1
self.maxLv = 6 # max number of levels
self.isLevelGenerated = False # check the level (map) is generated or not
self.isPlaying = False # check the game is actually started (moving) or not
self.statusStartingTimer = 0 # countdown timer for 'get ready' feature
self.statusDeadTimer = 0 # countdown timer for dead event
self.statusFinishTimer = 0 # countdown timer for clear event
self.statusScore = 0 # score
self.statusRecord = highscore # record
self.statusLife = 3 # life
self.rebirthIndex = [(23, 13), (23, 13), (23, 13), (23, 13),
(23, 13)] # relative coord of rebirth location for ghost
self.randomFlag = randint(1, 5)
self.gameOverFlag = False
def run(self):
# call the next phase of initialization: loading resources
self.__initResource()
return self.statusRecord
def close(self):
self.root.destroy()
def __initResource(self):
## read the sprite files
# all sprites will saved in this dictionary
self.wSprites = {
'getready': PhotoImage(file="resources/graphics/get_ready.png"),
'gameover': PhotoImage(file="resources/graphics/game_over.png"),
'win': PhotoImage(file="resources/graphics/youwin.png"),
'wall': PhotoImage(file="resources/graphics/wall{}.png".format(self.randomFlag)),
'cage': PhotoImage(file="resources/graphics/cage.png"),
'pellet': PhotoImage(file="resources/graphics/pellet.png"),
'powerpellet': PhotoImage(file="resources/graphics/powerpoint.png"),
'fruit0': PhotoImage(file="resources/graphics/fruit-0.png"),
'fruit1': PhotoImage(file="resources/graphics/fruit-1.png"),
'fruit2': PhotoImage(file="resources/graphics/fruit-2.png"),
'lives': PhotoImage(file="resources/graphics/pacmanR-0.png")
}
# bind sprites for moving objects
for i in range(4):
# pacman: pacman(direction)(index)
if i == 3:
pass
else:
self.wSprites['pacmanL{}'.format(i + 1)] = PhotoImage(
file="resources/graphics/pacmanL-{}.png".format(i))
self.wSprites['pacmanR{}'.format(i + 1)] = PhotoImage(
file="resources/graphics/pacmanR-{}.png".format(i))
self.wSprites['pacmanU{}'.format(i + 1)] = PhotoImage(
file="resources/graphics/pacmanU-{}.png".format(i))
self.wSprites['pacmanD{}'.format(i + 1)] = PhotoImage(
file="resources/graphics/pacmanD-{}.png".format(i))
# ghosts: ghost(index1)(direction)(index2)
self.wSprites['ghost{}L1'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-0.png".format(i + 1))
self.wSprites['ghost{}L2'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-1.png".format(i + 1))
self.wSprites['ghost{}R1'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-0.png".format(i + 1))
self.wSprites['ghost{}R2'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-1.png".format(i + 1))
self.wSprites['ghost{}U1'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-0.png".format(i + 1))
self.wSprites['ghost{}U2'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-1.png".format(i + 1))
self.wSprites['ghost{}D1'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-0.png".format(i + 1))
self.wSprites['ghost{}D2'.format(i + 1)] = PhotoImage(
file="resources/graphics/ghost{}-1.png".format(i + 1))
for i in range(11):
self.wSprites['pacmanDeath{}'.format(i + 1)] = PhotoImage(
file="resources/graphics/pacman_death{}.png".format(i + 1))
# weak ghost
self.wSprites['ghostWeak1'] = PhotoImage(
file="resources/graphics/ghostWeak-0.png")
self.wSprites['ghostWeak2'] = PhotoImage(
file="resources/graphics/ghostWeak-1.png")
self.wSounds = {
'chomp1': pygame.mixer.Sound(file="resources/audio/click1.wav"),
'chomp2': pygame.mixer.Sound(file="resources/audio/click4.wav"),
'eat_power': pygame.mixer.Sound(file="resources/audio/click3.wav"),
'eat_fruit': pygame.mixer.Sound(file="resources/audio/click2.wav"),
'eat_ghost': pygame.mixer.Sound(file="resources/audio/eat_ghost.wav")
}
# call the next phase of initialization: generate widgets
self.__initWidgets()
def __initWidgets(self):
# initialize widgets for level selection
# self.wLvLabel = Label(self.root, text="Select the level.")
# self.wLvEntry = Entry(self.root)
# self.wLvBtn = Button(self.root, text="Select", command=self.lvSelect, width=5, height=1)
# initialize widgets for the game
self.wGameLabelScore = Label(self.root, text=("Score:" + str(self.statusScore)), font= ("Zig"))
self.wGameLabelLife = Label(self.root, text=("LV{} Life:".format(self.currentLv) + str(self.statusLife)), font= ("Zig"))
self.wGameLabelRecord = Label(self.root, text=("Record:" + str(self.statusRecord)), font= ("Zig"))
self.wGameCanv = Canvas(width=800, height=600)
self.wGameCanvLabelGetReady = self.wGameCanv.create_image(405, 327, image=None)
self.wGameCanvLabelGameOver = self.wGameCanv.create_image(410, 327, image=None)
self.wGameCanvLabelWin = self.wGameCanv.create_image(410, 327, image=None)
self.wGameCanvObjects = [[self.wGameCanv.create_image(0, 0, image=None) for j in range(32)] for i in range(48)]
self.wGameCanvLives = [self.wGameCanv.create_image(0, 0, image=None) for j in range(5)]
self.wGameCanv.config(background="black")
self.wGameCanvMovingObjects = [self.wGameCanv.create_image(0, 0, image=None) for n in
range(5)] # 0: pacman, 1-4: ghosts
# key binds for the game control
self.root.bind('<Left>', self.inputResponseLeft)
self.root.bind('a', self.inputResponseLeft)
self.root.bind('<Right>', self.inputResponseRight)
self.root.bind('d', self.inputResponseRight)
self.root.bind('<Up>', self.inputResponseUp)
self.root.bind('w', self.inputResponseUp)
self.root.bind('<Down>', self.inputResponseDown)
self.root.bind('s', self.inputResponseDown)
self.root.bind('<Escape>', self.inputResponseEsc)
self.root.bind('<Return>', self.inputResponseReturn)
# execute the game
# self.root.mainloop()
# call the next phase of initialization: level initialization
aimaze.newMaze.randomFlag = randint(1, 2)
self.__initLevelOnce(self.currentLv)
self.root.mainloop()
def __initLevelSelect(self):
# level selection, showing all relevant widgets
# self.wLvLabel.pack()
# self.wLvEntry.pack()
# self.wLvBtn.pack()
# execute the game
self.root.mainloop()
def lvSelect(self):
try:
self.__initLevelOnce(1)
except ValueError:
self.wLvEntry.delete(0, END) # clear the text box
messagebox.showinfo("Error!", "Enter a valid level.")
except FileNotFoundError:
self.wLvEntry.delete(0, END) # clear the text box
messagebox.showinfo("Error!", "Enter a valid level.")
def __initLevelOnce(self, level):
## this function will be called only once
self.randomFlag = randint(1, 5)
self.__initLevel(level)
# removing level selection features
# self.wLvLabel.destroy()
# self.wLvEntry.destroy()
# self.wLvBtn.destroy()
# place the canvas and set isPlaying True
self.wGameCanv.place(x=0, y=30)
self.wGameLabelScore.place(x=5, y=5)
self.wGameLabelRecord.place(x=210, y=5)
self.wGameLabelLife.place(x=615, y=5)
def __initLevel(self, level):
self.currentLv = int(level)
aimaze.newMaze.load_maze(level) # generate selected/passed level
#self.wGameCanvObjects = [[self.wGameCanv.create_image(0, 0, image=None) for j in range(32)] for i in range(48)]
self.wSprites.update({'wall': PhotoImage(file="resources/graphics/wall{}.png".format(self.randomFlag))})
self.wGameLabelLife.configure(text=("LV{} Life:".format(self.currentLv) + str(self.statusLife)), font=("Zig"))
# check the name of the object and bind the sprite, adjust their coordinate
for j in range(32):
for i in range(48):
#print("row: {}, col: {}, name: {}".format(j, i, maze.newMaze.levelObjects[i][j].name))
if aimaze.newMaze.levelObjects[i][j].name == "empty":
pass
elif aimaze.newMaze.levelObjects[i][j].name == "wall":
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['wall'], state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
elif aimaze.newMaze.levelObjects[i][j].name == "cage":
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['cage'], state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
elif aimaze.newMaze.levelObjects[i][j].name == "pellet":
if aimaze.newMaze.levelObjects[i][j].isDestroyed == False:
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['pellet'],
state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
else:
pass
elif aimaze.newMaze.levelObjects[i][j].name == "power":
if aimaze.newMaze.levelObjects[i][j].isDestroyed == False:
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['powerpellet'],
state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
else:
pass
elif aimaze.newMaze.levelObjects[i][j].name == "fruit0":
if aimaze.newMaze.levelObjects[i][j].isDestroyed == False:
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['fruit0'],
state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
else:
pass
elif aimaze.newMaze.levelObjects[i][j].name == "fruit1":
if aimaze.newMaze.levelObjects[i][j].isDestroyed == False:
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['fruit1'],
state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
else:
pass
elif aimaze.newMaze.levelObjects[i][j].name == "fruit2":
if aimaze.newMaze.levelObjects[i][j].isDestroyed == False:
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['fruit2'],
state='normal')
self.wGameCanv.coords(self.wGameCanvObjects[i][j], 3 + i * 17 + 8, 30 + j * 17 + 8)
else:
pass
for i in range(5):
if i <= self.statusLife and i > 0:
self.wGameCanv.itemconfig(self.wGameCanvLives[i], image=self.wSprites['lives'],
state='normal')
self.wGameCanv.coords(self.wGameCanvLives[i], 3 + i * 17 + 8, 30 + 32 * 17 + 8)
# bind the sprite and give it current coord. for pacman
self.wGameCanv.coords(self.wGameCanvMovingObjects[0],
3 + aimaze.newMaze.movingObjectPacman.coordinateRel[0] * 17 + 8,
30 + aimaze.newMaze.movingObjectPacman.coordinateRel[1] * 17 + 8)
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanL1'], state='normal')
# bind the sprite give them current coord. for ghosts
for i in range(4):
if aimaze.newMaze.movingObjectGhosts[i].isActive == True:
self.wGameCanv.coords(self.wGameCanvMovingObjects[i + 1],
3 + aimaze.newMaze.movingObjectGhosts[i].coordinateRel[0] * 17 + 8,
30 + aimaze.newMaze.movingObjectGhosts[i].coordinateRel[1] * 17 + 8)
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[i + 1],
image=self.wSprites['ghost{}L1'.format(i + 1)], state='normal')
# advance to next phase: get ready!
pygame.mixer.music.stop()
pygame.mixer.music.load("resources/audio/sound_intro.mp3")
pygame.mixer.music.play(loops=0, start=0.0)
self.isLevelGenerated = True
self.timerReady = PerpetualTimer(0.55, self.__initLevelStarting)
self.timerReady.start()
def inputResponseLeft(self, event):
aimaze.newMaze.movingObjectPacman.dirNext = "Left"
def inputResponseRight(self, event):
aimaze.newMaze.movingObjectPacman.dirNext = "Right"
def inputResponseUp(self, event):
aimaze.newMaze.movingObjectPacman.dirNext = "Up"
def inputResponseDown(self, event):
aimaze.newMaze.movingObjectPacman.dirNext = "Down"
def inputResponseEsc(self, event):
self.timerLoop.stop()
pygame.mixer.music.stop()
messagebox.showinfo("Game Over!", "You hit the escape key!\nWill quit after click")
self.root.quit()
def inputResponseReturn(self, event):
# return to homepage
if self.gameOverFlag == True:
self.root.quit()
else:
pass
def __initLevelStarting(self):
self.statusStartingTimer += 1 # countdown timer for this function
# bind the sprite for the widget
self.wGameCanv.itemconfig(self.wGameCanvLabelGetReady, image=self.wSprites['getready'])
if self.statusStartingTimer < 10:
# blinking function
if self.statusStartingTimer % 2 == 1:
self.wGameCanv.itemconfigure(self.wGameCanvLabelGetReady, state='normal')
else:
self.wGameCanv.itemconfigure(self.wGameCanvLabelGetReady, state='hidden')
else: # after 10 loop, the main game will be started with loopFunction
self.gameStartingTrigger()
def gameStartingTrigger(self):
## stop to print out 'get ready' and start the game
self.timerReady.stop()
self.wGameCanv.itemconfigure(self.wGameCanvLabelGetReady, state='hidden')
self.statusStartingTimer = 0
self.isPlaying = True
aimaze.newMaze.movingObjectPacman.dirNext = "Left"
# ghost sound as music
pygame.mixer.music.stop()
self.randomFlagBGM = randint(1, 4)
pygame.mixer.music.load("resources/audio/bgm{}.wav".format(self.randomFlagBGM+1))
pygame.mixer.music.play(-1)
self.timerLoop = PerpetualTimer(0.045, self.loopFunction)
self.timerLoop.start()
def loopFunction(self):
aimaze.newMaze.loopFunction()
coordGhosts = {}
for i in range(4):
coordGhosts['RelG{}'.format(i + 1)] = aimaze.newMaze.movingObjectGhosts[
i].coordinateRel # ghosts relative coordinate
coordGhosts['AbsG{}'.format(i + 1)] = aimaze.newMaze.movingObjectGhosts[
i].coordinateAbs # ghosts absolute coordinate
self.spritePacman(aimaze.newMaze.movingObjectPacman.coordinateRel,
aimaze.newMaze.movingObjectPacman.coordinateAbs)
self.spriteGhost(coordGhosts)
self.encounterEvent(aimaze.newMaze.movingObjectPacman.coordinateRel,
aimaze.newMaze.movingObjectPacman.coordinateAbs)
def spritePacman(self, coordRelP, coordAbsP):
## pacman sprite feature
# this will adjust the coordinate of the sprite and make them animated, based on their absoluteCoord.
if aimaze.newMaze.movingObjectPacman.dirCurrent == "Left":
# check the object passed maze edges
if aimaze.newMaze.movingObjectPacman.dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 17 * 47 + 17,
0) # notice this will move the sprite 17*27+17 (not 17*27+12) as the sprite will move once again below
aimaze.newMaze.movingObjectPacman.dirEdgePassed = False
else:
pass
if coordAbsP[0] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanL2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], -4, 0)
elif coordAbsP[0] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanL3'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], -4, 0)
elif coordAbsP[0] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanL2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], -4, 0)
elif coordAbsP[0] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanL1'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], -5, 0)
elif aimaze.newMaze.movingObjectPacman.dirCurrent == "Right":
# check the object passed maze edges
if aimaze.newMaze.movingObjectPacman.dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[0], -(17 * 47 + 17), 0)
aimaze.newMaze.movingObjectPacman.dirEdgePassed = False
else:
pass
if coordAbsP[0] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanR2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 4, 0)
elif coordAbsP[0] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanR3'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 4, 0)
elif coordAbsP[0] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanR2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 4, 0)
elif coordAbsP[0] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanR1'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 5, 0)
elif aimaze.newMaze.movingObjectPacman.dirCurrent == "Up":
# check the object passed maze edges
if aimaze.newMaze.movingObjectPacman.dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, 17 * 31 + 17)
aimaze.newMaze.movingObjectPacman.dirEdgePassed = False
else:
pass
if coordAbsP[1] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanU2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, -4)
elif coordAbsP[1] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanU3'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, -4)
elif coordAbsP[1] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanU2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, -4)
elif coordAbsP[1] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanU1'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, -5)
elif aimaze.newMaze.movingObjectPacman.dirCurrent == "Down":
# check the object passed maze edges
if aimaze.newMaze.movingObjectPacman.dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, -(17 * 31 + 17))
aimaze.newMaze.movingObjectPacman.dirEdgePassed = False
else:
pass
if coordAbsP[1] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanD2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, 4)
elif coordAbsP[1] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanD3'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, 4)
elif coordAbsP[1] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanD2'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, 4)
elif coordAbsP[1] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0], image=self.wSprites['pacmanD1'])
self.wGameCanv.move(self.wGameCanvMovingObjects[0], 0, 5)
def spriteGhost(self, coordGhosts):
## ghosts sprite feature
# this will adjust the coordinate of the sprite and make them animated, based on their absoluteCoord.
for ghostNo in range(4):
if aimaze.newMaze.movingObjectGhosts[ghostNo].isActive == True: # only active ghost will be shown
# normal state
if aimaze.newMaze.movingObjectGhosts[ghostNo].weakTimer == 0:
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Left":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 17 * 47 + 17, 0)
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}L1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}L1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}L2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}L2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -5, 0)
elif aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Right":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -(17 * 47 + 17), 0)
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}R1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}R1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}R2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}R2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 5, 0)
elif aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Up":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 17 * 31 + 17)
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}U1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}U1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}U2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}U2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -5)
elif aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Down":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -(17 * 31 + 17))
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}D1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}D1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}D2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghost{}D2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 5)
# weak state
else:
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Left":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 17 * 47 + 17, 0)
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -5, 0)
elif aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Right":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], -(17 * 47 + 17), 0)
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 4, 0)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][0] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 5, 0)
elif aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Up":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 17 * 31 + 17)
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -5)
elif aimaze.newMaze.movingObjectGhosts[ghostNo].dirCurrent == "Down":
# check the object passed maze edges
if aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed == True:
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, -(17 * 31 + 17))
aimaze.newMaze.movingObjectGhosts[ghostNo].dirEdgePassed = False
else:
pass
if coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 0:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 1:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak1'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 2:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 4)
elif coordGhosts['AbsG{}'.format(ghostNo + 1)][1] % 4 == 3:
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[ghostNo + 1],
image=self.wSprites['ghostWeak2'.format(ghostNo + 1)])
self.wGameCanv.move(self.wGameCanvMovingObjects[ghostNo + 1], 0, 5)
if aimaze.newMaze.movingObjectGhosts[ghostNo].weakTimer!=0:
aimaze.newMaze.movingObjectGhosts[ghostNo].weakTimer -= 1
else: # inactive ghost
pass
def encounterEvent(self, coordRelP, coordAbsP):
## encounter features
encounterMov = aimaze.newMaze.encounterMoving(coordAbsP[0],
coordAbsP[1]) # call encounterEvent for moving objects
if encounterMov == 'dead':
self.encounterEventDead()
else:
for i in range(4):
if encounterMov == 'eat{}'.format(i):
# play the sound
self.wSounds['eat_ghost'].play(loops=0)
# get plenty of score
self.statusScore += 100 # adjust the score
if self.statusScore > self.statusRecord:
self.statusRecord = self.statusScore
self.wGameLabelScore.configure(text=("Score:" + str(self.statusScore)),
font=("Zig")) # showing on the board
if self.statusRecord == self.statusScore:
self.wGameLabelRecord.configure(text=("Record:" + str(self.statusRecord)), font=("Zig"),
fg="red") # showing on the board
# reset the ghost
# adjust current coordinate
aimaze.newMaze.movingObjectGhosts[i].coordinateRel[0] = self.rebirthIndex[self.currentLv - 1][0]
aimaze.newMaze.movingObjectGhosts[i].coordinateRel[1] = self.rebirthIndex[self.currentLv - 1][1]
aimaze.newMaze.movingObjectGhosts[i].coordinateAbs[0] = self.rebirthIndex[self.currentLv - 1][0] * 4
aimaze.newMaze.movingObjectGhosts[i].coordinateAbs[1] = self.rebirthIndex[self.currentLv - 1][1] * 4
self.wGameCanv.coords(self.wGameCanvMovingObjects[i + 1],
3 + self.rebirthIndex[self.currentLv - 1][0] * 17 + 8,
30 + self.rebirthIndex[self.currentLv - 1][1] * 17 + 8)
aimaze.newMaze.movingObjectGhosts[i].weakTimer = 5
# check the object reaches grid coordinate
if coordAbsP[0] % 4 == 0 and coordAbsP[1] % 4 == 0:
encounterFix = aimaze.newMaze.encounterFixed(coordRelP[0], coordRelP[1]) # call encounterEvent function
if encounterFix == "empty":
pass
elif encounterFix == "pellet":
if aimaze.newMaze.levelObjects[coordRelP[0]][
coordRelP[1]].isDestroyed == False: # check the pellet is alive
aimaze.newMaze.levelObjects[coordRelP[0]][coordRelP[1]].isDestroyed = True # destroy the pellet
self.wGameCanv.itemconfigure(self.wGameCanvObjects[coordRelP[0]][coordRelP[1]],
state='hidden') # remove from the canvas
# play the sound (wa, ka, wa, ka, ...)
if self.statusScore % 20 == 0:
self.wSounds['chomp1'].play(loops=0)
else:
self.wSounds['chomp2'].play(loops=0)
self.statusScore += 10 # adjust the score
if self.statusScore > self.statusRecord:
self.statusRecord = self.statusScore
self.wGameLabelScore.configure(text=("Score:" + str(self.statusScore)),
font=("Zig")) # showing on the board
if self.statusRecord == self.statusScore:
self.wGameLabelRecord.configure(
text=("Record:" + str(self.statusRecord)), font=("Zig"), fg="red") # showing on the board
aimaze.newMaze.levelPelletRemaining -= 1 # adjust the remaining pellet numbers
if aimaze.newMaze.levelPelletRemaining == 0:
self.encounterEventLevelClear() # level clear
else:
pass
else: # the pellet is already taken
pass
elif encounterFix == "power":
if aimaze.newMaze.levelObjects[coordRelP[0]][
coordRelP[1]].isDestroyed == False: # check the pellet is alive
aimaze.newMaze.levelObjects[coordRelP[0]][coordRelP[1]].isDestroyed = True # destroy the pellet
self.wGameCanv.itemconfigure(self.wGameCanvObjects[coordRelP[0]][coordRelP[1]],
state='hidden') # remove from the canvas
# play the sound
self.wSounds['eat_power'].play(loops=0)
self.statusScore += 50 # adjust the score
if self.statusScore > self.statusRecord:
self.statusRecord = self.statusScore
self.wGameLabelScore.configure(text=("Score:" + str(self.statusScore)),
font=("Zig")) # showing on the board
if self.statusRecord == self.statusScore:
self.wGameLabelRecord.configure(
text=("Record:" + str(self.statusRecord)), font=("Zig"), fg="red") # showing on the board
aimaze.newMaze.levelPelletRemaining -= 1 # adjust the remaining pellet numbers
if aimaze.newMaze.levelPelletRemaining == 0:
self.encounterEventLevelClear() # level clear
else:
pass
# ghosts become weak for a certain time
for i in range(4):
if aimaze.newMaze.movingObjectGhosts[i].isActive == True:
aimaze.newMaze.movingObjectGhosts[i].weakTimer = 200
else: # the pellet is already taken
pass
else:
for i in range(3):
if encounterFix == "fruit{}".format(i):
if aimaze.newMaze.levelObjects[coordRelP[0]][
coordRelP[1]].isDestroyed == False: # check the pellet is alive
aimaze.newMaze.levelObjects[coordRelP[0]][
coordRelP[1]].isDestroyed = True # destroy the pellet
self.wGameCanv.itemconfigure(self.wGameCanvObjects[coordRelP[0]][coordRelP[1]],
state='hidden') # remove from the canvas
# play the sound
self.wSounds['eat_fruit'].play(loops=0)
self.statusScore += 50 + i * 10 # adjust the score
if self.statusScore > self.statusRecord:
self.statusRecord = self.statusScore
self.wGameLabelScore.configure(
text=("Score:" + str(self.statusScore)), font=("Zig")) # showing on the board
if self.statusRecord == self.statusScore:
self.wGameLabelRecord.configure(
text=("Record:" + str(self.statusRecord)), font=("Zig"), fg="red") # showing on the board
# maze.newMaze.levelPelletRemaining -= 1 # adjust the remaining pellet numbers
if aimaze.newMaze.levelPelletRemaining == 0:
self.encounterEventLevelClear() # level clear
else:
pass
else: # the pellet is already taken
pass
else: # pacman is not on grid coordinate
pass
def encounterEventLevelClear(self):
# pause the game
pygame.mixer.music.stop()
pygame.mixer.music.load("resources/audio/level_clear.wav")
pygame.mixer.music.play(loops=0, start=0.0)
self.timerLoop.stop()
self.isPlaying = False
for i in range(5): # hide the moving objects' sprite
self.wGameCanv.itemconfigure(self.wGameCanvMovingObjects[i], state='hidden')
self.timerClear = PerpetualTimer(0.4, self.encounterEventLevelClearLoop)
self.timerClear.start()
def encounterEventLevelClearLoop(self):
self.statusFinishTimer += 1 # countdown timer for this function
if self.statusFinishTimer < 9:
# wall blinking function
if self.statusFinishTimer % 2 == 1:
self.wSprites.update({'wall': PhotoImage(file="resources/graphics/wall7.png")})
for j in range(32):
for i in range(48):
if aimaze.newMaze.levelObjects[i][j].name == "wall":
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['wall'])
else:
pass
else:
self.wSprites.update({'wall': PhotoImage(file="resources/graphics/wall1.png")})
for j in range(32):
for i in range(48):
if aimaze.newMaze.levelObjects[i][j].name == "wall":
self.wGameCanv.itemconfig(self.wGameCanvObjects[i][j], image=self.wSprites['wall'])
else:
pass
else: # after 11 loop, the level clear process will be continued
self.encounterEventLevelClearFinish()
def encounterEventLevelClearFinish(self):
self.timerClear.stop()
self.statusFinishTimer = 0
# reset all values and hide the sprite (or level generate process will be shown)
for j in range(32):
for i in range(48):
aimaze.newMaze.levelObjects[i][j].reset('')
self.wGameCanv.itemconfigure(self.wGameCanvObjects[i][j], state='hidden')
aimaze.newMaze.movingObjectPacman.reset('Pacman')
for n in range(4):
aimaze.newMaze.movingObjectGhosts[n].reset('Ghost')
self.currentLv += 1
if self.currentLv > self.maxLv:
# win finally
self.statusWinTimer = 0
self.winTimer = PerpetualTimer(0.55, self.encounterEventWin)
self.winTimer.start()
else:
self.isLevelGenerated = False
self.randomFlag = randint(1, 5)
self.wGameCanvObjects = [[self.wGameCanv.create_image(0, 0, image=None) for j in range(32)] for i in
range(48)]
aimaze.newMaze.randomFlag = randint(1, 2)
self.__initLevel(self.currentLv)
def encounterEventDead(self):
self.wGameCanv.itemconfig(self.wGameCanvLives[self.statusLife], image=self.wSprites['lives'],
state='hidden')
self.statusLife -= 1 # subtract remaining life
if self.statusLife >= 0:
self.wGameLabelLife.configure(text=("LV{} Life:".format(self.currentLv) + str(self.statusLife)), font= ("Zig")) # showing on the board
else: # prevent showing minus life (will be game over anyway)
pass
# pause the game
self.isPlaying = False
pygame.mixer.music.stop()
self.timerLoop.stop()
# call the death loop
self.timerDeath = PerpetualTimer(0.10, self.encounterEventDeadLoop)
self.timerDeath.start()
def encounterEventDeadLoop(self):
self.statusDeadTimer += 1 # countdown timer for this function
if self.statusDeadTimer <= 5: # waiting for a while
pass
elif self.statusDeadTimer == 6:
# sound effect
pygame.mixer.music.load("resources/audio/lose2.wav")
pygame.mixer.music.play(loops=0, start=0.0)
for i in range(4): # hide the ghost sprite and initialize their status
self.wGameCanv.itemconfigure(self.wGameCanvMovingObjects[i + 1], state='hidden')
aimaze.newMaze.movingObjectGhosts[i].isActive = False
aimaze.newMaze.movingObjectGhosts[i].isCaged = True
elif 6 < self.statusDeadTimer <= 17: # animate the death sprite
self.wGameCanv.itemconfig(self.wGameCanvMovingObjects[0],
image=self.wSprites['pacmanDeath{}'.format(self.statusDeadTimer - 6)])
elif self.statusDeadTimer == 18: # blink!
self.wGameCanv.itemconfigure(self.wGameCanvMovingObjects[0], state='hidden')
elif 18 < self.statusDeadTimer <= 22: # waiting for a while
pass
else:
self.encounterEventDeadRestart()
def encounterEventDeadRestart(self):
## stop the death event and restart the game
if self.statusLife >= 0:
self.statusDeadTimer = 0 # reset the countdown timer
self.timerDeath.stop() # stopping the timer for death event
self.isPlaying = False # isPlaying flag check
aimaze.newMaze.levelPelletRemaining = 0 # Pellet count reset (will be re-counted in __initLevel)
self.__initLevel(self.currentLv)
else: # game over
self.statusDeadTimer = 0
self.timerDeath.stop()
self.gameOverTimer = PerpetualTimer(0.55, self.encounterEventDeadGameOver)
self.gameOverTimer.start()
def encounterEventDeadGameOver(self):
self.statusDeadTimer += 1
self.wGameCanv.itemconfig(self.wGameCanvLabelGameOver, image=self.wSprites['gameover'])
if self.statusDeadTimer < 8:
# blinking function
if self.statusDeadTimer % 2 == 1:
self.wGameCanv.itemconfigure(self.wGameCanvLabelGameOver, state='normal')
else:
self.wGameCanv.itemconfigure(self.wGameCanvLabelGameOver, state='hidden')
else: # after 8 loop, the game is completely finished
self.gameOverTimer.stop()
self.gameOverFlag = True
def encounterEventWin(self):
self.statusWinTimer += 1
self.wGameCanv.itemconfig(self.wGameCanvLabelWin, image=self.wSprites['win'])
if self.statusWinTimer < 8:
# blinking function
if self.statusWinTimer % 2 == 1:
self.wGameCanv.itemconfigure(self.wGameCanvLabelWin, state='normal')
else: