def main(): screen = pygame.display.set_mode( (MAP_WIDTH * BLOCK_SIZE, MAP_HEIGHT * BLOCK_SIZE)) pygame.display.set_caption("Pacman") clock = pygame.time.Clock() with open(os.path.join(ASSETS_DIR, "map.txt"), "r") as f: pacman_map = f.read().splitlines() create_map(pacman_map) all_sprite = pygame.sprite.Group() player = Pac(pacman_map, (15, 9), all_sprite) ghost_blinky = Ghost(pacman_map, (10, 8), all_sprite, texture="blinky.png", name="Blinky") ghost_pinky = Ghost(pacman_map, (10, 8), all_sprite, texture="pinky.png", name="Pinky") # ghost_inky = Ghost(pacman_map, (15, 9), all_sprite) # ghost_clyde = Ghost(pacman_map, (15, 9), all_sprite) search = Search().set_map(pacman_map) print(search.map) all_sprite.draw(screen) pygame.display.flip() while running: clock.tick(FPS) all_sprite.update() screen.fill((0, 0, 0)) draw_map(screen) all_sprite.draw(screen) for event in pygame.event.get(): if event.type == pygame.QUIT: quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: player.ch_dir(1) elif event.key == pygame.K_DOWN: player.ch_dir(2) elif event.key == pygame.K_LEFT: player.ch_dir(3) elif event.key == pygame.K_RIGHT: player.ch_dir(4) pygame.display.flip() # TODO: Update Ghost's target # player also known as wall to the ghosts 'cos they can't go through the player, # But they can cross the other ghosts. player_old_pos = player.get_old_pos() player_pos = player.get_pos() search.update_point(player_old_pos[0], player_old_pos[1], False) search.update_point(player_pos[0], player_pos[1], True) """ Blinky will simply trace the player in the shortest way. pinky will aim to 2 blocks in front of player. the AI of Inky is complicated. first, let Point A = the point which 2 block in front of player. Then draw line from Blinky to A, extending double length, the position is the target that Inky is aiming to. """ b_x, b_y = search.search(ghost_pinky.get_pos(), player_pos) ghost_blinky.ch_dir_by_new_xy(b_x, b_y) # get the target of pinky pinky_target = (0, 0) for i in range(3): if ghost_pinky.is_road(player.get_next_pos(i)): pinky_target = player.get_next_pos(i) p_x, p_y = search.search(ghost_pinky.get_pos(), pinky_target) ghost_pinky.ch_dir_by_new_xy(p_x, p_y)