记住一个方向,移动到一个点,然后改变方向。新华社

Remembering a direction, moving to a point, then changing direction. XNA

我正在 XNA 中制作 pacman 克隆。 到目前为止,我已经使用二维数组绘制了瓷砖地图,使用另一个二维数组添加了药丸,并制作了一个允许吃豆子移动的二维数组。

实际游戏中可以在上移的时候按右,等到可以右移的时候再转弯。

我有一个系统,仅当 spritePosition % 32 = 16 时才允许转弯。 这意味着精灵将在墙壁之间居中。

我需要程序记住上次按下的键或在转动前移动到正确的位置,但我找不到实现它的方法。 这是涵盖我正在尝试的内容的一些代码。

public void MovementCheck()
        {
            presentKey = Keyboard.GetState();
            spritePosition = spriteVelocity + spritePosition;
            pacManRec = new Rectangle((int)spritePosition.X,     (int)spritePosition.Y, pacManTex.Width, pacManTex.Height);
            spriteOrigin = new Vector2(pacManRec.Width / 2, pacManRec.Height / 2);

        //Press Right
        if (presentKey.IsKeyDown(Keys.Right) && pastKey.IsKeyUp(Keys.Right))
        {

            Right();
        }
}

private void Right()
        {
            direction = "right"; 
//if the next block in tile map to the right is a 1, and the sprite is centred - allow a turn
            if (inputMap[(int)Math.Floor(spritePosition.Y / 32), (int)Math.Floor(spritePosition.X / 32) + 1] == 1 && (spritePosition.Y % 32 == 16))
            {
                rotation = ((float)Math.PI / 180);
                spriteVelocity.X = 0;
                spriteVelocity.Y = 0;
                spriteVelocity.X = movementSpeed;
            }
        }

只显示右键,其他类似,但方向都变了,对瓦片地图的检查也相应改变了。 (此处的 X 为 +1)

我试过

while (spritePosition.Y % 32 != 16)
{ spritePosition = spriteVelocity + spritePosition; }

但这只会让精灵弹射到屏幕上,(有点明显):(

我在 Right() 调用之前尝试了一种新方法

bool RightCheck()
{
    if ( CONDITIONS MET HERE )
    return true
    else
    { 
      //dont remember if I used this line, but something similar
      spritePosition = spriteVelocity + spritePosition;
      RightCheck()
    }
    return false; //allows program to build
 }

只是一个导致无限递归。

一个解决方案是添加一个 int counter = 0;,您每隔 frame/time-step 在您的游戏循环中更新(使用 counter++;)。每次进行有效输入并保存该输入时设置为 0。

大纲代码:

public class GameClassWhereUpdateIsDone
{
    private enum Directions { None, Up, Down, Left, Right };

    private int counter = 0;
    private Directions currentDirection; // Current movement-direction
    private Directions lastInput; // Last direction from input

    public void Update(...)
    {
       var keyboardState = Keyboard.GetState();
       if(keyboardState.IsKeyPressed(Keys.Right))
       {
           counter = 0;
           direction = Directions.Right;
       }

       if(currentDirection != lastInput && counter < 5) // Allow turning 5 updates ahead.
       {
           // Player want to turn
           if(AllowedToTurn(lastInput)
           {
                currentDirection = lastInput;
           }
       }

       MoveDirection(currentDirection);

       counter++;
    }

    private bool AllowedToTurn(Directions direction)
    {
       if(direction == Directions.Right)
       {
           return RightCheck();
       }
    }
}

关键思想是跟踪移动方向和最后输入的方向...

在原来的吃豆人中 "pre-turning" 被实际使用,这意味着如果你在一个角落前面转弯,你将开始沿对角线移动:http://home.comcast.net/~jpittman2/pacman/pacmandossier.html 这是一个有趣的读物。