-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
405 lines (341 loc) · 17.6 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
#this project is built on this repo https://github.com/moritree/PY-MAN
import pygame
import pygame.freetype
from Maze import Maze
from Pac_Man import Pac_Man
from Items import *
from Ghost import Ghost
from Constants import *
from pygame.locals import *
from movementAlgos import *
from NeatHelpers import *
import neat
import random
pacmanController = humanPlayer # #options: dummy, humanPlayer, neat, avoid_ghost_and_wall_dummy, pathFind_to_target
if(neatMode):
pacmanController = modelNeat
if(neatLoadMode):
pacmanController = modelNeat
class Main:
def __init__(self):
self.maze_width = MapSizeX
self.maze_height = MapSizeY
self.current_generation = 0
if LoadTrainingCheckpointPath != None:
self.current_generation = LoadTrainingCheckpointGenerationNum
self.last_life_score = 0
self.display_width = self.maze_width * block_size
self.display_height = self.maze_height * block_size + offset
self.fps = 60
if(fastMode): self.fps = 99999999
self.fps_clock = pygame.time.Clock()
self.tick_counter = 1
self.temp_counter = 0
self.score = 0
self.lastFrameScore = 0
self.framesUntilOutOfTime = scoreTimeConstraint
if(neatMode and self.current_generation%disableGhostsEvery ==1 and turnOffGhosts): self.framesUntilOutOfTime= 15*60
self.collected_pellets = 0
self.pellets = []
self.power_pellets = []
self.num_fruit = 0
self.running = True
self.coinFlip = (random.choice([0,1]) == 1)
self.manuallySave = False #keep this false
self.game_state = "run"
self.level = 0
def events(self, player):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
player.humanInput = UP
if event.key == pygame.K_DOWN:
player.humanInput = DOWN
if event.key == pygame.K_LEFT:
player.humanInput = LEFT
if event.key == pygame.K_RIGHT:
player.humanInput = RIGHT
if event.key == pygame.K_COMMA:
printMaze(self.maze.maze_array)
print("pac_man_x: {} pac_man_y: {}".format(self.player.x, self.player.y))
if event.key == pygame.K_EQUALS and neatMode:
self.player.lives = -1
self.game_state = "lose"
self.manuallySave = True
if event.type == pygame.QUIT:
pygame.quit()
exit()
return
def loop(self):
#kill score
if(neatMode and killScore!=None and self.score >= killScore):
self.player.lives = -1
self.game_state = "lose"
if(self.ghosts["clyde"].mode == "normal"):
self.player.allGhostsOut = True
if(self.lastFrameScore==self.score):
self.player.framesNotScoring +=1
else:
self.player.framesNotScoring =0
self.player.frameMod60 = self.tick_counter%60
#die if you don't score fast enough
if scoreTimeConstraint != None and (neatMode):
if self.lastFrameScore==self.score:
self.framesUntilOutOfTime-=1
if self.framesUntilOutOfTime <=0:
self.framesUntilOutOfTime = scoreTimeConstraint
if(neatMode and self.current_generation%disableGhostsEvery ==1 and turnOffGhosts): self.framesUntilOutOfTime= 15*60
if self.player.lives > 0:
self.player.target = 0
self.game_state = "respawn"
self.fruit.here = False
self.player.lives -= 1
self.temp_counter = 0
else:
self.game_state = "lose"
else:
self.framesUntilOutOfTime = scoreTimeConstraint
if(neatMode and self.current_generation%disableGhostsEvery ==1 and turnOffGhosts): self.framesUntilOutOfTime= 15*60
self.lastFrameScore =self.score
self.player.pelletRatio = self.collected_pellets/len(self.pellets)
if self.game_state == "run":
# activate inky once 30 coins have been collected
if self.ghosts["inky"].mode == "house" and self.collected_pellets > 30:
self.ghosts["inky"].mode = "normal"
# activate clyde once 1/3 of total coins have been collected
if self.ghosts["clyde"].mode == "house" and self.collected_pellets > len(self.pellets) / 3:
self.ghosts["clyde"].mode = "normal"
#this only decreases score in neatmode (look at the move function)
scoreB4 = self.score
self.score -= self.player.move(maze = self.maze, display_width = self.display_width, ghosts = self.ghosts, power_pellets = self.power_pellets, pellets = self.pellets, fruit = self.fruit)
if(self.lastFrameScore == scoreB4):
self.lastFrameScore = self.score #simulates the score not increasing
if self.player.update_power_up():
for ghost in self.ghosts.values():
if ghost.mode != "dead" and not ghost.blue:
ghost.blue = True
ghost.blue_timer = 0
for pellet in self.pellets:
if pellet.collide(self.player):
self.collected_pellets += 1
self.score += pellet_score*(1+self.collected_pellets*clearMapBonus/251)
for power_pellet in self.power_pellets:
if power_pellet.collide(self.player):
self.score += power_pellet_score*(1+self.collected_pellets*clearMapBonus/251)
self.player.power_up(8 * 60)
for ghost in self.ghosts.values():
ghost.move(self.player, self.maze, self.display_width, self.tick_counter, self.ghosts["blinky"].array_coord)
if ghost.collide(self.player):
if ghost.blue and ghost.mode != "dead":
ghost.mode = "dead"
self.score += ghost_scores[self.player.ghosts_eaten]*(1+self.collected_pellets*clearMapBonus/251)
self.player.ghosts_eaten +=1
else:
if self.player.lives > 0:
self.player.target = 0
self.game_state = "respawn"
self.fruit.here = False
self.player.lives -= 1
self.temp_counter = 0
else:
self.game_state = "lose"
# Update fruitdisplayWidth
self.fruit.update()
# Give fruit
if self.num_fruit == 0 and self.collected_pellets >= len(self.pellets) / 3:
self.fruit.time = fruit_time * 60
self.fruit.here = True
self.num_fruit += 1
elif self.num_fruit == 1 and self.collected_pellets >= len(self.pellets) * 2/3:
self.fruit.time = fruit_time * 60
self.fruit.here = True
self.num_fruit += 1
self.score += self.fruit.collide(self.player)*(1+self.collected_pellets*clearMapBonus/251)
if self.score - self.last_life_score >= life_points:
self.player.lives += 1
self.last_life_score += life_points
def draw(self, surface, window):
if(fastMode and (self.tick_counter%neatFrameShow != 0)):
if self.game_state == "respawn":
if self.temp_counter < 36:
self.temp_counter += 1
else:
self.game_state = "run"
self.player.x = spawn_x * block_size + block_size / 2
self.player.y = spawn_y * block_size + block_size / 2
self.look_dir = DOWN
self.humanInput = DOWN
self.move_dir = DOWN
self.try_move_dir = DOWN
self.lastMoveDir = DOWN
if(forceStuck and neatMode):
self.player.x=(spawn_x-5)* block_size + block_size / 2
self.player.y=house_y* block_size + block_size / 2
self.player.move_dir = UP
self.player.look_dir = UP
self.player.try_move_dir = UP
self.player.humanInput = UP
self.lastMoveDir = UP
for ghost in self.ghosts.values():
if(ghost.mode != "house"):
ghost.x = house_x* block_size + block_size / 2
ghost.y = house_y* block_size + block_size / 2
return
pygame.draw.rect(surface, (0, 0, 0), (0, 0, self.display_width, self.display_height))
self.maze.draw(surface)
self.display_fruit.draw(surface)
self.fruit.draw(surface)
for power_pellet in self.power_pellets:
power_pellet.draw(surface)
for pellet in self.pellets:
pellet.draw(surface)
if self.game_state == "run":
self.player.draw_while_running(surface, self.display_width, self.maze, self.tick_counter)
elif self.game_state == "respawn":
if self.temp_counter < 36:
self.player.draw_wedge_pacman(surface, 0 + 10 * self.temp_counter)
self.temp_counter += 1
else:
self.game_state = "run"
self.player.x = spawn_x * block_size + block_size / 2
self.player.y = spawn_y * block_size + block_size / 2
self.look_dir = DOWN
self.humanInput = DOWN
self.move_dir = DOWN
self.try_move_dir = DOWN
self.lastMoveDir = DOWN
if(forceStuck and neatMode):
self.player.x=(spawn_x-5)* block_size + block_size / 2
self.player.y=house_y* block_size + block_size / 2
self.player.move_dir = UP
self.player.look_dir = UP
self.player.try_move_dir = UP
self.player.humanInput = UP
self.lastMoveDir = UP
self.player.draw_while_running(surface, self.display_width, self.maze, self.tick_counter)
for ghost in self.ghosts.values():
if(ghost.mode != "house"):
ghost.x = house_x* block_size + block_size / 2
ghost.y = house_y* block_size + block_size / 2
for ghost in self.ghosts.values():
ghost.draw(surface, self.player, self.tick_counter)
game_font = pygame.freetype.SysFont("Helvetica.ttf", 40)
game_font.render_to(surface, (15, 15), "SCORE: " + str(int(self.score)), (255, 255, 255))
game_font = pygame.freetype.SysFont("Helvetica.ttf", 20)
game_font.render_to(surface, (400, 15), str(self.player.lives) + " LIVES", (255, 255, 255))
game_font.render_to(surface, (600, 15), "FRUIT: ", (255, 255, 255))
#scaling code from https://stackoverflow.com/questions/43196126/how-do-you-scale-a-design-resolution-to-other-resolutions-with-pygame
frame = pygame.transform.scale(surface, (self.display_width*scaling_factor, self.display_height*scaling_factor))
window.blit(frame, frame.get_rect())
#resets the game
#if hard is false, we are just moving to a new level
#if hard is true, its a true reset
def reset(self, hard = False, newMap = False,coinFlip = False):
net = self.player.net
newlives = self.player.lives
if(coinFlip): self.coinFlip = (random.choice([0,1]) == 1)
#hard reset
if(hard):
newlives = 2
if(neatMode): newlives = neatLives
self.last_life_score = 0
self.score = 0
self.level = 0
#soft reset
else:
self.level += 1
if(neatMode and self.current_generation%disableGhostsEvery ==1 and turnOffGhosts): newlives= 0
self.framesUntilOutOfTime = scoreTimeConstraint
#make a new map
if newMap:
self.maze = Maze(self.maze_width, self.maze_height) #regen maze
self.collected_pellets = 0
self.temp_counter = 0
self.player = Pac_Man(spawn_x, spawn_y,pacmanController,self.current_generation)
self.player.net = net
self.player.lives = newlives
# generate all pellets and power pellets
self.power_pellets = []
for loc in self.maze.power_pellet_locs:
self.power_pellets.append(PowerPellet(loc[0], loc[1],self.current_generation))
self.pellets = []
for loc in self.maze.pellet_locs:
self.pellets.append(Pellet(loc[0], loc[1],(neatMode and sparseMode and self.coinFlip)))
self.ghosts = {}
# spawn ghosts
self.ghosts["blinky"] = Ghost(house_x, house_y-2, (255, 80, 80), [house_x+7, house_y-7], "shadow",self.current_generation)
self.ghosts["pinky"] = Ghost(house_x-1, house_y, (255, 100, 150), [house_x-7, house_y-7], "speedy",self.current_generation)
self.ghosts["inky"] = Ghost(house_x, house_y, (100, 255, 255), [house_x+7, house_y+9], "bashful",self.current_generation)
self.ghosts["clyde"] = Ghost(house_x+1, house_y, (255, 200, 000), [house_x-7, house_y+9], "pokey",self.current_generation)
self.ghosts["blinky"].mode = "normal"
self.ghosts["pinky"].mode = "normal"
# spawn fruit
self.num_fruit = 0
self.display_fruit = Fruit(23, -2, fruit_scores[self.level % 8], pygame.image.load(fruit_images[self.level % 8]), True)
self.fruit = Fruit(spawn_x, spawn_y, fruit_scores[self.level % 8], pygame.image.load(fruit_images[self.level % 8]), False)
#main loop of the game
def game_loop(self):
while self.running:
if self.game_state in ("run", "respawn"):
# main game loop
self.events(self.player)
self.loop()
self.draw(self.display_surf, self.display)
# check win condition
if self.collected_pellets >= len(self.pellets):
self.game_state = "win"
if((not fastMode) or (self.tick_counter%neatFrameShow == 0)):
pygame.display.flip()
if(showFPS): print("fps:",self.fps_clock.get_fps())
self.fps_clock.tick(self.fps)
self.tick_counter += 1
# What to do when we win/lose
elif self.game_state == "win":
self.reset()
if((main.current_generation%disableGhostsEvery != 1 or not turnOffGhosts) and (main.current_generation%disablePowerPelletsEvery != 0 or not disablePowerPellets)) or not neatMode:print("won level, moving on, score:",self.score)
self.game_state = "run"
elif self.game_state == "lose":
self.running = False
if(neatMode or evaluateModelMode):
self.running = True
self.game_state = "run"
return self.score #update fitness
print("score:",self.score)
def run(self):
# initialize
random.seed()
pygame.init()
pygame.display.set_caption(neatHyperparams["modelName"])
self.display = pygame.display.set_mode((int(self.display_width*scaling_factor), int(self.display_height*scaling_factor)))
self.display_surf = pygame.Surface([self.display_width, self.display_height])
pygame.font.init()
# spawn maze and player
self.maze = Maze(self.maze_width, self.maze_height)
self.player = Pac_Man(spawn_x, spawn_y, pacmanController,self.current_generation)
# generate all pellets and power pellets
self.power_pellets = []
for loc in self.maze.power_pellet_locs:
self.power_pellets.append(PowerPellet(loc[0], loc[1],self.current_generation))
self.pellets = []
for loc in self.maze.pellet_locs:
self.pellets.append(Pellet(loc[0], loc[1],(neatMode and sparseMode and self.coinFlip)))
self.ghosts = {}
# spawn ghosts
self.ghosts["blinky"] = Ghost(house_x, house_y-2, (255, 80, 80), [house_x+7, house_y-7], "shadow",self.current_generation)
self.ghosts["pinky"] = Ghost(house_x-1, house_y, (255, 100, 150), [house_x-7, house_y-7], "speedy",self.current_generation)
self.ghosts["inky"] = Ghost(house_x, house_y, (100, 255, 255), [house_x+7, house_y+9], "bashful",self.current_generation)
self.ghosts["clyde"] = Ghost(house_x+1, house_y, (255, 200, 000), [house_x-7, house_y+9], "pokey",self.current_generation)
self.ghosts["blinky"].mode = "normal"
self.ghosts["pinky"].mode = "normal"
# spawn fruit
self.display_fruit = Fruit(23, -2, fruit_scores[self.level % 8], pygame.image.load(fruit_images[self.level % 8]).convert(), True)
self.fruit = Fruit(spawn_x, spawn_y, fruit_scores[self.level % 8], pygame.image.load(fruit_images[self.level % 8]).convert(), False)
#the gameloop is elsewhere for neat
if (not neatMode and not evaluateModelMode): self.game_loop()
#initialize neat stuff
if neatMode: neatInit(self)
#initialize eval mode
if evaluateModelMode: evaluateModelInit(self)
if __name__ == "__main__":
main = Main()
main.run()