我需要一些帮助,弄清楚如何让救护车绕过障碍而不是经过它。这就是我到目前为止...... 我的计划是做一个赛车游戏,红色的盒子代表我的车,我正在试图弄清楚它是如何制作的,所以它不会与矩形相撞,所以他们不能只是作弊并越过终点线,他们必须通过赛道。这就是我想做的事情
https://i.stack.imgur.com/apgBf.png
到目前为止,我只有汽车和其中一个矩形,但不能让它不跨越矩形。
我正努力让救护车不仅可以越过那个障碍,而且必须绕过它,我想制作一个赛道,一个赛车游戏但是卡住了
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace FinalProject
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
int count;
int speed = 5;
Texture2D background, carUp, carDown, carRight, carLeft, obstacle;
Rectangle backgroundRec, carRec, carRecDown, carRecUp, carRecLeft, carRecRight, carRec1, obstacleRec;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
base.Initialize();
carRec = new Rectangle(500, 300, 100, 100);
carRecDown = new Rectangle(500, (300 + speed), 100, 100);
carRecLeft = new Rectangle((500 - speed), 300, 100, 100);
carRecUp = new Rectangle(500, (300 - speed), 100, 100);
carRecRight = new Rectangle((500 + speed), 300, 100, 100);
backgroundRec = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
obstacleRec = new Rectangle(100, 240, 100, 300);
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
carUp = Content.Load<Texture2D>("up");
carDown = Content.Load<Texture2D>("down");
carRight = Content.Load<Texture2D>("right");
carLeft = Content.Load<Texture2D>("left");
background = Content.Load<Texture2D>("background");
obstacle = Content.Load<Texture2D>("obstacle");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
KeyboardState keys = Keyboard.GetState();
GamePadState pad1 = GamePad.GetState(PlayerIndex.One);
carRecDown.Y = carRec.Y - speed;
carRecLeft.X = carRec.X - speed;
carRecRight.X = carRec.X + speed;
carRecUp.Y = carRec.X + speed;
// Keyboard
if (keys.IsKeyDown(Keys.Up) && !(obstacleRec.Intersects(carRecUp)))
{
count = 1;
carRec.Y -= speed;
}
if (keys.IsKeyDown(Keys.Down) && !(obstacleRec.Intersects(carRecDown)))
{
count = 2;
carRec.Y += speed;
}
if (keys.IsKeyDown(Keys.Right) && !(obstacleRec.Intersects(carRecRight)))
{
count = 3;
carRec.X += speed;
}
if (keys.IsKeyDown(Keys.Left) && !(obstacleRec.Intersects(carRecLeft)))
{
count = 4;
carRec.X -= speed;
}
/* if (obstacleRec.Intersects(carRec))
{
carRec.X +=1 ;
}*/
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(background, backgroundRec, Color.White);
spriteBatch.Draw(obstacle, obstacleRec, Color.White);
if (count == 0)
spriteBatch.Draw(carUp, carRec, Color.White);
if (count == 1)
spriteBatch.Draw(carUp, carRec, Color.White);
if (count == 2)
spriteBatch.Draw(carDown, carRec, Color.White);
if (count == 3)
spriteBatch.Draw(carRight, carRec, Color.White);
if (count == 4)
spriteBatch.Draw(carLeft, carRec, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
答案 0 :(得分:0)
如果你想做路径寻找,你应该创建一个代表你的赛道使用BFS或Djikstra算法的图表。
对于碰撞,请阅读有关基本碰撞检测方法的内容。如果您想进行非常基本的碰撞检测,只需计算汽车的下一个位置两次,一次计算每个轴上的移动,然后检查这些创建的位置是否与地图发生碰撞。如果是这样,只要阻止汽车在此轴上移动。
这是一个简短的伪代码:
Vector2 movement = ...
Rectangle nextPosX = ....
Rectangle nextPosY = ....
if (IntersectingWithMap(nextPosX))
movement.x = 0
if (IntersectingWithMap(nextPosY))
movement.y = 0
currPos += movement;