我试图让迷宫游戏的主要机制起作用,但我无法弄清楚这一点。
如果他们碰到矩形,我想让它跳回来,但它似乎没有这样做,你最终卡住了。
这是我的代码:
Texture2D txr;
Rectangle rect;
Vector2 position;
Vector2 velocity;
public player(Texture2D texture)
{
txr = texture;
position = new Vector2(100, 100);
rect = new Rectangle(100, 100, txr.Width, txr.Height);
}
public void update(Rectangle wall, GameTime gt)
{
MoveIfPossible(gt, wall);
KeyboardState keys = Keyboard.GetState();
if (keys.IsKeyDown(Keys.Up))
velocity = new Vector2(0, -1);
else if (keys.IsKeyDown(Keys.Down))
velocity = new Vector2(0, 1);
else if (keys.IsKeyDown(Keys.Left))
velocity = new Vector2(-1, 0);
else if (keys.IsKeyDown(Keys.Right))
velocity = new Vector2(1, 0);
else
velocity = new Vector2(0, 0);
rect.X = (int)position.X;
rect.Y = (int)position.Y;
}
private void UpdatePositionBasedOnMovement(GameTime gameTime)
{
position += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
}
private void MoveIfPossible(GameTime gameTime, Rectangle wall)
{
Vector2 oldPosition = position;
UpdatePositionBasedOnMovement(gameTime);
if (rect.Intersects(wall))
{
position = oldPosition;
}
}
如果有人知道它为什么不起作用,请告诉我!!
我很遗憾地说,这些变化并没有解决玩家陷入困境的事实。如果它有帮助,这是我的其余代码 的Game1
KeyboardState curr_keys;
Rectangle wall_rect;
Texture2D wall;
player player;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// wall
wall_rect = new Rectangle(200, 100, 100, 100);
player = new player(Content.Load<Texture2D>("maze_rect_border"));
wall = Content.Load<Texture2D>("maze_rect_border");
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
curr_keys = Keyboard.GetState();
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (curr_keys.IsKeyDown(Keys.Escape))
Exit();
player.update(wall_rect, gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
spriteBatch.Draw(wall, wall_rect, Color.White);
player.draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
播放器
Texture2D txr;
Rectangle rect;
Vector2 position;
public player(Texture2D texture)
{
txr = texture;
position = new Vector2(100, 100);
rect = new Rectangle(100, 100, txr.Width, txr.Height);
}
public void update(Rectangle wall, GameTime gt)
{
KeyboardState keys = Keyboard.GetState();
Vector2 velocity;
if (keys.IsKeyDown(Keys.Up))
velocity = new Vector2(0, -1);
else if (keys.IsKeyDown(Keys.Down))
velocity = new Vector2(0, 1);
else if (keys.IsKeyDown(Keys.Left))
velocity = new Vector2(-1, 0);
else if (keys.IsKeyDown(Keys.Right))
velocity = new Vector2(1, 0);
else
velocity = new Vector2(0, 0);
Move(gt, wall, velocity);
rect.X = (int)position.X;
rect.Y = (int)position.Y;
}
private void Move(GameTime gameTime, Rectangle wall, Vector2 velocity)
{
Vector2 oldPosition = position;
position += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
if (rect.Intersects(wall))
{
position = oldPosition;
}
}
public void draw(SpriteBatch sb)
{
sb.Draw(txr, position, Color.White);
}
到目前为止,感谢您的帮助,还有其他想法吗?
答案 0 :(得分:0)
我认为问题在于你在初始化之前使用了速度矢量。我不确定会产生什么结果。我认为根本原因是你将速度作为一个类变量,而它确实应该是本地的。
我会将MoveIfPossible改为初始化速度之后。我也会将速度改为局部变量。以下是对更改的摘要以及跟踪我所做的操作的评论。
我也会摆脱UpdatePositionBasedOnMovement方法。在这种情况下,除了模糊你在这种情况下要做的事情之外,我不确定它是否会增加任何内容。
Texture2D txr;
Rectangle rect;
Vector2 position;
//Vector2 velocity;
public player(Texture2D texture)
{
txr = texture;
position = new Vector2(100, 100);
rect = new Rectangle(100, 100, txr.Width, txr.Height);
}
public void update(Rectangle wall, GameTime gt)
{
//MoveIfPossible(gt, wall); // the velocity vector was not set when this was being called
KeyboardState keys = Keyboard.GetState();
Vector2 velocity; // Change this to local
if (keys.IsKeyDown(Keys.Up))
velocity = new Vector2(0, -1);
else if (keys.IsKeyDown(Keys.Down))
velocity = new Vector2(0, 1);
else if (keys.IsKeyDown(Keys.Left))
velocity = new Vector2(-1, 0);
else if (keys.IsKeyDown(Keys.Right))
velocity = new Vector2(1, 0);
else
velocity = new Vector2(0, 0);
MoveIfPossible(gt, wall, velocity); // Pass vector as a parameter, if it's not needed elsewhere, keep it local
rect.X = (int)position.X;
rect.Y = (int)position.Y;
}
// private void UpdatePositionBasedOnMovement(GameTime gameTime)
// {
// position += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
// }
private void MoveIfPossible(GameTime gameTime, Rectangle wall, Vector2 velocity)
{
Vector2 oldPosition = position;
//UpdatePositionBasedOnMovement(gameTime); // Don't think this is adding anything with just one line of code
position += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
if (rect.Intersects(wall))
{
position = oldPosition;
}
}
清理并删除所有评论,我会以这种方式写/命名:
Texture2D txr;
Rectangle rect;
Vector2 position;
public player(Texture2D texture)
{
txr = texture;
position = new Vector2(100, 100);
rect = new Rectangle(100, 100, txr.Width, txr.Height);
}
public void update(Rectangle wall, GameTime gt)
{
KeyboardState keys = Keyboard.GetState();
Vector2 velocity;
if (keys.IsKeyDown(Keys.Up))
velocity = new Vector2(0, -1);
else if (keys.IsKeyDown(Keys.Down))
velocity = new Vector2(0, 1);
else if (keys.IsKeyDown(Keys.Left))
velocity = new Vector2(-1, 0);
else if (keys.IsKeyDown(Keys.Right))
velocity = new Vector2(1, 0);
else
velocity = new Vector2(0, 0);
Move(gt, wall, velocity);
rect.X = (int)position.X;
rect.Y = (int)position.Y;
}
private void Move(GameTime gameTime, Rectangle wall, Vector2 velocity)
{
Vector2 oldPosition = position;
position += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 4;
if (rect.Intersects(wall))
{
position = oldPosition;
}
}
我猜这不是所有的代码。我的直觉告诉我,将Vector2位置移动到本地也会更好,虽然没有深入了解rect.Intersects()方法我没有足够的信息可以肯定地说。或者更可能看起来它应该是矩形类本身的属性。
一般来说,我默认不使用类变量。首先要做的不是创建类变量。如果找到原因,您可以随时返回并更改它,但局部变量可以简化代码。简单的代码有更少的错误。