Enhancing Your Game With Sound Effects
00:00 In the previous lesson, your game got one step closer to being an Asteroids clone. It now has collisions and different rock sizes. In this lesson, you’ll add some polish by including some sound.
The key to using sound is the
Sound object from Pygame’s mixer module. With it imported in, you just need to get the path to your sound file, then create a new object with it. Like loading a sprite from way back when, this path assumes that sounds are found in the
Sounds will live in their own folder there, called
sounds/, and are assumed to be
.wav files. Path in hand, create the object and return it. This is so small, it almost isn’t worth a utility method, but using the utility method means you don’t have to remember the relative path logic everywhere in your code wherein you’re loading a sound file.
Let me scroll down to the
.pew_pew attribute of
Spaceship stores the sound of your space bullets firing, using the recently written
load_sound() utility function. Now, all that is left to do is make the noise. Inside of the
.shoot() method, call the
.play() method on the
.pew_pew sound and you’re all good.
Become a Member to join the conversation.