This week, we’re going to liven the game up a bit by adding sound effects. While I am pretty amazing at drawing triangles and other figures that you can create without any artistic skills, I have even less skills making sounds. Luckily, there are some places we can get some free sounds to use in our game. We’ll be using a collection of sounds from the AppHub’s SoundLab project, so you can grab that, and pick up the source code from last week from the SkyDrive share.

This one is going to actually be pretty easy. To start with, we’ll grab the sounds from the SoundLab project, and pick a couple that we think might work for the game. You could go with some gun sounds if you are feeling in a more aggressive mood, but I feel that some of the sounds in the UI folder actually work better for the feel of the game. I picked UI_Misc13.wav for the sound of a bullet hitting an enemy, and UI_Misc17.wav for the shooting sound. So the first thing we need to do is grab the files we picked, and drop them into the TriangleShooterContent project. Once they are added in, we can set up a variable for them, load them from the content project, and then play them.

The variables are of type SoundEffect, so we’ll set one up for firing, and the other for when the enemy gets hit.

SoundEffect Variables
  1. SoundEffect fire;
  2. SoundEffect enemyHit;

Loading the content is straightforward and similar to the previous loading of content we’ve done.

LoadContent
  1. fire = Content.Load<SoundEffect>("UI_Misc17");
  2. enemyHit = Content.Load<SoundEffect>("UI_Misc13");

And playing them is a simple matter as well. The collision sound is good how it is, so we don’t need to tweak it at all. We can just use the default method signature with no arguments.

Code Snippet
  1. if (new Rectangle((int)enemy.Position.X - enemy.Avatar.Width / 2, (int)enemy.Position.Y - enemy.Avatar.Height / 2, enemy.Avatar.Width, enemy.Avatar.Height).Contains((int)b.Position.X, (int)b.Position.Y))
  2. {
  3.     bullets.Remove(b);
  4.     enemies.Remove(enemy);
  5.     enemyHit.Play();
  6.     score++;
  7.     if (score > highScore)
  8.     {
  9.         highScore = score;
  10.     }
  11.     break;
  12. }

The firing sound effect is a bit loud for how often it happens, so we use the overload that allows you to define the volume, pitch modification, and panning. about 50% volume seems good.

Shooting with a sound effect
  1. if (timeToShoot <= TimeSpan.Zero)
  2. {
  3.     timeToShoot = new TimeSpan(0, 0, 0, 0, ShootDelay);
  4.     bullets.Add(new Bullet() { Avatar = txBullet, Position = new Vector2(player.Position.X + player.Avatar.Width * (float)Math.Cos(player.Rotation), player.Position.Y + player.Avatar.Height * (float)Math.Sin(player.Rotation)), Rotation = player.Rotation, Speed = 15f });
  5.     fire.Play(.5f, 0f, 0f);
  6. }

And really, that’s it. Well that was easy. Next week, we’ll refactor the code to make it easier to read and make some tweaks to the gameplay.

Download the latest version of the source code.