C# 记住一个方向,移动到一个点,然后改变方向。新华社
我正在XNA制作一个吃豆人克隆。 到目前为止,我已经使用2D数组绘制了平铺图,使用另一个2D数组添加了药丸,并制作了一个允许pacman移动的2D数组 在实际的游戏中,你可以在向上移动的同时按下向右键,直到你能够向右移动和转弯为止 我有一个系统,只有当精灵位置%32=16时才允许转弯。 这意味着精灵将在墙之间居中 我需要程序记住最后一次按下的键,或者在转弯前移动到正确的位置,但我找不到方法。 下面是一些代码,介绍了我正在尝试的内容C# 记住一个方向,移动到一个点,然后改变方向。新华社,c#,xna,game-physics,pacman,C#,Xna,Game Physics,Pacman,我正在XNA制作一个吃豆人克隆。 到目前为止,我已经使用2D数组绘制了平铺图,使用另一个2D数组添加了药丸,并制作了一个允许pacman移动的2D数组 在实际的游戏中,你可以在向上移动的同时按下向右键,直到你能够向右移动和转弯为止 我有一个系统,只有当精灵位置%32=16时才允许转弯。 这意味着精灵将在墙之间居中 我需要程序记住最后一次按下的键,或者在转弯前移动到正确的位置,但我找不到方法。 下面是一些代码,介绍了我正在尝试的内容 public void MovementCheck()
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;
}
}
仅显示右键,其他键类似,但方向都会更改,对平铺贴图的检查也会相应更改。(+1位于此处的X上)
我试过这样的事情
while (spritePosition.Y % 32 != 16)
{ spritePosition = spriteVelocity + spritePosition; }
但这只会让精灵在屏幕上出现,(有点明显):(
在调用Right()之前,我尝试了一个新方法
只有一个计数器会导致无限递归。一个解决方案是添加一个
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();
}
}
}
公共类GameClassWhereUpdatesDone
{
私有枚举方向{None,Up,Down,Left,Right};
专用整数计数器=0;
私有方向currentDirection;//当前移动方向
私有方向lastInput;//输入的最后一个方向
公共无效更新(…)
{
var keyboardState=Keyboard.GetState();
if(keyboardState.IsKeyPressed(按键右侧))
{
计数器=0;
方向=方向。对;
}
if(currentDirection!=lastInput&&counter<5)//允许提前5次更新。
{
//玩家想转身吗
if(允许旋转(上次输入)
{
currentDirection=lastInput;
}
}
移动方向(当前方向);
计数器++;
}
允许私家车转弯(方向)
{
如果(方向==方向.右)
{
返回RightCheck();
}
}
}
关键思想是跟踪运动方向和输入的最后方向
在原始的Pac Man“预转向”中实际上是使用的,这意味着如果你在拐角前转弯,你将开始沿对角线移动,根据:这是一个有趣的读数。谢谢你的回答,我刚刚重新检查了这一页。我已经看过《吃豆人档案》,是的,这是一个非常有趣的读数。我将尝试一下,看看结果如何。
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();
}
}
}