Ich brauche Hilfe, um herauszufinden, wie ich den Krankenwagen dazu bringen kann, das Hindernis zu umgehen, anstatt es durchzugehen. Das ist, was ich bis jetzt habe ... Mein Plan ist, ein Rennspiel zu machen, die rote Box stellt mein Auto dar und ich versuche herauszufinden, wie man es macht, damit es nicht mit den Rechtecken kollidiert, so dass sie können Schummeln Sie nicht und überqueren Sie die Ziellinie, sie müssen durch die Strecke gehen. Das ist, was ichWie man ein Hindernis auf eine Mauer trifft, nicht durch die Mauer/das Hindernis
https://i.stack.imgur.com/apgBf.png
Bisher habe ich nur das Auto und einer der Rechtecke tun wollen, aber kann es nicht bekommen, nicht das Rechteck zu überqueren.
Ich versuche es so die Krankenwagen gehen kann nicht einfach vorbei, dass Hindernis zu machen, habe aber um ihn herum zu gehen, möchte ich einen Track machen, ein Spiel Rennen aber fest
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);
}
}
}
Wegfindung durchaus nicht trivial ist. Das ist wirklich zu breit – BradleyDotNET
Hinzugefügt mehr Informationen über mein Problem – Quakers
wenn jedes Hindernis seine eigene Position und Dimension haben, überprüfen Sie, ob Ihr Objekt mit ihnen schneidet. –