【问题标题】:Pacman Game - how to get pacman to move automatically吃豆人游戏 - 如何让吃豆人自动移动
【发布时间】:2015-05-02 11:22:24
【问题描述】:

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

这是

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

抱歉,如果我的问题难以理解,但我相信这是一个相当简单的解释。

提前谢谢你。

【问题讨论】:

  • 添加私有变量来保持 pacman 的速度 - 水平和垂直。添加一个线程/计时器,根据它的速度移动 pacman。更改您的处理程序以修改 pacman 的速度,而不是位置。
  • 干杯感谢您的意见

标签: c# keypress pacman


【解决方案1】:

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

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 的私有变量中设置方向。

【讨论】:

  • 谢谢您的帮助!我会试一试
猜你喜欢
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 2022-08-15
  • 1970-01-01
  • 2011-02-28
  • 2019-08-16
相关资源
最近更新 更多