Pac-Man
You need to program the Pac-Man game, and its screenshot is shown in
Figure 1. The player controls Pac-Man, who must eat all the dots inside an
enclosed maze while avoiding four colored ghosts. Eating large flashing
dots called “Power Pellets” causes the ghosts to temporarily turn blue,
allowing Pac-Man to eat them for bonus points.
Yor can search Pac-Man and try to play the game first.
运行截图

算法简述
目标是避开鬼吃掉所有豆子
每走一步循环如下语句:
- 若没有目标豆子, 选择一个曼哈顿距离最近的豆子作为目标豆子
- bfs寻找前往目标豆子最近的路, 不能吃的鬼视作障碍物
- 根据最短路径的选择前进方向前进
例如:
初始吃豆人坐标为 start: (14, 15)
目标豆子坐标为 focus: [11, 15]
计算出的最短路径为 result: [(14, 15), (13, 15), (12, 15), (11, 15)]
则更换吃豆人方向为向上
走了一步后,
start: (13, 15)
focus: [11, 15]
result: [(13, 15), (12, 15), (11, 15)]
吃豆人方向为向上, 继续前进
算法AI代码如下:
class AI:
def __init__(self):
self.focus_pac = None
self.pacman_location = None
self.first = True
def action(self):
self.pacman_location = [int(game.pacman.row), int(game.pacman.col)]
# 如果没有focus, 寻找一个豆子为目标
if not self.focus_pac:
self.focus_pac = self.get_nearst_pac()
# 判断focus的豆子被吃没有, 被吃后换新的豆子
if gameBoard[self.focus_pac[0]][self.focus_pac[1]] == 1:
self.focus_pac = self.get_nearst_pac()
# 规划前往focus的路线
global visited, path, result
m, n = len(gameBoard), len(gameBoard[

本文介绍了如何编程实现Pac-Man游戏AI,使用BFS算法规划吃豆人避开鬼怪的路径,确保吃到所有豆子并利用PowerPellets战术。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



