我正在 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
}
只是导致无限递归。