我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
我发现在《吃豆人》中有很多关于幽灵AI的参考,但没有一个提到当幽灵被《吃豆人》吃掉后,眼睛是如何找到中央幽灵洞的。
在我的实现中,我实现了一个简单但糟糕的解决方案。我只是在每个角落都用硬编码标明了应该往哪个方向走。
有没有更好的/最好的解决办法?也许是适用于不同关卡设计的通用设计?
当前回答
这是一个寻径问题。有关流行的算法,请参见http://wiki.gamedev.net/index.php/A*。
其他回答
任何简单、可维护、可靠、性能足够好的解决方案都是好的解决方案。听起来你已经找到了一个很好的解决办法。
寻径解决方案可能比当前解决方案更复杂,因此更可能需要调试。它可能也会变慢。
在我看来,如果它没坏,就不要修。
EDIT
在我看来,如果迷宫是固定的,那么你当前的解决方案就是好的/优雅的代码。不要错误地把“好”或“优雅”等同于“聪明”。简单的代码也可以是“好的”和“优雅的”。
如果你有可配置的迷宫级别,那么也许你应该在最初配置迷宫时进行寻路。最简单的方法就是让迷宫设计师亲自动手。如果你有无数个迷宫,我才会费心自动化这个……或者用户可以自行设计。
(另外:如果路线是手工配置的,那么迷宫设计师可以通过使用次优路线来让关卡变得更有趣……)
Dtb23的建议是在每个角落随机选择一个方向,最终你会发现怪物洞听起来非常低效。
然而,你可以利用它低效的“回家”算法,通过在游戏难度中引入更多变化来让游戏变得更有趣。你可以通过应用上面的方法,比如你的路径点或洪水填充来做到这一点,但这样做是非确定性的。所以在每个角落,你都可以生成一个随机数来决定是走最优路线,还是随机方向。
随着玩家不断推进关卡,你将减少玩家选择随机方向的可能性。这将在关卡速度,幽灵速度,吃药丸暂停等之外为整体难度关卡添加另一个杠杆。你有更多的时间放松,而鬼魂只是无害的眼睛,但随着你的进步,时间会越来越短。
我不太清楚你是如何执行游戏的,但你可以这么做:
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
在游戏开始前保存地图上的节点(交叉点) 当怪物死亡时,取点(坐标)并找到 节点列表中最近的节点 计算从该节点到洞的所有路径 按长度取最短路径 将该点与最近节点之间的空间长度相加 绘制并在路径上移动
享受吧!
我的方法有点内存密集型(从《吃豆人》时代的角度来看),但你只需要计算一次,它适用于任何关卡设计(包括跳跃)。
一次标记节点
当你第一次加载一个关卡时,将所有怪物巢穴节点标记为0(代表与巢穴的距离)。继续向外标记已连接的节点1,连接到它们的节点2,依此类推,直到所有节点都被标记。(注意:如果巢穴有多个入口,这也是有效的)
我假设您已经有了表示每个节点和到它们的邻居的连接的对象。伪代码可能看起来像这样:
public void fillMap(List<Node> nodes) { // call passing lairNodes
int i = 0;
while(nodes.count > 0) {
// Label with distance from lair
nodes.labelAll(i++);
// Find connected unlabelled nodes
nodes = nodes
.flatMap(n -> n.neighbours)
.filter(!n.isDistanceAssigned());
}
}
眼睛移动到距离标签最小的邻居
一旦所有节点都标记好了,路由眼睛就变得很简单了……只需要选择距离标签最小的相邻节点(注意:如果多个节点的距离相等,那么选择哪个节点并不重要)。伪代码:
public Node moveEyes(final Node current) {
return current.neighbours.min((n1, n2) -> n1.distance - n2.distance);
}
全标记示例