First Step CSharp - Playing Sounds
From SwinGame
We want to add a sound for when the ball bounces off the outside walls and a different one when it hits the paddle. We are not up to putting in the code for the paddle yet so we will just focus on getting the sound for the outside walls working.
Sourcing the sounds
We will need to get the 2 sounds for this effect and put them in the project folder for audio. We will use .wav files. Once again you can find your own as with the pictures or use these two to save time.
- Boink.wav - the noise we will use when the ball bounces off the walls
- Paddle.wav - the noise we will use when the ball hits the paddle
If you find your own make sure they are in either .wav or .ogg format. You could even record your own in a program like "GarageBand", "Sound Recorder" or Audacity. Or search for *.wav files on your computer. If you do source your own sounds make sure you call them Boink.wav and Paddle.wav otherwise the rest of the tutorial will be a little confusing.
Copy the two sound files into the Sounds folder of your Resources folder of your BallProgram project for example on my computer the path for this folder is: C:\Documents and Settings\rmercer\My Documents\Visual Studio 2005\Projects\BallProgram\BallProgram\Resources.
Now we need to add them to the code. Open up the GameResource file from your project and find "private static void LoadSounds()" underneath this is where we add any sounds we want for our project similar to when we added the images. The code we need to add for the 2 sounds is:
NewSound("boink", "Boink.wav"); NewSound("paddlehit", "Paddle.wav");
So from now on in the code "Boink.wav" will be referred to as "boink".
Now open up the GameLogic source as we need to tell the program when to play the sounds. The boink should play when the ball hits one of the walls. You will remember that we previously added some "If" statement to test when the ball hit the wall so it could bounce the ball. All we need to do now is find them and add a line of code to tell it to play the sound as well as bouncing. Add the following line of code to the body (between the braces "{" and "}") of the four iif statements:
Audio.PlaySoundEffect(Resources.GameSound("boink"));
so for example one of them will look like this now:
if (ball.Y < 0) { ball.Movement.Y = -ball.Movement.Y; Audio.PlaySoundEffect(Resources.GameSound("boink")); }
Once that is added to all four if statements then hit "Start Debugging" and test it out. It should play the "boink" sound every time the ball hits an outside border.
Now you do this...
Find some more sounds and see if you can get it to make a different noise when it hits the top or bottom or sides. SwinGame can use wav and ogg audio files.
- First Step CSharp - Getting started and compiling "Hello World"
- Adding an Image - How to add an image and draw it
- Sprites and Movement - Getting the image to move
- Playing Sounds - Loading and playing a sound
- Keyboard Input - Moving a sprite with keys
- Sprite Collisions - Adding a "bounce" to the game
- Scoring - Adding a score to your game
- Mouse Input - Clicking sprites in your game

