This is part 13 in an ongoing series about creating video games in Python 3 using the Pygame module. Previous articles are:
- Learn how to program in Python by building a simple dice game
- Build a game framework with Python using the Pygame module
- How to add a player to your Python game
- Using Pygame to move your game character around
- What's a hero without a villain? How to add one to your Python game
- Add platforms to your game
- Simulate gravity in your Python game
- Add jumping to your Python platformer game
- Enable your Python game player to run forward and backward
- Using Python to set up loot in Pygame
- Add scorekeeping to your Python game
- Add throwing mechanics to your Python game
Pygame provides an easy way to integrate sounds into your Python video game. Pygame's mixer module can play one or more sounds on command, and by mixing those sounds together, you can have, for instance, background music playing at the same time you hear the sounds of your hero collecting loot or jumping over enemies.
It is easy to integrate the mixer module into an existing game, so—rather than giving you code samples showing you exactly where to put them—this article explains the four steps required to get sound in your application.
Start the mixer
First, in your code's setup section, start the mixer process. Your code already starts Pygame and Pygame fonts, so grouping it together with these is a good idea:
pygame.init()
pygame.font.init()
pygame.mixer.init() # add this line
Define the sounds
Next, you must define the sounds you want to use. This requires that you have the sounds on your computer, just as using fonts requires you to have fonts, and using graphics requires you to have graphics.
You also must bundle those sounds with your game so that anyone playing your game has the sound files.
To bundle a sound with your game, first create a new directory in your game folder, right along with the directory you created for your images and fonts. Call it sound
:
s = 'sound'
Even though there are plenty of sounds on the internet, it's not necessarily legal to download them and give them away with your game. It seems strange because so many sounds from famous video games are such a part of popular culture, but that's how the law works. If you want to ship a sound with your game, you must find an open source or Creative Commons sound that gives you permission to give the sound away with your game.
There are several sites that specialize in free and legal sounds, including:
- Freesound hosts sound effects of all sorts.
- Incompetech hosts background music.
- Open Game Art hosts some sound effects and music.
Some sound files are free to use only if you give the composer or sound designer credit. Read the conditions of use carefully before bundling any with your game! Musicians and sound designers work just as hard on their sounds as you work on your code, so it's nice to give them credit even when they don't require it.
To give your sound sources credit, list the sounds that you use in a text file called CREDIT
, and place the text file in your game folder.
You might also try making your own music. The excellent LMMS audio workstation is easy to use and ships with lots of interesting sounds. It's available on all major platforms and exports to Ogg Vorbis (OGG) audio format.
Add sound to Pygame
When you find a sound that you like, download it. If it comes in a ZIP or TAR file, extract it and move the sounds into the sound
folder in your game directory.
If the sound file has a complicated name with spaces or special characters, rename it. The filename is completely arbitrary, and the simpler it is, the easier it is for you to type into your code.
Most video games use OGG sound files because the format provides high quality in small file sizes. When you download a sound file, it might be an MP3, WAVE, FLAC, or another audio format. To keep your compatibility high and your download size low, convert these to Ogg Vorbis with a tool like fre:ac or Miro.
For example, assume you have downloaded a sound file called ouch.ogg
.
In your code's setup section, create a variable representing the sound file you want to use:
ouch = pygame.mixer.Sound(os.path.join(s, 'ouch.ogg'))
Trigger a sound
To use a sound, all you have to do is call the variable when you want to trigger it. For instance, to trigger the OUCH
sound effect when your player hits an enemy:
for enemy in enemy_hit_list:
pygame.mixer.Sound.play(ouch)
score -= 1
You can create sounds for all kinds of actions, such as jumping, collecting loot, throwing, colliding, and whatever else you can imagine.
Add background music
If you have music or atmospheric sound effects you want to play in your game's background, you can use the music
function of Pygame's mixer module. In your setup section, load the music file:
music = pygame.mixer.music.load(os.path.join(s, 'music.ogg'))
And start the music:
pygame.mixer.music.play(-1)
The -1
value tells Pygame to loop the music file infinitely. You can set it to anything from 0
and beyond to define how many times the music should loop before stopping.
Enjoy the soundscapes
Music and sound can add a lot of flavor to your game. Try adding some to your Pygame project!
Comments are closed.