【发布时间】:2017-03-30 17:56:27
【问题描述】:
我试图让迷宫游戏的主要机制正常工作,但我无法弄清楚。
如果角色碰到矩形,我希望它跳回来,但它似乎没有这样做,你最终会卡住。
这是我的代码:
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;
}
}
如果有人知道它为什么不起作用,请告诉我!!
很抱歉,这些改动并没有解决玩家卡在墙上的问题。如果有帮助,这是我的其余代码 游戏1
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);
}
感谢您迄今为止的帮助,还有其他想法吗?
【问题讨论】:
-
当您与墙相交时,您将返回与开始时相同的位置。向后移动将速度乘以 -1 并移动。