吃豆人游戏 - 如何让吃豆人自动移动

本文关键字:移动 游戏 | 更新日期: 2023-09-27 17:55:11

我正在制作一个吃豆人游戏,目前当我按右、左、向上或向下箭头键时,我的吃豆人正在地图允许的坐标内移动。只有当我按住键时,它才会移动。我想知道如何做到这一点,以便他在按键时自动移动,直到他撞到地图中的墙壁,这样我就不需要按住箭头了。

这是

   if (e.KeyCode == Keys.Down)
        {
            if (coordinates[(pac.xPosition + 16) / 20, (pac.yPosition + 20) / 20].CellType == 'o'
                || coordinates[(pac.xPosition + 16) / 20, (pac.yPosition + 20) / 20].CellType == 'd'
                || coordinates[(pac.xPosition + 16) / 20, (pac.yPosition + 20) / 20].CellType == 'p')
            {
               pac.setPacmanImage();
                pac.setPacmanImageDown(currentMouthPosition);
                checkBounds();
            }

单元格类型o,p和d是他被允许在地图中移动的唯一单元格。这些单元格是在文本文件中绘制的。

抱歉,如果很难理解我在问什么,但我相信这是一个相当简单的解释。

提前谢谢你。

吃豆人游戏 - 如何让吃豆人自动移动

不要在按键过程中移动吃豆人,而是使用按键设置方向,并将吃豆人移动到按键逻辑之外

enum Direction {Stopped, Left, Right, Up, Down};
Direction current_dir = Direction.Stopped;
// Check keypress for direction change.
if (e.KeyCode == Keys.Down) {
    current_dir = Direction.Down;
} else if (e.KeyCode == Keys.Up) {
    current_dir = Direction.Up;
} else if (e.KeyCode == Keys.Left) {
    current_dir = Direction.Left;
} else if (e.KeyCode == Keys.Right) {
    current_dir = Direction.Right;
}
// Depending on direction, move Pac-Man.
if (current_dir == Direction.Up) {
    // Move Pac-Man up
} else if (current_dir == Direction.Down) {
    // Move Pac-Man down
} else if (current_dir == Direction.Left) {
    // Move Pac-Man left
} else if (current_dir == Direction.Right) {
    // You get the picture..
}

正如BartoszKP的评论所建议的那样,您需要在吃豆人的私有变量中设置方向。