XNA Level 1 Lesson 6
Collision Detection and Text
- Add a new spriteFont
- Create font and score variables
- Load Font
- Draw Font
- Check Collision
- Update Score
|
|
|
Lesson 6 – Collision Detection and Text
In this lesson we will learn how to add text so we can display the score and how to detect if the ice asteroid has hit our ufo.
Right click your content menu and select Add –> New Item
Select Sprite Font and give it the name mainFont
In the Gloabal Variables after the code where you created your textures add the SpriteFont font;
Also to save time later create a variable called score
SpriteFont font;
int score = 0;
We now need to load the font, go to the load function and add the line:
font = Content.Load< SpriteFont>( "mainFont");
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//load our graphic
ufoTex = Content.Load<Texture2D>("ufo");
asteroidTex = Content.Load<Texture2D>("asteroid");
font = Content.Load<SpriteFont>("mainFont"); //NEW LINE
}
All we need to do now is display the font using the draw function with the line:
spriteBatch.DrawString (font, "Score: "+score , new Vector2(20, 10), Color.White);
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(ufoTex, ufoPos, Color.White);
spriteBatch.Draw(asteroidTex, asteroidPos, Color.White);
spriteBatch.DrawString(font, "Score: "+score , new Vector2(20, 10), Color.White);//NEW LINE
spriteBatch.End();
base.Draw(gameTime);
}
Pressing F5 should now display your Score
Collision Detection
To check if the asteroid has hit our ufo we are first going to calculate the midpoint of the ufo and the check if that point is inside the asteroid rectangle
Midpoint x is equal to ufo x + ½ the width of the ufo texture
And
Midpoint y is equal to ufo y + ½ the height of the ufo texture
We can then check if midpoint x greater than asteroid x and is also less than asteroid x + asteroid width
Then
Check if midpoint Y is greater than asteroid Y and is less than asteroid Y + asteroid height
If these checks are both true then the midpoint is within the rectangle and a collision will be detected
Looks a bit like this (add this into your Update code, just after you moved the asteroid)
//Get Midpoint of ufo
int x = (int)ufoPos.X+(ufoTex.Width/2);
int y = (int)ufoPos.Y+(ufoTex.Height/2);
//check if midpoint is inside asteroid
if (x > asteroidPos.X && x < asteroidPos.X + asteroidTex.Width)
{
if (y > asteroidPos.Y && y < asteroidPos.Y + asteroidTex.Height)
{
score = 0; //reset score
}
}
Also change the asteroid update code so your score goes up each time the asteroid gets to the bottom
if (asteroidPos.Y > 800)
{
Random rand = new Random();
asteroidPos.X = rand.Next(800 - asteroidTex.Width);
asteroidPos.Y = -asteroidTex.Height;
score += 10; //NEW LINE
}
Complete Listings
public class Game1 : Microsoft.Xna.Framework.Game
{
//GLOBAL VARIABLES
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// This is a texture we can render.
Texture2D ufoTex;
Texture2D asteroidTex;
// Set the coordinates to draw the sprite at.
Vector2 ufoPos = Vector2.Zero;
Vector2 asteroidPos = Vector2.Zero;
SpriteFont font;
int score = 0;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
//ufo starting position
ufoPos.Y = 480;
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//load our graphic
ufoTex = Content.Load<Texture2D>("ufo");
asteroidTex = Content.Load<Texture2D>("asteroid");
font = Content.Load<SpriteFont>("mainFont");
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
//update asteroid
asteroidPos.Y += 5;
if (asteroidPos.Y > 800)
{
Random rand = new Random();
asteroidPos.X = rand.Next(800 - asteroidTex.Width);
asteroidPos.Y = -asteroidTex.Height;
score += 10;
}
//Get Midpoint of ufo
int x = (int)ufoPos.X+(ufoTex.Width/2);
int y = (int)ufoPos.Y+(ufoTex.Height/2);
//check if midpoint is inside asteroid
if (x > asteroidPos.X && x < asteroidPos.X + asteroidTex.Width)
{
if (y > asteroidPos.Y && y < asteroidPos.Y + asteroidTex.Height)
{
score = 0; //reset score
}
}
//update ufo
KeyboardState key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Left) && ufoPos.X > 0)
{
ufoPos.X -= 5;
}
if (key.IsKeyDown(Keys.Right) && ufoPos.X < 800 - ufoTex.Width)
{
ufoPos.X += 5;
}
if (key.IsKeyDown(Keys.Up) && ufoPos.Y > 0)
{
ufoPos.Y -= 5;
}
if (key.IsKeyDown(Keys.Down) && ufoPos.Y < 600 - ufoTex.Height)
{
ufoPos.Y += 5;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(ufoTex, ufoPos, Color.White);
spriteBatch.Draw(asteroidTex, asteroidPos, Color.White);
spriteBatch.DrawString(font, "Score: "+score , new Vector2(20, 10), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
|