简体   繁体   English

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

[英]Pacman Game - how to get pacman to move automatically

I'm making a pacman game and at the moment my pacman is moving within the allowed coordinates of the map when I press the right, left, up or down arrow key. 我正在做一个吃豆人游戏,此刻,当我按向右,向左,向上或向下箭头键时,我的吃豆人正在地图允许的坐标内移动。 It is only moving when I hold the key down. 当我按住键时它才移动。 I was wondering how I could do this so that he moves automatically on the key press until he hits the wall in the map, so that I don't need to hold the arrow down. 我想知道如何做到这一点,以便他在按键时自动移动,直到他撞到地图上的墙为止,这样我就不需要按住箭头了。

This is 这是

   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();

            }

The cell types o, p and d are the only cells he is allowed to move on within the map. 单元格类型o,p和d是允许他在地图上移动的唯一单元格。 These cells are being drawn within a textfile. 这些单元格正在文本文件中绘制。

Sorry if it is hard to understand what I am asking but I am sure it is a fairly simple explanation. 抱歉,如果很难理解我的要求,但我相信这是一个相当简单的解释。

Thank you in advance. 先感谢您。

Instead of moving Pac-Man during the keypress, use the keypress to set a direction, and move Pac-Man outside the keypress logic . 不要在按键过程中移动吃豆人,而是使用按键来设置方向,然后将“吃豆人”移动到按键逻辑之外

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..
}

As BartoszKP's comment recommends, you will want to set the direction in Pac-Man's private variables. 正如BartoszKP的评论所建议的那样,您将需要在吃豆人的私有变量中设置方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM