-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1220 lines (985 loc) · 47.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#26 long + 2 edges
#29 high + 2 edges + 3 above + 2 below
import pygame
import time
import random
from AI import TestAI
import torch
import os
pygame.init()
#Read the given text file and return it as a list of strings
def makeBoard():
b = []
f = open("initialBoard.txt", "r")
for line in f:
line = line.replace("\n", "")
b.append(line)
return b
#Find which tile an object is currently on
def findTile(center, tileSize):
return (int(center[0] // tileSize), int(center[1] // tileSize))
#Find the new location of an object
def getNewObjectLocation(location, direction, speed, tileSize):
if direction == "right":
return (location[0] + speed, ((location[1] // tileSize) + .5) * tileSize)
if direction == "left":
return (location[0] - speed, ((location[1] // tileSize) + .5) * tileSize)
if direction == "up":
return (((location[0] // tileSize) + .5) * tileSize, location[1] - speed)
if direction == "down":
return (((location[0] // tileSize) + .5) * tileSize, location[1] + speed)
return location
#Find the tile that each edge of an object is on
def findEdgeTiles(center, tileSize, objectSize):
edges = []
edges.append(findTile((center[0], center[1] + objectSize / 2), tileSize))
edges.append(findTile((center[0], center[1] - objectSize / 2), tileSize))
edges.append(findTile((center[0] + objectSize / 2, center[1]), tileSize))
edges.append(findTile((center[0] - objectSize / 2, center[1]), tileSize))
return edges
#Finds the center of a given tile
def findCenter(tile, tileSize):
return (int((tile[0] + .5) * tileSize), int((tile[1] + .5) * tileSize))
#Checks if turning in a given direction is legal
def isLegalMove(board, edges, direction):
legal = True
for edge in edges:
if direction == "right":
if len(board[0]) != edge[0] + 1:
if board[edge[1]][edge[0] + 1] == "%":
legal = False
if direction == "left":
if board[edge[1]][edge[0] - 1] == "%":
legal = False
if direction == "up":
if board[edge[1] - 1][edge[0]] == "%":
legal = False
if direction == "down":
if board[edge[1] + 1][edge[0]] == "%":
legal = False
return legal
#Check if the object has passed into a wall. If so, set its new location
def getLegalLocation(board, location, tileSize, objectSize):
edgeTiles = findEdgeTiles(location, tileSize, objectSize)
#Set validLocation to the edge not in the tile
validLocation = -1
for e in range(len(edgeTiles)):
if edgeTiles[e][0] == len(board[0]): #Right teleport pad
validLocation = e
elif board[edgeTiles[e][1]][edgeTiles[e][0]] == "%":
validLocation = e
#If would walk through edge, stop
if validLocation != -1:
if validLocation == 0:
newTile = edgeTiles[1]
location = findCenter(newTile, tileSize)
else:
newTile = edgeTiles[0]
location = findCenter(newTile, tileSize)
return location
#At a given tile, determine which directions are legal
def findPossibleDirections(board, tile):
row = tile[1]
col = tile[0]
possible = []
if board[row + 1][col] not in ["G", "%"]:
possible.append("down")
if board[row - 1][col] not in ["G", "%"]:
possible.append("up")
if col + 1 == len(board[row]): #Walked through teleport pad
possible.append("right")
elif board[row][col + 1] not in ["G", "%"]:
possible.append("right")
if col - 1 == 0: #Walked through teleport pad
possible.append("left")
elif board[row][col - 1] not in ["G", "%"]:
possible.append("left")
return possible
#Find the distance after moving
def newDistance(board, startTile, direction, endTile):
if direction == "left":
newTile = (startTile[0] - 1, startTile[1])
if direction == "right":
newTile = (startTile[0] + 1, startTile[1])
if direction == "up":
newTile = (startTile[0], startTile[1] - 1)
if direction == "down":
newTile = (startTile[0], startTile[1] + 1)
return (newTile[0] - endTile[0]) ** 2 + (newTile[1] - endTile[1]) ** 2
#Check if a given movement will cause the object to pass the center of a tile
def willPassCenter(location, direction, speed, tileSize):
#Figure out whether X or Y coordinate is important
if direction in ["up", "down"]:
location = location[1]
else:
location = location[0]
#Check whether the object will move in the positive or negative direction
if direction in ["down", "right"]:
change = speed
else:
change = -1 * speed
if ((location % tileSize) <= tileSize / 2 and (location % tileSize) + change > tileSize / 2) or ((location % tileSize) >= tileSize / 2 and (location % tileSize) + change < tileSize / 2):
return True
return False
#Check if the ghosts should switch between chase and scatter modes.
def shouldSwitchModes(cycle, timeSinceCycleStart, chaseMode, chaseTimes, scatterTimes):
shouldSwitch = False
if chaseMode:
#If chased for necessary time
if timeSinceCycleStart > chaseTimes[cycle]:
#Scatter
chaseMode = False
cycleStart = time.time()
shouldSwitch = True
#If scatter mode
else:
if timeSinceCycleStart > scatterTimes[cycle]:
#Chase
chaseMode = True
cycleStart = time.time()
shouldSwitch = True
return chaseMode
#Find the new direction of an object given the board state
def getNewDirection(board, location, direction, target, oppositeDirections):
#Find possible ways to move
possibleDirections = findPossibleDirections(board, location)
if oppositeDirections[direction] in possibleDirections:
possibleDirections.remove(oppositeDirections[direction])
#Find distance from each direction
distances = []
for i in possibleDirections:
distances.append(newDistance(board, location, i, target))
#Set direction to that
index = distances.index(min(distances))
return possibleDirections[index]
#Use AI to decide what move to make. Return a direction
def makeMoveAI(ai, location, ghostLocations, nearestPelletLocation, nearestSuperPelletLocation, score, ghostState, ghostMode, fruitVisible, currentDirection):
combinedGhostLocations = [value for tuple in ghostLocations for value in tuple]
combinedGhostLocations = [i * 1.0 for i in combinedGhostLocations]
combinedGhostLocations = [i / 27 if i % 2 == 1 else i / 35 for i in combinedGhostLocations]
formattedLocation = [location[0] * 1.0 / 27, location[1] * 1.0 / 35]
formattedPelletLocation = [nearestPelletLocation[0] * 1.0 / 27, nearestPelletLocation[1] * 1.0 / 35] + [nearestSuperPelletLocation[0] * 1.0 / 27, nearestSuperPelletLocation[1] * 1.0 / 35]
formattedInfo = [score * 1.0 / 244, ghostState * 1.0, ghostMode * 1.0, fruitVisible * 1.0]
input = torch.tensor(formattedLocation + combinedGhostLocations + formattedPelletLocation + formattedInfo)
direction = ai.run(input)
if direction == "none":
direction = currentDirection
return direction
def closestPoint(board, location, symbols):
distances = [[(location[1] - i) ** 2 + (location[0] - j) ** 2 for j in range(len(board[i]))] for i in range(len(board))]
minVal = 10000000
minLoc = None
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] in symbols:
if distances[i][j] < minVal:
minVal = distances[i][j]
minLoc = (j, i)
return minLoc
#Find inkys target
def findInkyTarget(currentDirection, pacmanTile, blinkyTile):
if currentDirection == "left":
tempTarget = (max(0, pacmanTile[0] - 4), pacmanTile[1])
elif currentDirection == "right":
tempTarget = (min(27, pacmanTile[0] + 4), pacmanTile[1])
elif currentDirection == "up":
tempTarget = (pacmanTile[0], max(0, pacmanTile[1] - 4))
elif currentDirection == "down":
tempTarget = (pacmanTile[0], min(35, pacmanTile[1] + 4))
xDistance = blinkyTile[0] - tempTarget[0]
yDistance = blinkyTile[1] - tempTarget[1]
return (min(27, max(tempTarget[0] - xDistance, 0)), min(35, max(tempTarget[1] - yDistance, 0)))
#Find pinkys target
def findPinkyTarget(currentDirection, pacmanTile):
if currentDirection == "left":
return (max(0, pacmanTile[0] - 4), pacmanTile[1])
elif currentDirection == "right":
return (min(27, pacmanTile[0] + 4), pacmanTile[1])
elif currentDirection == "up":
return (pacmanTile[0], max(0, pacmanTile[1] - 4))
elif currentDirection == "down":
return (pacmanTile[0], min(35, pacmanTile[1] + 4))
#Find clydes target
def findClydeTarget(newClydeLocation, pacmanTile, clydeScatterTile):
distance = (newClydeLocation[0] - pacmanTile[0]) ** 2 + (newClydeLocation[0] - pacmanTile[0]) ** 2
if distance > 64:
clydeTarget = pacmanTile
else:
clydeTarget = clydeScatterTile
return clydeTarget
def findTopModels(strings, X): #Credit to chatgpt for this funtion
sorted_strings = sorted(strings, key=lambda x: int(x.split('-')[1]), reverse=True)
return sorted_strings[:X]
def run(AIMode = False, ai = TestAI()):
ghosts = ["blinky", "pinky", "inky", "clyde"]
pacmanColor = (255, 255, 0)#Yellow
wallColor = (0, 0, 255)#Blue
emptyColor = (0, 0, 0)#Black
dotColor = (211, 211, 211)#Gray
offScreenColor = (0, 0, 0)#Black
ghostEntranceColor = (255, 192, 203)#Pink
tunnelColor = (0, 0, 0)#Black
intersectionWithDotColor = (0, 255, 0)#Green
intersectionWithoutDotColor = (255, 0, 255)#Purple
superPelletColor = (0, 255, 255)#Teal
edibleGhostColor = (255, 255, 255)#White
textColor = (255, 255, 255)#White
ghostColors = {"blinky":(255, 0, 0), "pinky":(255, 184, 255), "inky":(0, 255, 255), "clyde":(255, 184, 82)}
tileSize = 16
pacmanSize = 14
ghostSize = 14
pacmanSpeed = 8#Tiles/sec
ghostSpeed = 7.5#Tiles/sec
pacmanSpeedFrightened = 9
ghostSpeedFrightened = 5
framerate = 60#fps
initialLives = 3
scatterTimes = [7, 7, 5, 5]#Seconds
chaseTimes = [20, 20, 20, 20, 1000000]#Seconds
superPelletLength = 10#Seconds
speedUpThresholds = [40, 20]
#Define scatter tiles
scatterTiles = {"blinky":(25, 0), "pinky":(2, 0), "inky":(27, 35), "clyde":(0, 35)}
#Define start locations
startPositions = {"blinky":(tileSize * 14, tileSize * 14.5), "pinky":(tileSize * 14, tileSize * 17.5), "inky":(tileSize * 12, tileSize * 17.5), "clyde":(tileSize * 16, tileSize * 17.5)}
pacmanStartPosition = (tileSize * 14, tileSize * 26.5)
lives = 3
#Initialize the tiles
tile = pygame.Rect(0, 0, tileSize, tileSize)
#Initialize the window
window_size = (tileSize * 28, tileSize * 36)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Pac-Man")
#Initialize pacman
pacman = pygame.Rect(0, 0, pacmanSize, pacmanSize)
pacman.center = pacmanStartPosition
#Initialize red ghost
blinky = pygame.Rect(0, 0, ghostSize, ghostSize)
blinky.center = startPositions["blinky"]
#Initialize pink ghost
pinky = pygame.Rect(0, 0, ghostSize, ghostSize)
pinky.center = startPositions["pinky"]
#Initialize teal ghost
inky = pygame.Rect(0, 0, ghostSize, ghostSize)
inky.center = startPositions["inky"]
#Initialize pink ghost
clyde = pygame.Rect(0, 0, ghostSize, ghostSize)
clyde.center = startPositions["clyde"]
#Initialize the clock
clock = pygame.time.Clock()
#Create the board
board = makeBoard()
oppositeDirections = {"right":"left", "left":"right", "up":"down", "down":"up"}
running = True
lastInput = ""
currentDirection = ""
score = 0
pelletsEaten = 0
blinkyDirection = "left"
blinkySpeedUp = 0
pinkyDirection = "left"
inkyDirection = "left"
clydeDirection = "left"
chaseMode = True
cycle = 0
timeSinceCycleStart = 0
justSwitched = False
superPelletMode = False
superPelletStartTime = -1
eatenGhosts = []
dummyCounter = 0
globalCounter = 0
pinkyCounter = 0
inkyCounter = 0
clydeCounter = 0
pinkyThreshold = 0
inkyThreshold = 30
clydeThreshold = 60
releaseThresholds = [0, 7, 17, 32]
fruitActive = False
releasedGhosts = []
font = pygame.font.Font("PressStart2P-Regular.ttf", int(tileSize * 1.5))
ghostDirections = {ghost:"left" for ghost in ghosts}
ghostTargets = {ghost:(0, 0) for ghost in ghosts}
newGhostLocations = {ghost:(0, 0) for ghost in ghosts}
ghostTiles = {ghost:(0, 0) for ghost in ghosts}
index = 0
f = open("randomNumbers.txt", "r")
temp = f.read()
numberList = list(temp)
numberList = [int(i) for i in numberList]
while lives > 0:
if lastInput == "":#Just died or starting out
releasedGhosts = []
#Reset ghosts and pacman
pacman.center = pacmanStartPosition
for ghost in ghosts:
eval(ghost).center = startPositions[ghost]
ghostTiles[ghost] = findTile(eval(ghost).center, tileSize)
if releasedGhosts == [] and lastInput != "":
releasedGhosts = ["blinky"]
if AIMode:
if lastInput == "":
lastInput = "left"
currentDirection = "left"
pacmanTile = findTile(pacman.center, tileSize)
ghostLocations = []
for ghost in ghosts:
ghostLocations.append(findTile(eval(ghost).center, tileSize))
closestPellet = closestPoint(board, pacmanTile, [".", "I"])
closestSuperPellet = closestPoint(board, pacmanTile, ["+"])
if closestPellet == None:
closestPellet = closestSuperPellet
if closestSuperPellet == None:
closestSuperPellet = closestPellet
ghostState = 1 if superPelletMode else 0
ghostMode = 1 if chaseMode else 0
fruitVisible = 1 if fruitActive else 0
lastInput = makeMoveAI(ai, pacmanTile, ghostLocations, closestPellet, closestSuperPellet, score, ghostState, ghostMode, fruitVisible, currentDirection)
else:
#Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == pygame.K_w:
lastInput = "up"
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
lastInput = "down"
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
lastInput = "left"
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
lastInput = "right"
#Switch between chase and scatter modes
tempChaseMode = shouldSwitchModes(cycle, timeSinceCycleStart, chaseMode, chaseTimes, scatterTimes)
if tempChaseMode != chaseMode:
chaseMode = tempChaseMode
justSwitched = True
timeSinceCycleStart = 0
#End super pellet mode
if superPelletMode:
if time.time() - superPelletStartTime > superPelletLength:
superPelletMode = False
#Make the window white
window.fill((255, 255, 255))
#Draw the board
for row in range(len(board)):
for item in range(len(board[0])):
tile.center = (tileSize // 2 + item * tileSize, tileSize // 2 + row * tileSize)
if board[row][item] == "%":#Wall
pygame.draw.rect(window, wallColor, tile)
elif board[row][item] == ".":#Dot
pygame.draw.rect(window, dotColor, tile)
elif board[row][item] == "0":#Empty
pygame.draw.rect(window, emptyColor, tile)
elif board[row][item] == "-":#Above or below screen
pygame.draw.rect(window, offScreenColor, tile)
elif board[row][item] == "G":#Ghost entrance
pygame.draw.rect(window, ghostEntranceColor, tile)
elif board[row][item] == "X":#Tunnel
pygame.draw.rect(window, tunnelColor, tile)
elif board[row][item] == "I":#Intersection with dot
pygame.draw.rect(window, intersectionWithDotColor, tile)
elif board[row][item] == "i":#Intersection without dot
pygame.draw.rect(window, intersectionWithoutDotColor, tile)
elif board[row][item] == "+":#Super pellet
pygame.draw.rect(window, superPelletColor, tile)
else:
pass
#Display score
scoreText = font.render("Score: " + str(score), True, textColor)
scoreRect = scoreText.get_rect()
scoreRect.topleft = (0, 0)
window.blit(scoreText, scoreRect)
#Display lives
livesText = font.render("Lives: " + str(lives), True, textColor)
livesRect = livesText.get_rect()
livesRect.topleft = (200, 0)
window.blit(livesText, livesRect)
#Start moving
if currentDirection == "" and lastInput != "":
if lastInput == "left":
currentDirection = lastInput
elif lastInput == "right":
currentDirection = lastInput
#Check if move is legal
edgeTiles = findEdgeTiles(pacman.center, tileSize, pacmanSize)
if isLegalMove(board, edgeTiles, lastInput):
currentDirection = lastInput
#Move pacman
pacmanLocation = pacman.center
newPacmanLocation = getNewObjectLocation(pacman.center, currentDirection, pacmanSpeed * tileSize / framerate, tileSize)
#Prevent walking through walls
newPacmanLocation = getLegalLocation(board, newPacmanLocation, tileSize, pacmanSize)
pacman.center = newPacmanLocation
#Teleport pads for pacman
pacmanTile = findTile(newPacmanLocation, tileSize)
if board[pacmanTile[1]][pacmanTile[0]] == "X": #On teleport
if pacmanTile == (0, 17): #Left teleport
if currentDirection == "left": #Moving left
pacmanTile = (27, 17)
pacman.center = findCenter(pacmanTile, tileSize)
elif pacmanTile == (27, 17): #Right teleport
if currentDirection == "right":
pacmanTile = (0, 17)
pacman.center = findCenter(pacmanTile, tileSize)
#Update tiles based on pacmans position
pacmanTile = findTile(pacman.center, tileSize)
#Change dot to empt y
if board[pacmanTile[1]][pacmanTile[0]] == ".": #Dot, not intersection
board[pacmanTile[1]] = board[pacmanTile[1]][:pacmanTile[0]] + "0" + board[pacmanTile[1]][pacmanTile[0] + 1:]
score += 1
pelletsEaten += 1
if 244 - pelletsEaten in speedUpThresholds:
blinkySpeedUp += 1
if lives == initialLives:
if "pinky" not in releasedGhosts:
pinkyCounter += 1
elif "inky" not in releasedGhosts:
inkyCounter += 1
elif "clyde" not in releasedGhosts:
clydeCounter += 1
if pinkyCounter >= pinkyThreshold and "pinky" not in releasedGhosts:
pinky.center = startPositions["blinky"]
releasedGhosts.append("pinky")
if inkyCounter >= inkyThreshold and "inky" not in releasedGhosts:
inky.center = startPositions["blinky"]
releasedGhosts.append("inky")
if clydeCounter >= clydeThreshold and "clyde" not in releasedGhosts:
clyde.center = startPositions["blinky"]
releasedGhosts.append("clyde")
else:
globalCounter += 1
if globalCounter in releaseThresholds:
ghostToBeReleased = ghosts[releaseThresholds.index(globalCounter)]
releasedGhosts.append(ghostToBeReleased)
eval(ghostToBeReleased).center = startPositions["blinky"]
elif board[pacmanTile[1]][pacmanTile[0]] == "I": #Dot, intersection
board[pacmanTile[1]] = board[pacmanTile[1]][:pacmanTile[0]] + "i" + board[pacmanTile[1]][pacmanTile[0] + 1:]
score += 1
pelletsEaten += 1
if 244 - pelletsEaten in speedUpThresholds:
blinkySpeedUp += 1
if lives == initialLives:
if "pinky" not in releasedGhosts:
pinkyCounter += 1
elif "inky" not in releasedGhosts:
inkyCounter += 1
elif "clyde" not in releasedGhosts:
clydeCounter += 1
if pinkyCounter >= pinkyThreshold and "pinky" not in releasedGhosts:
pinky.center = startPositions["blinky"]
releasedGhosts.append("pinky")
if inkyCounter >= inkyThreshold and "inky" not in releasedGhosts:
inky.center = startPositions["blinky"]
releasedGhosts.append("inky")
if clydeCounter >= clydeThreshold and "clyde" not in releasedGhosts:
clyde.center = startPositions["blinky"]
releasedGhosts.append("clyde")
else:
globalCounter += 1
if globalCounter in releaseThresholds:
ghostToBeReleased = ghosts[releaseThresholds.index(globalCounter)]
releasedGhosts.append(ghostToBeReleased)
eval(ghostToBeReleased).center = startPositions[ghostToBeReleased]
elif board[pacmanTile[1]][pacmanTile[0]] == "+": #Super pellet
board[pacmanTile[1]] = board[pacmanTile[1]][:pacmanTile[0]] + "0" + board[pacmanTile[1]][pacmanTile[0] + 1:]
superPelletMode = True
superPelletStartTime = time.time()
score += 10
pelletsEaten += 1
if 244 - pelletsEaten in speedUpThresholds:
blinkySpeedUp += 1
eatenGhosts = []
justSwitched = True
if lives == initialLives:
if "pinky" not in releasedGhosts:
pinkyCounter += 1
elif "inky" not in releasedGhosts:
inkyCounter += 1
elif "clyde" not in releasedGhosts:
clydeCounter += 1
if pinkyCounter >= pinkyThreshold and "pinky" not in releasedGhosts:
pinky.center = startPositions["blinky"]
releasedGhosts.append("pinky")
if inkyCounter >= inkyThreshold and "inky" not in releasedGhosts:
inky.center = startPositions["blinky"]
releasedGhosts.append("inky")
if clydeCounter >= clydeThreshold and "clyde" not in releasedGhosts:
clyde.center = startPositions["blinky"]
releasedGhosts.append("clyde")
else:
globalCounter += 1
if globalCounter in releaseThresholds:
ghostToBeReleased = ghosts[releaseThresholds.index(globalCounter)]
releasedGhosts.append(ghostToBeReleased)
eval(ghostToBeReleased).center = startPositions[ghostToBeReleased]
for ghost in releasedGhosts:
#Find ghosts new location
newGhostLocations[ghost] = eval(ghost).center
if currentDirection != "":#Started
if justSwitched:
ghostDirections[ghost] = oppositeDirections[ghostDirections[ghost]]
if superPelletMode:
curTile = ghostTiles[ghost]
if numberList[index] == 0:
target = (curTile[0] - 1, curTile[1])
if numberList[index] == 1:
target = (curTile[0] + 1, curTile[1])
if numberList[index] == 2:
target = (curTile[0], curTile[1] - 1)
if numberList[index] == 3:
target = (curTile[0], curTile[1] + 1)
ghostTargets[ghost] = target
index = (index + 1) % 10000
elif chaseMode or (ghost == "blinky" and 244 - pelletsEaten <= speedUpThresholds[0]): #Blinky has no scatter mode after speeding up
#Find target
if ghost == "blinky":
ghostTargets["blinky"] = pacmanTile
elif ghost == "pinky":
ghostTargets["pinky"] = findPinkyTarget(currentDirection, pacmanTile)
elif ghost == "inky":
ghostTargets["inky"] = findInkyTarget(currentDirection, pacmanTile, ghostTiles["blinky"])
elif ghost == "clyde":
ghostTargets["clyde"] = findClydeTarget(newGhostLocations["clyde"], pacmanTile, scatterTiles["clyde"])
else: #Scatter mode
#Find target
ghostTargets[ghost] = scatterTiles[ghost]
if superPelletMode:
speed = ghostSpeedFrightened * tileSize / framerate
if ghost == "blinky":
speed = (ghostSpeed + .5 * blinkySpeedUp) * tileSize / framerate
else:
speed = ghostSpeed * tileSize / framerate
if willPassCenter(eval(ghost).center, ghostDirections[ghost], speed, tileSize):
#Get new direction
ghostDirections[ghost] = getNewDirection(board, ghostTiles[ghost], ghostDirections[ghost], ghostTargets[ghost], oppositeDirections)
#Center ghost on tile
eval(ghost).center = findCenter(ghostTiles[ghost], tileSize)
newGhostLocations[ghost] = getNewObjectLocation(eval(ghost).center, ghostDirections[ghost], speed, tileSize)
justSwitched = False
#Dont walk through walls
for ghost in releasedGhosts:
newGhostLocations[ghost] = getLegalLocation(board, newGhostLocations[ghost], tileSize, ghostSize)
eval(ghost).center = newGhostLocations[ghost]
ghostTiles[ghost] = findTile(eval(ghost).center, tileSize)
#Teleport pads
for ghost in releasedGhosts:
if board[ghostTiles[ghost][1]][ghostTiles[ghost][0]] == "X": #On teleport
if ghostTiles[ghost] == (0, 17): #Left teleport
if ghostDirections[ghost] == "left": #Moving left
ghostTiles[ghost] = (27, 17)
eval(ghost).center = findCenter(ghostTiles[ghost], tileSize)
elif ghostTiles[ghost] == (27, 17): #Right teleport
if ghostDirections[ghost] == "right": #Moving right
ghostTiles[ghost] = (0, 17)
eval(ghost).center = findCenter(ghostTiles[ghost], tileSize)
#Update tile
for ghost in releasedGhosts:
ghostTiles[ghost] = findTile(eval(ghost).center, tileSize)
#Check if pacman or ghosts are touching
for ghost in releasedGhosts:
if pacmanTile == ghostTiles[ghost]:
if superPelletMode and ghost not in eatenGhosts:
#Eat ghosts
score += 100
eatenGhosts.append(ghost)
#Reset ghost
eval(ghost).center = (tileSize * 14, tileSize * 14.5)
ghostTiles[ghost] = findTile(eval(ghost).center, tileSize)
else:
lives -= 1
lastInput = ""
globalCounter = 0
superPelletMode = False
justDied = True
#Add pacman
pygame.draw.rect(window, pacmanColor, pacman)
#Add ghosts
for ghost in ghosts:
if superPelletMode and ghost not in eatenGhosts: #Pinky can still be eaten
pygame.draw.rect(window, edibleGhostColor, eval(ghost))
else:
pygame.draw.rect(window, ghostColors[ghost], eval(ghost))
#Update time since ghosts switched between chase and scatter modes. Dont update if in super pellet mode (i think)
if lastInput != "" and not superPelletMode:
timeSinceCycleStart += 1 / framerate
#Update the board
pygame.display.update()
#Limit the frame rate
clock.tick(framerate)
pygame.quit()
return score, ai
def runNoGraphics(AIMode = True, ai = "", speedUpFactor = 1):
ghosts = ["blinky", "pinky", "inky", "clyde"]
if ai == "":
ai = TestAI()
ai.createModel()
tileSize = 16
pacmanSize = 14
ghostSize = 14
pacmanSpeed = 8 * speedUpFactor#Tiles/sec
ghostSpeed = 7.5 * speedUpFactor#Tiles/sec
pacmanSpeedFrightened = 9 * speedUpFactor
ghostSpeedFrightened = 5 * speedUpFactor
framerate = 60 * speedUpFactor#fps
initialLives = 3
scatterTimes = [7, 7, 5, 5]#Seconds
chaseTimes = [20, 20, 20, 20, 1000000]#Seconds
superPelletLength = 10#Seconds
speedUpThresholds = [40, 20]
#Define scatter tiles
scatterTiles = {"blinky":(25, 0), "pinky":(2, 0), "inky":(27, 35), "clyde":(0, 35)}
#Define start locations
startPositions = {"blinky":(tileSize * 14, tileSize * 14.5), "pinky":(tileSize * 14, tileSize * 17.5), "inky":(tileSize * 12, tileSize * 17.5), "clyde":(tileSize * 16, tileSize * 17.5)}
pacmanStartPosition = (tileSize * 14, tileSize * 26.5)
lives = 3
#Initialize the tiles
tile = pygame.Rect(0, 0, tileSize, tileSize)
#Initialize pacman
pacman = pygame.Rect(0, 0, pacmanSize, pacmanSize)
pacman.center = pacmanStartPosition
#Initialize red ghost
blinky = pygame.Rect(0, 0, ghostSize, ghostSize)
blinky.center = startPositions["blinky"]
#Initialize pink ghost
pinky = pygame.Rect(0, 0, ghostSize, ghostSize)
pinky.center = startPositions["pinky"]
#Initialize teal ghost
inky = pygame.Rect(0, 0, ghostSize, ghostSize)
inky.center = startPositions["inky"]
#Initialize pink ghost
clyde = pygame.Rect(0, 0, ghostSize, ghostSize)
clyde.center = startPositions["clyde"]
#Initialize the clock
clock = pygame.time.Clock()
#Create the board
board = makeBoard()
oppositeDirections = {"right":"left", "left":"right", "up":"down", "down":"up"}
running = True
lastInput = ""
currentDirection = ""
score = 0
pelletsEaten = 0
blinkyDirection = "left"
blinkySpeedUp = 0
pinkyDirection = "left"
inkyDirection = "left"
clydeDirection = "left"
chaseMode = True
cycle = 0
timeSinceCycleStart = 0
justSwitched = False
superPelletMode = False
superPelletStartTime = -1
eatenGhosts = []
dummyCounter = 0
globalCounter = 0
pinkyCounter = 0
inkyCounter = 0
clydeCounter = 0
pinkyThreshold = 0
inkyThreshold = 30
clydeThreshold = 60
releaseThresholds = [0, 7, 17, 32]
fruitActive = False
releasedGhosts = []
ghostDirections = {ghost:"left" for ghost in ghosts}
ghostTargets = {ghost:(0, 0) for ghost in ghosts}
newGhostLocations = {ghost:(0, 0) for ghost in ghosts}
ghostTiles = {ghost:(0, 0) for ghost in ghosts}
index = 0
f = open("randomNumbers.txt", "r")
temp = f.read()
numberList = list(temp)
numberList = [int(i) for i in numberList]
while lives > 0:
if lastInput == "":#Just died or starting out
releasedGhosts = []
#Reset ghosts and pacman
pacman.center = pacmanStartPosition
for ghost in ghosts:
eval(ghost).center = startPositions[ghost]
ghostTiles[ghost] = findTile(eval(ghost).center, tileSize)
if releasedGhosts == [] and lastInput != "":
releasedGhosts = ["blinky"]
if AIMode:
if lastInput == "":
lastInput = "left"
currentDirection = "left"
pacmanTile = findTile(pacman.center, tileSize)
ghostLocations = []
for ghost in ghosts:
ghostLocations.append(findTile(eval(ghost).center, tileSize))
closestPellet = closestPoint(board, pacmanTile, [".", "I"])
closestSuperPellet = closestPoint(board, pacmanTile, ["+"])
if closestPellet == None:
closestPellet = closestSuperPellet
if closestSuperPellet == None:
closestSuperPellet = closestPellet
ghostState = 1 if superPelletMode else 0
ghostMode = 1 if chaseMode else 0
fruitVisible = 1 if fruitActive else 0
lastInput = makeMoveAI(ai, pacmanTile, ghostLocations, closestPellet, closestSuperPellet, score, ghostState, ghostMode, fruitVisible, currentDirection)
else:
#Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == pygame.K_w:
lastInput = "up"
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
lastInput = "down"
elif event.key == pygame.K_LEFT or event.key == pygame.K_a:
lastInput = "left"
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
lastInput = "right"
#Switch between chase and scatter modes
tempChaseMode = shouldSwitchModes(cycle, timeSinceCycleStart, chaseMode, chaseTimes, scatterTimes)
if tempChaseMode != chaseMode:
chaseMode = tempChaseMode
justSwitched = True
timeSinceCycleStart = 0
#End super pellet mode
if superPelletMode:
if time.time() - superPelletStartTime > superPelletLength:
superPelletMode = False
#Start moving
if currentDirection == "" and lastInput != "":
if lastInput == "left":
currentDirection = lastInput
elif lastInput == "right":
currentDirection = lastInput
#Check if move is legal
edgeTiles = findEdgeTiles(pacman.center, tileSize, pacmanSize)
if isLegalMove(board, edgeTiles, lastInput):
currentDirection = lastInput
#Move pacman
pacmanLocation = pacman.center
newPacmanLocation = getNewObjectLocation(pacman.center, currentDirection, pacmanSpeed * tileSize / framerate, tileSize)
#Prevent walking through walls
newPacmanLocation = getLegalLocation(board, newPacmanLocation, tileSize, pacmanSize)
pacman.center = newPacmanLocation
#Teleport pads for pacman
pacmanTile = findTile(newPacmanLocation, tileSize)
if board[pacmanTile[1]][pacmanTile[0]] == "X": #On teleport
if pacmanTile == (0, 17): #Left teleport
if currentDirection == "left": #Moving left
pacmanTile = (27, 17)
pacman.center = findCenter(pacmanTile, tileSize)
elif pacmanTile == (27, 17): #Right teleport
if currentDirection == "right":
pacmanTile = (0, 17)
pacman.center = findCenter(pacmanTile, tileSize)
#Update tiles based on pacmans position
pacmanTile = findTile(pacman.center, tileSize)
#Change dot to empt y
if board[pacmanTile[1]][pacmanTile[0]] == ".": #Dot, not intersection
board[pacmanTile[1]] = board[pacmanTile[1]][:pacmanTile[0]] + "0" + board[pacmanTile[1]][pacmanTile[0] + 1:]
score += 1
pelletsEaten += 1
if 244 - pelletsEaten in speedUpThresholds:
blinkySpeedUp += 1
if lives == initialLives:
if "pinky" not in releasedGhosts:
pinkyCounter += 1
elif "inky" not in releasedGhosts:
inkyCounter += 1
elif "clyde" not in releasedGhosts:
clydeCounter += 1
if pinkyCounter >= pinkyThreshold and "pinky" not in releasedGhosts:
pinky.center = startPositions["blinky"]
releasedGhosts.append("pinky")
if inkyCounter >= inkyThreshold and "inky" not in releasedGhosts:
inky.center = startPositions["blinky"]
releasedGhosts.append("inky")
if clydeCounter >= clydeThreshold and "clyde" not in releasedGhosts:
clyde.center = startPositions["blinky"]
releasedGhosts.append("clyde")
else:
globalCounter += 1
if globalCounter in releaseThresholds: