我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
当前回答
我建议幽灵存储他从洞到吃豆人的路径。所以一旦鬼魂死了,他就可以沿着这条存储路径向相反的方向移动。
其他回答
在最初的《吃豆人》中,幽灵会通过“气味”在地图上留下痕迹,鬼魂会随机四处游荡,直到它们找到气味,然后它们会沿着气味路径直接找到玩家。吃豆人每移动一次,“气味值”就会减少1。
现在,一个扭转整个过程的简单方法是建立一个“幽灵气味金字塔”,它的最高点在地图的中心,然后鬼魂就会朝着气味的方向移动。
任何简单、可维护、可靠、性能足够好的解决方案都是好的解决方案。听起来你已经找到了一个很好的解决办法。
寻径解决方案可能比当前解决方案更复杂,因此更可能需要调试。它可能也会变慢。
在我看来,如果它没坏,就不要修。
EDIT
在我看来,如果迷宫是固定的,那么你当前的解决方案就是好的/优雅的代码。不要错误地把“好”或“优雅”等同于“聪明”。简单的代码也可以是“好的”和“优雅的”。
如果你有可配置的迷宫级别,那么也许你应该在最初配置迷宫时进行寻路。最简单的方法就是让迷宫设计师亲自动手。如果你有无数个迷宫,我才会费心自动化这个……或者用户可以自行设计。
(另外:如果路线是手工配置的,那么迷宫设计师可以通过使用次优路线来让关卡变得更有趣……)
知道吃豆人的路径是非随机的(例如,每个特定的关卡0-255,inky, blinky, pinky和clyde将在该关卡中工作完全相同的路径)。
我会选择这个,然后猜测有一些主路径围绕整个 迷宫是眼球物体的“返回路径”,当吃豆人吃掉幽灵时,它就在那里。
我不太清楚你是如何执行游戏的,但你可以这么做:
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
对于我的《吃豆人》游戏,我创造了一个“最短多条回家路径”算法,它适用于我所提供的任何迷宫(在我的规则集内)。它也适用于隧道。
当关卡被加载时,每个十字路口的所有归途数据都是空的(默认),一旦幽灵开始探索迷宫,它们每次遇到“新的”十字路口或从不同的路径再次遇到已知的十字路口时,它们的归途路径信息就会不断更新。