我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
当前回答
我建议幽灵存储他从洞到吃豆人的路径。所以一旦鬼魂死了,他就可以沿着这条存储路径向相反的方向移动。
其他回答
我不太清楚你是如何执行游戏的,但你可以这么做:
Determine the eyes location relative position to the gate. i.e. Is it left above? Right below? Then move the eyes opposite one of the two directions (such as make it move left if it is right of the gate, and below the gate) and check if there are and walls preventing you from doing so. If there are walls preventing you from doing so then make it move opposite the other direction (for example, if the coordinates of the eyes relative to the pin is right north and it was currently moving left but there is a wall in the way make it move south. Remember to keep checking each time to move to keep checking where the eyes are in relative to the gate and check to see when there is no latitudinal coordinate. i.e. it is only above the gate. In the case it is only above the gate move down if there is a wall, move either left or right and keep doing this number 1 - 4 until the eyes are in the den. I've never seen a dead end in Pacman this code will not account for dead ends. Also, I have included a solution to when the eyes would "wobble" between a wall that spans across the origin in my pseudocode.
一些伪代码:
x = getRelativeOppositeLatitudinalCoord()
y
origX = x
while(eyesNotInPen())
x = getRelativeOppositeLatitudinalCoordofGate()
y = getRelativeOppositeLongitudinalCoordofGate()
if (getRelativeOppositeLatitudinalCoordofGate() == 0 && move(y) == false/*assume zero is neither left or right of the the gate and false means wall is in the way */)
while (move(y) == false)
move(origX)
x = getRelativeOppositeLatitudinalCoordofGate()
else if (move(x) == false) {
move(y)
endWhile
在最初的《吃豆人》中,幽灵会通过“气味”在地图上留下痕迹,鬼魂会随机四处游荡,直到它们找到气味,然后它们会沿着气味路径直接找到玩家。吃豆人每移动一次,“气味值”就会减少1。
现在,一个扭转整个过程的简单方法是建立一个“幽灵气味金字塔”,它的最高点在地图的中心,然后鬼魂就会朝着气味的方向移动。
你应该看看寻径算法,比如Dijsktra算法或a *算法。这就是你的问题所在:图/路径问题。
这是我能找到的关于它是如何工作的最好的资料。
http://gameai.com/wiki/index.php?title=Pac-Man#Respawn 当幽灵被杀死时,它们脱离实体的眼睛会回到最初的位置。这可以通过将幽灵的目标贴图设置到该位置来实现。导航使用相同的规则。
这是有道理的。也许不是世界上最有效的方法但这是一种很好的方法不用担心另一种状态或者沿着这些线你只需要改变目标。
附注:我没有意识到那些吃豆人程序员有多棒,他们基本上是在非常小的空间和非常有限的内存中创建了一个完整的消息系统……太神奇了。
下面是ammoQ的洪水填充想法的模拟和伪代码。
queue q
enqueue q, ghost_origin
set visited
while q has squares
p <= dequeue q
for each square s adjacent to p
if ( s not in visited ) then
add s to visited
s.returndirection <= direction from s to p
enqueue q, s
end if
next
next
它的思想是宽度优先搜索,所以每次你遇到一个新的相邻正方形s,最好的路径是经过p。我相信是O(N)。