App Hub
Banner of Game Development Tutorial

making things explode

Adding animated explosions for destroyed enemies

 

A game is all about challenge mixed with reward. As it stands right now, our current challenge to the player feels formidable – the enemies are swooping by fast and it’s hard to make them all disappear quickly enough. But what about a reward? Simply having the enemies disappear when destroyed isn’t good enough. Let’s do something flashy – how about an explosion?

To create an explosion effect, we’re going to use two concepts we have already written in our code, and put them together. We’ll use the sprite strip graphic playback in the Animation class, but combine that with the temporary nature and List mechanics we’ve used for Enemy and Projectile classes. Explosions will be animations that stick around long enough to play once, and then are removed.

We don’t need a new class for this, we’ll just do the work inside Game1 with some basic List management that we’ve done before.

Switch to the Game1 class by double-clicking the Game1.cs file in the Solution Explorer in Visual Studio.

inside game1.cs

This one’s going to be a cakewalk. Everything you’re going to code you’ve done before in one form or another. You’re a pro! First, we need to declare some variables at the top of our Game1 class.

Look for the first { mark under the start of the Game1 class, and go just below TimeSpan previousFireTime; that you added in the previous step. Add a new line and then add the following:

Texture2D explosionTexture;
List<Animation> explosions;

We need to initialize the list and the texture. Let’s start with the list, in Initialize() .Look down the code, find the Initialize() method. Inside that method, add this line:

explosions = new List<Animation>();

Now, the texture. That’s done in ContentLoad(). Find the LoadContent() method. Inside that method, below the projectileTexture = call, add this single line:

explosionTexture = Content.Load<Texture2D>("explosion");

Just like Projectile, you’ll need a way to easily add an explosion when the time comes. Create your own AddExplosion() method; find some empty space in the Game1 class and add these lines:

private void AddExplosion(Vector2 position)
{
Animation explosion = new Animation();
explosion.Initialize(explosionTexture,position, 134, 134, 12, 45, Color.White, 1f,false);
explosions.Add(explosion);
}

When do we call AddExplosion()? That’s the trick. We need to find somewhere that we’re making a decision about an Enemy who’s just gotten destroyed. You’ve seen that place; it’s in UpdateEnemies(). We have to be careful about where we put these next few lines – it must be in the right spot.

Scroll down and find the UpdateEnemies() method. Look for the line marked if (enemies [i].Active == false) and go just below the { mark. Add the following lines:

// If not active and health <= 0
if (enemies[i].Health <= 0)
{
// Add an explosion
AddExplosion(enemies[i].Position);
}

Excellent. Just like the projectiles, we’ll need to write a method to update the explosions and remove them from the list. Call this method UpdateExplosions(). Find some empty space inside Game1 class, preferably near the Update() method, make a new line, and you’re ready to go. Add the following lines to make your new UpdateExplosions() method:

private void UpdateExplosions(GameTime gameTime)
{
for (int i = explosions.Count - 1; i >= 0; i--)
{
explosions[i].Update(gameTime);
if (explosions[i].Active == false)
{
explosions.RemoveAt(i);
}
}
}

And, of course, we must call this new method from the Update() method in the game loop. Look for the Update() method inside the Game1 class, and after the UpdateProjectiles() call, add the following lines:

// Update the explosions
UpdateExplosions(gameTime);

Finally, we need to draw any active explosions on the screen. Just like the projectiles, it’s an easy matter of adding the following lines to the Draw() call:

// Draw the explosions
for (int i = 0; i < explosions.Count; i++)
{
explosions[i].Draw(spriteBatch);
}

We’re ready to build and watch the fireworks!

running the game

Build and run your game by pressing CTRL+F5. When you run, you should see an explosion every time you shoot down an enemy ship (or run into one). If your game doesn’t look like the screenshot below, retrace your steps through this section.

It’s amazing what a few animated graphics will do to make a game look and feel more polished, but we’re about to take it up a whole other level by adding sound and music in the next step!

Move on to the next step: Playing Sounds

var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG