4-8 Music and Sound Effects

Although we often think of graphics when we think of games, another important element is sound.  Most games play background music to set the mood of the game, and sound effects when certain events occur.   Thanks to Pygame, adding music and sound effects to animations and games is pretty easy!

The first thing that you need to know is that Pygame can play .mp3, .wav, .wma, and .ogg sound files.  Like Python and Pygame, the .ogg format is free and open-source.   Past students have found that .ogg files work best for both background music and sound effects.   You can find many (free!) music and sound effects files online, or create your own using (free!) tools (e.g., Audacity).  Please be sure that if you distribute a game with sound files, that they are royalty-free and you are not breaking any copyright laws.

Pygame provides a submodule named pygame.mixer (automatically imported with pygame) that conveniently handles background music and sound effects for us.   To initialize the sound system on your computer, we need to call the function pygame.mixer.init() in the Initialize (IDEA) step, just after calling pygame.init().

Background Music

Once the mixer is initialized, the following functions can be used to play music while your game is running:

pygame.mixer.music.load(filename)

This function is used to load background music from a given filename. You can only load and play one background music file at a time.

pygame.mixer.music.play(num_times = 0)

This function tells pygame to start playing the background music. You can pass an (optional) argument specifying the number of times to play the music.  Counting starts from 0, so 0 means to play the music 1 time, 1 means to play the music 2 times etc…  The default parameter value is 0.  If you pass -1, the music will repeat over and over.

pygame.mixer.music.set_volume(volume_level)

The set_volume() function requires a volume_level between 0.0 and 1.0; for example, 0.5 means  to play the background music at 50% volume level.

pygame.mixer.music.fadeout(time)

This function causes the background music to end after time milliseconds; for example, a time of 2000 means that the music will stop in 2 seconds.  While the music is being stopped, its volume gradually decreases (or “fades”) to 0.0.

The functions above are all typically called in the Entities (IDEA) section.  Past students have found that .ogg or .mp3 files work best for background music, and make sure the file is not too large ( < 4 MB).

Sound Effects

While Pygame supports only 1 background music file at a time, your game can have many different sound effects and they can be played such that their sounds overlap.   To support this you need to instantiate a Sound object for each sound effect in your game. Each Sound object includes methods to control the playing of its associated sound effect.   Here is a summary of the most useful Sound methods:

mySound = pygame.mixer.Sound(filename)

The Sound class is located in the pygame.mixer submodule.   Here, mySound is a variable used to refer to the Soundobject being instantiated, you can call this variable anything you like. You can instantiate as many Sound objects as you need, each one loads a sound effect from a given filename.  Note that .ogg and .wav files work best for sound effects.   Do not use .mp3 files for sound effects as the files are often too large and cause sound timing problems.

mySound.play(num_times = 0)

Once you have created a Sound effect object, you can call its play() method.  The (optional) argument works the same way as described above for pygame.mixer.music.play().

mySound.set_volume(volume_level)

The set_volume() method sets the volume level for a given sound effect and also requires a volume_level between 0.0 and 1.0.   Each Sound object in your game can have a different volume level.

Sound Extravaganza!

Let’s put all of this together with our favourite bouncing box example:

Here are the music.mp3 and boing.wav files used in the example above.

I have modified our familiar “red box” animation so that the box bounces from one side of the window to the other.  Each time the box hits the edge of the window a “Boing!” sound effect is played.  While all of this is happening, I play some groovy background music to set the mood.

During the Initialize (IDEA) step, the pygame.mixer.init() function is called to initialize the sound system.  In the Entities (IDEA) step, we load the background music and “Boing!” sound effect, set their volume properties, and start the background music playing (infinitely).  In the Events (ALTER) step, we check to see if the box has reached the left or right limit of the window.  If so, we play the “Boing!” sound effect and change the direction of the red box.

Finally, at line 67 I call the pygame.time.delay() function so that there is a 3 second (3000 ms) delay before pygame.quit()executes. This ensures that the background music has enough time to fade out before the program terminates.

That’s all there is to sound in Pygame!

You Try!

  1. Start a new page in your Learning Journal titled “4-8 Music and Sound Effects”.  Carefully read the notes above and in your own words summarize the key ideas from each section.