寻路在Java 2D游戏? [英] Path finding in a Java 2d Game?

查看:263
本文介绍了寻路在Java 2D游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本质上讲它是一种Pa​​cman的克隆游戏我工作。我有一个敌人类,这个类创建所有重新present 4鬼怪游戏的4个实例。

Essentially its a pacman clone game I'm working on. I have an Enemy class, and 4 instances of this class created which all represent 4 ghosts of the game.

所有的鬼魂开始在屏幕的任意区域,然后他们必须工作他们的方式对吃豆子字符。作为玩家控制着吃豆子,移动它,他们应该遵循它,并采取了最接近的方式向他。

All ghosts start up in random areas of the screen and then they have to work their way towards the pacman character. As the player controls the pacman, moving it around, they should follow it and take the nearest possible way towards him.

有没有迷宫/障碍(还),因此整个地图(400×400像素)的空地给他们。

There is no maze/obstacles (yet) so the entire map (400x400 pixels) is open ground to them.

有关球员和每一个鬼,我可以检索的X,Y,图像的宽度和高度属性。此外,我已经有一个碰撞检测算法,所以并不担心,只是鬼找到自己的方式吃豆子。

For the player and each Ghost, i can retrieve the X, Y, image width and height attributes. Also, i already have a collision detection algorithm, so not worried about that, just about the ghosts finding their way to pacman.

推荐答案

对于一个良好的寻路算法,使用 A * 可能会是一个不错的主意,但是,对于一个简单的游戏,并不需要复杂的,高效的,也不是有效路径搜索,只要有通过找出目标的方向人物走向目标应是足够的。

For a good pathfinding algorithm, using A* would probably be a good idea, however, for a simple game that doesn't require sophisticated, efficient, nor effective path searching, simply having the characters move toward a target by finding out the direction of the target should be sufficient.

例如,该决定使人物移动,在伪code:

For example, the decision to make the character move, in pseudocode:

if (target is to the left of me):
    move(left);
else
    move(right);

if (target is above me):
    move(up);
else
    move(down);

是的,性格是不会做出最有效的运动,但它会得到越来越接近目标的游戏循环的每个迭代。

Yes, the character is not going to make the most efficient movement, but it will get closer and closer to the target on each iteration of the game loop.

这也是我的猜测,从80年代初可能是一个街机游戏不会使用复杂的寻路算法。

It's also my guess that an arcade game from the early 80's probably wouldn't be using sophisticated pathfinding algorithms.

这篇关于寻路在Java 2D游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆