XNA GameStudio 2.0
Adding Sound

Adding a few sounds to the game makes it a little more interesting. Download the following sounds and save them to the Content folder of your project.

  • bounce.wav - a sound to play when the ball bounces off the bat
  • applause.wav - a sound to play when a goal is scored
  • uhh.wav - a sound to play when the game is over

Step 1 - Setting Up XACT

For XNA games, we set up our sounds using a tool called XACT. You can find the tool in the XNA Game Studio folder in the Start Menu. Load it up and choose to Create A New Project. Call the project pong_audio.xap and save it in the Content folder of your project.

Click with the right mouse on the section labeeled Wave Bank, choose to create a New Wave Bank.

In the window that pops up, click with the right mouse button and choose to Insert Wave Files and select the files that you saved to your content folder. You should see this,

XNA Screenshot

Now click on the Sound Bank section and add a new sound bank. Drag the files from the Wave Bank window into the top section of the sound bank window. Onceyou have done that, drag the files from the top of the Sound Bank window into the bottom section. When you are done, you will have this,

XNA Screenshot

Save the XACT project and return to C#.

Step 2 - Importing The Audio

The first thing to do is to import the audio project into C#. Click with the righ mouse button on the Content folder in the Solution Explorer window. Choose to add an exisiting item and add the audio project.

The rest of our work will involve the code window. Start at the top with the list of fields in the main game class. Add the following fields,

AudioEngine audioEngine;
WaveBank waveBank;
SoundBank soundBank;

We need to initialise the sounds whn the program starts running. Go to the Intialize() method and add the following lines,

audioEngine = new AudioEngine("Content\\pong_audio.xgs");
waveBank = new WaveBank(audioEngine, "Content\\Wave Bank.xwb");
soundBank = new SoundBank(audioEngine, "Content\\Sound Bank.xsb");

In order to keep track of the sounds being played, we need a line of code in the Update() method of the main game class. Add the following line,

audioEngine.Update();

Step 3 - Playing The Sounds

Now that we've done all of the preparation, it's time to make those sounds work in our game. The following code in the Update() method needs to be changed so that the bold lines are included.

if (player1.BatRect.Contains(ball.BallRect))
{
   ball.velocity.X *= -1.0f;
   ball.velocity.Y = ((ball.position.Y - (player1.position.Y - ball.sprite.Height)) / (player1.sprite.Height + (2 * ball.sprite.Height)) * 10.0f) - 5.0f;
   soundBank.PlayCue("bounce");
}
if (player2.BatRect.Contains(ball.BallRect))
{
   ball.velocity.X *= -1.0f;
   ball.velocity.Y = ((ball.position.Y - (player2.position.Y - ball.sprite.Height)) / (player2.sprite.Height + (2 * ball.sprite.Height)) * 10.0f) - 5.0f;
   soundBank.PlayCue("bounce");
}

If you can remember or work out where the program looks to see if the ball has hit the top or bottom of the screen, you could add the same sound there too.

When a player scores a goal, their score is increased by 1. Find the lines of code that do this and add the following line just below,

soundBank.PlayCue("applause");

Finally, you need to add the Game Over sound. We changed the field, gamestate to 3 when the game is over. Find where this is done and add the following line,

soundBank.PlayCue("uhh");