while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() pygame.display.flip() tg = Game() def turn_timer(): tg.update() threading.Timer(0.1, turn_timer).start() turn_timer(); while not ended: time.sleep(1) move = ri(1,4); pacman_x2, pacman_y2, ghost_x2, ghost_y2, goal_x, goal_y, ended, won, moved = g.game_func(move, pacman_x, pacman_y, ghost_x, ghost_y, goal_x, goal_y) if moved: moves.append(move) pacman[pacman_y][pacman_x]='False' ghost[ghost_y][ghost_x]='False' pacman_x, pacman_y, ghost_x, ghost_y = pacman_x2, pacman_y2, ghost_x2, ghost_y2 pacman[pacman_y][pacman_x]='True' ghost[ghost_y][ghost_x]='True' print(moves)
## Start game while not game.ended: game.update() # update graphics time.sleep(0.04) # Get state (0 to numStates-1) from pacman and ghost coordinates state = coord2state(game.pacman_x, game.pacman_y, game.ghost_x, game.ghost_y, num_ghosts, grid_len) # My policy[state] outputs 0 to 3, that's why I add 1, because Steven's actions go 1 to 4 move = policy[state] + 1 steps_to_win += 1 # Get game status given current state and move (action) pacman_x2, pacman_y2, ghost_x2, ghost_y2, game.goal_x, game.goal_y, game.ended, game.won = g.game_func( move, game.pacman_x, game.pacman_y, game.ghost_x, game.ghost_y, game.goal_x, game.goal_y, game.grid, 1, ghostType) game.moves.append(move) game.updateState(pacman_x2, pacman_y2, ghost_x2, ghost_y2, num_ghosts, ghostType) # update internal grid # Update one last time before game end game.update() if (game.won): win_count += 1 winning_steps.append( steps_to_win) # does not add to list if game is lost print('Total # of wins: ', win_count) print(winning_steps)