XNA Level 1 Lesson 10
Firing!!!
- Add laser sprite
- Create new variables
- Load sprite
- Edit the update function
- Remove score from the asteroid loop
- Add new switch case options
- Edit the draw function
|
|
|
Adding Firing to the game
Adding the ability to fire to our game isn't that difficult.
- We add a picture of a laser
- When we press fire we align the laser to the centre of the ufo
- Move the laser up the screen
- Check to see if the laser has hit any of the asteroids
- Check to see if the laser has gone off of the screen
Add the laser sprite
Right Click on your project content menu and add the laser image to the project
(Check lesson 3 and 9 if you have forgotten how to do this)
Create new variables for the laser
In the global variables section, just before 'public Game1()'
Vector2 laserPos = new Vector2();
int fireState = 0; //0 = not firing, 1 = firing
Texture2D laserTex;
laserPos stores the coordinates of the laser
fireState stores the status of the laser, is it firing?
laserTex store the image of the laser
Load the laser image (sprite)
Go to the LoadContent function and add the following code
laserTex = Content.Load<Texture2D>("laser");
Edit the updateGame function
In the loop that checks if an asteroid has gone off of the screen, remove score+=10. We will only get points if we shoot an asteroid
//update all asteroid positions
for (int i = 0; i < numberOfAsteroids; i++)
{
asteroid[i].Y += 5;
if (asteroid[i].Y > screenHeight)
{
Random rand = new Random();
asteroid[i].X = rand.Next(screenWidth - asteroidTex.Width);
asteroid[i].Y = 0 - (numberOfAsteroids * (250 - score / 10)) - 128; //put to top
score += 10; //remove this line!
}
}
The next section may look quite long, but it isn't anything you have not seen before. We are going to use a Switch Select Statement
if fireState is 0 then position laser under ufo
if fireState is 1 then fire
We check to see if the laser has hit any asteroids using almost the same code as checking if the ufo has hit any of the asteroids
If you have hit one, the asteroid is reset and your score goes up!
Re-visit lesson 6 if you need a refresher
Warning. This code must go after we have finished getting the keyboard input for the ufo
if (key.IsKeyDown(Keys.Space))
{
fireState = 1;
}
//update laser
switch (fireState)
{
case 0:
//Position laser under ufo
laserPos.X = x-(laserTex.Width/2);
laserPos.Y = y-20;
break;
case 1:
//Update position
laserPos.Y -= 10;
if (laserPos.Y < 0)
{
fireState = 0;
}
//check each asteroid
//Get Midpoint of ufo
int lx = (int)laserPos.X + (laserTex.Width / 2);
int ly = (int)laserPos.Y + (laserTex.Height / 2);
//check if midpoint is inside each asteroid
for (int i = 0; i < numberOfAsteroids; i++)
{
if (lx > asteroid[i].X && lx < asteroid[i].X + asteroidTex.Width)
{
if (ly > asteroid[i].Y && ly < asteroid[i].Y + asteroidTex.Height)
{
//reset asteroid position
Random rand = new Random();
asteroid[i].X = rand.Next(screenWidth - asteroidTex.Width);
asteroid[i].Y = 0 - (numberOfAsteroids * (250 - score / 10)) - 128; //put to top
//update score
score += 10;
//reset laser
fireState = 0;
}
}
}
break;
default:
//error//
break;
}
Edit the draw function
We can now simply draw our laser
After Sprite.Begin, but before we draw our UFO add the following line
spriteBatch.Draw(laserTex, laserPos, Color.White);
|