1

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

这是

   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 是他被允许在地图内继续移动的唯一单元格。这些单元格正在文本文件中绘制。

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

先感谢您。

4

1 回答 1

1

不要在按键期间移动 Pac-Man,而是使用按键设置方向,并将 Pac-Man 移到按键逻辑之外

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 的评论所建议的,您需要在 Pac-Man 的私有变量中设置方向。

于 2014-03-29T01:38:38.060 回答