Add sounds and music to make your game more immersive
We have made a fair bit of progress in our game just focusing on input and graphics; while they are key components of gameplay, an often-unsung hero is just as important: sound. The right sound effects and music can make your game exceptional, and without it, players might find something lacking that they can’t quite put their finger on.
We have a few in-game events that would do well to have some sound effects, and a good beat is always helpful to make a shoot-em-up game feel right. Let’s get sounds and music into our game. We’ll be working entirely within Game1.cs for this.
Switch to the Game1 class by double-clicking the Game1.cs file in the Solution Explorer in Visual Studio.
There are two classes that we need to make objects out of – SoundEffect and Song. They’re pretty self-explanatory, and load up very similar to the graphical assets we use. The first thing is to declare them up at the top of our class with everything else.
Look for the first { mark under the start of the Game1 class, and go just below List<Animation> explosions; that you added in the previous step. Add a new line and then add the following:
Now, since it’s all content, we’ll load up these objects inside the LoadContent() method. One note – not only will we load up the items, but we’ll start playback of the Song item right inside the LoadContent() method.
Find the LoadContent() method. Inside that method, below the explosionTexture = call, add these lines:
You’ll notice that we’re calling PlayMusic(). If you were to try to build right now you’d get a bit of a surprise – PlayMusic() doesn’t exist! We need to make this method, but it’s easy. Find some empty space inside the Game1 class that’s not in the middle of any other method. Add a new line then add the following:
This method performs a bit of a trick in order to help this game play equally well on the Phone emulator as well as the Phone device. At the time of this writing, a plugged-in device will raise an exception (a notification in the code that can be intercepted and dealt with) that we’d rather not have raised. We deal with it here.
Now that we’re playing music, let’s deal with our sound effects. We have two loaded – a laser and an explosion. There’s a perfect place for each of these. First, the laser should go in UpdatePlayer(), right under the call to AddProjectile(), so when the player fires a projectile, a sound is immediately played.
Scroll down and find the UpdatePlayer() method. Look for the line marked AddProjectile(player.Position + new Vector2(player.Width / 2, 0); just below it, add the following lines:
We’ll make explosions sound good, too. Explosions happen inside the UpdateEnemies() method when enemies’ health goes down to zero. We’ll put our sound effect call there.
Scroll down and find the UpdateEnemies() method. Look for the line marked AddExplosion(enemies[i].Position); just below it, add the following lines:
You’ve done it! Sound effects and music are ready to play as soon as you build and run the game.
Build and run your game by pressing CTRL+F5, to hear the sound effects and music at work. You should be hearing a laser every time the player fires, an explosion every time an enemy is destroyed, and there should be music playing in the background.
If you’re not hearing sound effects, check your code against the code above to be sure you’ve got it all. If you’re not hearing music, please note that you won’t hear any music if you’re on a physical Windows Phone 7 device that’s still tethered to your computer. If you untether your device and run the game from the menu on the phone, you should hear music again.
We’re off to our final coding challenge; in the next section, we’re going to add a user interface to the game that displays the player’s health and score.
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/