0

我正在 XNA 中制作一个 pacman 克隆。到目前为止,我已经使用 2D 数组绘制了瓦片地图,使用另一个 2D 数组添加了药丸,并制作了一个允许 pacman 移动的 2D 数组。

在实际游戏中,您可以在向上移动的同时按右,它会等到您能够向右移动和转弯。

我有一个系统,仅当 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
 }

只是导致无限递归。

4

1 回答 1

0

一种解决方案是在每帧/时间步骤中添加一个int counter = 0;您在游戏循环中更新的(使用)。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();
       }
    }
}

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

在最初的 Pac-Man 中,实际使用了“预转弯”,这意味着如果您在拐角前转弯,您将开始沿对角线移动,根据:http ://home.comcast.net/~jpittman2/pacman/pacmandossier.html其中读起来很有趣。

于 2015-02-25T12:21:54.973 回答