Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Adding Music

In this lesson, you’ll add background music to your game. The sounds and music you will use in this lesson and the next are in the sound directory:


Sample Code (.zip)

422.6 KB

As with most things in pygame, using mixer starts with an initialization step:

 98# Setup for music and sounds. Defaults are good.
101# Initialize pygame

After the system is initialized, you can get your background music setup:

129# Load and play background music
130# Sound source: Chris Bailey - artist Tripnet
131# License:"sound/Sky_dodge_theme.ogg")

Finally, when the game is over, all sounds should stop. This is true whether the game ends due to a collision or the user exits manually. To do this, add the following lines at the end of the program after the loop:

193# All done! Stop and quit the mixer.

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 learn about adding music to your game. pygame includes a module named mixer. mixer includes not only the tools to play back music, but also to control sound effects.

00:12 This lesson will focus on adding background music to your game. The mixer module handles all of the sound-related activities in pygame.

00:21 You have to initially set it up with pygame.mixer.init(). For music playback functionality, there’s a sub-module that allows you to stream individual sound files in a variety of formats. To accomplish this music playback, the sub-module has a variety of methods, and the first one you’ll practice with is .load(), which loads the file that you’re going to play back.

00:45 It supports Ogg files and Mod files. In the past, it has supported MP3 files also, but that support is a little sketchy depending on the platform and the file type.

00:56 You may want to make sure that the MP3 file you’re using loads properly. The next method would start playback, .play(). It has a named parameter of loops, which you can set to any integer you want—but if you set it to -1, then it will loop indefinitely. And then .stop() is used just for that—to stop playback. A good habit before you quit the application is to tell the mixer to quit, which uninitializes and stops all sound playback of the mixer.

01:25 Let’s set all these things up in your code. Here we are at the top of the file. The first thing you need to do—heading down

01:35 the file—is at line 98. Right before you initialize pygame, you need to set up the mixer and initialize it.

01:57 That’s done with pygame.mixer, and you use the method .init(). This can take some arguments into it, but the defaults are good.

02:06 No need to change anything here, but there are settings that you can modify such as the amount of bit depth, the sample rate, and so forth. If you’re interested, there’ll be links below the video again, for more information. Okay, so after you’ve initialized it, then you need to prepare and load up your music.

02:27 To do that, you’re going to do that just after you create all the groups for holding the enemies, and right before the main loop starts running, you’ll add the music. At line 129…

02:45 The original article had some music that was found from one of the resources that I’ll share later, and in this case, I’ve created the music. And I custom created this Ogg file to use for the music in the game since I was having problems with the MP3 playback.

03:04 You’re welcome to use this music—I’ll include a license here. But it’s under Creative Commons, so you’re welcome to use it as long as you provide credit, and this talks about how to do that.

03:15 And also if you want check out my other music, I go by the artist name Tripnet, and you can find it on iTunes, Apple Music, and Spotify. Okay. So here, pygame.mixer, then you’re going to do .music and use the method .load(). .load() takes the filename.

03:30 And, where do you find this music? Well, there’s a set of files—just like you had loaded from images/ before—in a folder called sound.

03:38 So make sure you have those. Those are downloadable. Again, just below this article you’ll see a link for that, for the code. Here’s sound/, and here is one I created called Sky_dodge_theme.ogg.

03:54 That’s going to be the name of the file. So make sure you use the directory, "sound/Sky_dodge_theme.ogg". Great! Then from, you’ll tell it to play.

04:08 So right before the loop starts, you’ll start playing the music. And here it takes that named parameter of loops and you’ll set it to -1, which means it will continually loop and loop and loop and loop until you stop, which is, well, the next point. How do you make everything stop?

04:26 At the very end of your program—after exiting the event loop and so forth, sort of wrapping up everything—outside the loop, you’re going to set it up to stop and quit the mixer.

04:43 pygame.mixer—in this case, the .music—tell it to stop using the method. And then pygame.mixer.quit()this will make sure no sound hangs and everything’s all set up as exiting your program. Okay. Just before playing back here, we’ll look at all the code. Zoom out of it.

05:13 Okay. Got it all importing, we’ve got your classes—Player, Enemy, Cloud. Then you added at 98, 99, the pygame.mixer and initted it.

05:32 Then here, at 129 to 133, you loaded and played back the background music, and have it looping infinitely with a -1 loop. Then at the very bottom, we set up pygame.mixer to stop and quit.

05:53 Cool! Let’s try it out.

06:01 Okay, it might be a little bit loud, so we can work on that.

06:11 Upbeat. Trying to be exciting. Here’s where it loops.

06:17 Sounds good. Now that you know how to add music, the next thing would be to add some sound effects, and that’s in the next lesson.

helenyip2 on March 21, 2020

Sky_dodge_theme.ogg file is not in the folder via the link provided.

helenyip2 on March 21, 2020

Nevermind! I found it when I downloaded the Code zip file under Suppporting Materials.

Pawe on March 29, 2020

Same here! Thank @helenyip2 for pointing the references to files.

jamesbrown68 on July 15, 2020

Well, maybe I’m missing something. When I click on “Click here to download the source code for the PyGame sample project used in this tutorial.” I’m taken to a github page called realpython/materials/pygame-a-primer.

In there I see a list of 14 files. There’s the ‘cloud.png’, ‘jet.png’, and ‘missile.png’ image files we used in an earlier lesson. But no ‘Sky_dodge_theme.ogg’ or any zip file that might contain it. I do see ‘collision.ogg’, ‘Rising_putter.ogg’ and ‘Falling_putter.ogg’, but I suppose those are for the upcoming ‘Adding Sound Effects’ lesson? There is an mp3 called ‘Apaxode-Electric_1.mp3’ which I presume is to be used as background music?

The other files I have no idea what to do with.

Chris Bailey RP Team on July 15, 2020

Hi @jamesbrown68, At the top of the description text for this lesson, just below the video player, you will see a button for “Supporting Material”. You can download a .zip file from there which has the code broken up into the individual lessons, and separate folders for images and sound.

jamesbrown68 on July 16, 2020

Thanks, Chris.

Am I crazy? Was that link there yesterday? I’m probably crazy.

Become a Member to join the conversation.