r = min(r + 1, n - 2) mat[pac[0]][pac[1]] = ' ' for i, j in ghost: mat[i][j] = ' ' pac1 = pac pac = (r, c) ghost = ghost1 ghost1, time, game, over = eat(pac, time, ghost1) if game == False: if epi > num - 5: visualPac.display(n, m, mat, pac, ghost1, time, f) lose(over, score) break if delay % 2: if time: ghost1, dg, df = pacmanBFS.BFS(mat, ghost, n, m, pac, f) ghost1 = escape.esc(mat, pac, n, m, ghost) else: ghost1, dg, df = pacmanBFS.BFS(mat, ghost, n, m, pac, f) delay = (delay + 1) % 2 ghost1, time, game, over = eat(pac, time, ghost1) if epi > num - 5: visualPac.display(n, m, mat, pac, ghost1, time, f) lose(over, score) time = max(0, time - 1) score += 1 if len(f) == 0: f = forig move = train.Qlearning(pac, pac1, ghost, ghost1, len(forig), f, over, dg, df, time)
move = 3 termios.tcflush(sys.stdin, termios.TCIFLUSH) if move == 0 and valid(pac[0],pac[1]-1,n,m,mat): c = max(c-1,1) elif move == 1 and valid(pac[0]-1,pac[1],n,m,mat): r = max(r-1,1) elif move == 2 and valid(pac[0],pac[1]+1,n,m,mat): c = min(c+1,m-2) elif move == 3 and valid(pac[0]+1,pac[1],n,m,mat): r = min(r+1,n-2) mat[pac[0]][pac[1]] = ' ' for i,j in ghost: mat[i][j] = ' ' pac = (r,c) game,time,win = eat(pac,time) if game == False: visualPac.display(n,m,mat,pac,ghost,time,f) lose(win,score) break if delay % 2: if time: ghost = escape.esc(mat,pac,n,m,ghost) else: ghost = pacmanBFS.BFS(mat,ghost,n,m,pac) delay = (delay + 1)%2 visualPac.display(n,m,mat,pac,ghost,time,f) game,time,win = eat(pac,time) lose(win,score) score += 1 time = max(0,time-1)
def main(): G = Game(40) Win = window(G.row, G.col) game = True direct = 0 move = -1 delay = 2 r, c = G.pac even = 0 Win.screen.blit(Win.store['start'], (0, 0)) pygame.display.flip() Win.clock.tick(0.5) while game: #Win.display(G.matrix, G.time, direct, even, G.pac, G.ghost) for event in pygame.event.get(): if event.type == pygame.QUIT: game = False pressed = pygame.key.get_pressed() if pressed[pygame.K_UP] and G.valid(G.pac[0] - 1, G.pac[1]): move = 0 elif pressed[pygame.K_DOWN] and G.valid(G.pac[0] + 1, G.pac[1]): move = 1 elif pressed[pygame.K_LEFT] and G.valid(G.pac[0], G.pac[1] - 1): move = 2 direct = 0 elif pressed[pygame.K_RIGHT] and G.valid(G.pac[0], G.pac[1] + 1): move = 3 direct = 1 if move == 0 and G.valid(G.pac[0] - 1, G.pac[1]): r = max(r - 1, 1) elif move == 1 and G.valid(G.pac[0] + 1, G.pac[1]): r = min(r + 1, G.row - 2) elif move == 2 and G.valid(G.pac[0], G.pac[1] - 1): c = max(c - 1, 1) elif move == 3 and G.valid(G.pac[0], G.pac[1] + 1): c = min(c + 1, G.col - 2) G.matrix[G.pac[0]][G.pac[1]] = ' ' for i, j in G.ghost: G.matrix[i][j] = ' ' G.pac = (r, c) G.eat() if G.over == True: Win.display(G.matrix, G.time, direct, even, G.pac, G.ghost, G.fruit) break if delay % 2: if G.time: G.ghost = escape.BFS(G.matrix, G.ghost, G.row, G.col, G.pac) else: G.ghost = pacmanBFS.BFS(G.matrix, G.ghost, G.row, G.col, G.pac) delay = (delay + 1) % 2 even = (even + 1) % 2 Win.display(G.matrix, G.time, direct, even, G.pac, G.ghost, G.fruit) G.eat() if G.over == True: Win.display(G.matrix, G.time, direct, even, G.pac, G.ghost, G.fruit) break if len(G.fruit) == 0: G.fruit = G.fruitOrg G.score += 1 G.time = max(0, G.time - 1) Win.clock.tick(6) pygame.display.flip() if G.win == False: Win.screen.blit(Win.store['game_over'], ((G.col // 2 - 2) * block_size, (G.row // 2 - 1) * block_size)) else: Win.screen.blit(Win.store['win'], ((G.col // 2 - 2) * block_size, (G.row // 2 - 1) * block_size)) pygame.display.flip() Win.clock.tick(1)