Adding Sound Effects
For more information about the music
module, mixer
, and .mixer.init()
, check out the following resources from the pygame
documentation:
For more information about the types of audio files that pygame
supports for music playback, check out the following resources:
00:00
In this lesson, you’ll start adding some sound effects. With sound effects, you’re going to attach the sounds to specific events that happen in the game, such as the jet moving up and down, or a collision. For each of those sound effects, you need to create an instance of the .Sound
class. pygame.mixer.Sound()
, and it takes a filename.
00:23
The types of files that you can use can be WAV files or Ogg files. For this game, the files that we provided are .ogg
files. At the point that the event happens in your code, you can trigger the sound effect by using the method .play()
.
00:40
You can have it stop playing back with the method .stop()
. Along with those controls, there’s also a method to set the volume, which takes a float in the range of 0
to 1.0
.
00:51
You’ll use that to adjust the levels of the different sound effects if you need to. In fact, we might even use that for the background music also, since music
has a method for .set_volume()
also. Okay, let’s add those effects into the code. I’m starting here at the top of the file.
01:07
You’re going to scroll down quite a ways to where you loaded the music up, so passing the Enemy
, passing the Cloud
, getting past where you initialize the mixer and the game, and here’s where you’re making custom events. Okay, cool. Here we go.
01:23
# Load and play the background music
. At 135, you need to load all the sound files.
01:35
I ended up making these sound effects myself. And you’re going to create a move_up_sound
. It will be a class instance from mixer.Sound()
. And in a similar way to how you did the music, sound files are there too. There’s a jet moving up sound, Jet_up
, Jet_down
, and then a collision sound of Boom
, and they’re all—again—in that directory that you should have added, downloaded from the resources there. So, sound/
is the directory and Jet_up
—it’s an Ogg file, but you can use the uncompressed WAVs, also. All right, keep going, move_down_sound
, and it will also be of mixer.Sound
…
02:21
"Jet_down.ogg"
. And then the third one is collision_sound
. That will be in sound/
also, and that one is just called Boom
. Okay.
02:35
Saving. In order to have these be triggered, you need to tie them to—in this case, the movement for these two. This one we’ll do next, after that. So that’s back up here, where you are updating the player based upon those keys that are being pressed. Here in 33 through 42, you have if K_UP
is there, then you’re moving left—or I’m sorry, moving up -5
, where you’re going to trigger the sound playback.
03:05
That’s with the method that’s part of that class, move_up_sound.play()
. That’ll make it play. So then here, for a move down, move_down_sound.play()
. You’re calling move_up_sound
if it sees that there’s an Up key pressed, and move_down_sound
… Now, the other thing that you need to have happen is for it to stop playback and play the crash, if you will, the collision. That is pretty far down.
03:37 You’re going to head down all the way to when you’re doing your collision. Okay. So then—I guess right after you’re killing the player, that line on 194—you’re going to stop any of the moving sounds and play the collision sound. Okay.
03:53
I’m going to hide these sounds. move_up_sound
—if it’s playing that, .stop()
—call that method. move_down_sound
—if it’s playing that, .stop()
!
04:02
But instead, the collision_sound
—you want it to play. Then we could move this, toggling the while
loop off of the game loop, and we’ll just label it again. # Stop the loop
! Great. Should be ready to go.
04:15 I think there may be some balance issues with the volume of the music and the volume of the sound effects, but if you’ve saved—let me make sure I saved it again. Okay.
04:26
So, python sky_dodge.py
.
04:36 That was the down sound. Down sound.
04:45 Okay, yeah. Volume levels are a little loud for the music, and the sound effects are okay. Um, maybe we could adjust them just a little bit, but I don’t think I heard the collision play at all, so a couple of things to fix. Back here, you’re creating the music to play. Just after that,
05:09
from the music
, there is a .set_volume()
, and again, it takes a float. Let’s set it at—let’s try half volume, 0.5
. And then the same thing here.
05:25
Okay. So, move_up_sound.set_volume()
, let’s set that to a 0.8
—ah, let’s do 7.0
. Nah, let’s do the 8.0
. Okay, that sounds good. move_up_sound
, move_down_sound
—.set_volume()
, the same. And then the collision, since we didn’t hear it, let’s leave that at 1.0
. Okay. Save.
05:53 A little better adjusted.
06:02
Okay. Still not hearing the sound play there, let’s turn this down a little more. I think I set it at 6.0
. And the music down a little too. Okay. Leave the collision because we haven’t heard it yet.
06:19 I think we may need to change something about how all of those guys stop playing. So here where you’re stopping the sounds from playback—so, we could have these two sounds stop.
06:30
There’s another feature that we can use from pygame
—it’s part of time
, and there’s a thing called .delay()
, and it takes milliseconds.
06:39
We’ll have a really short delay, maybe 50
milliseconds, and then have the collision play, but I think the other advantage would be maybe to have the game not quit so fast while this is trying to play, so we’ll give it a half second.
06:54 And then one other thing that we could do is—I know we stop the music playing here—let’s move that. Let’s have the music stop also here. Stop any moving sounds, music, and then play the collision sound. Okay.
07:11 We made some changes here, changing the delays here. Give it a shot.
07:29 Whoa there, there’s a collision, ha. All right! Good job. Now you can play with adjusting those sounds, triggering additional sounds. We’ll kind of zoom out here.
07:41
Again here. We didn’t change a whole lot at the top. The only thing that we changed in the beginning areas here under Player
is that you added those triggers to play the sound here and here. Again, you could make sounds for moving forward and what have you.
08:05 As far as the enemies and the cloud, you could do some interesting things where those things are added, when that stuff’s triggered. So here, where you’re adding an enemy, you could have a sound going off for that, or maybe some kind of bird sound for when the clouds are added. All kinds of cool stuff that you could do. Great.
08:29 The only other thing you did after loading the sounds in here, on lines 138 to 142, is you adjusted the volumes, adjusted the volume in the music, and last, this whole thing with stopping the playing sounds, adding a delay and again, moving the music to stop also.
08:50 Awesome. Coding’s done! Your next lesson includes some resources where you can find game assets such as graphics and music and sound effects to use in your own games.
Become a Member to join the conversation.