Chapter 19. Sound

 

In the last chapter, we made our first graphical game, PyPong, using what we have learned about graphics, sprites, collisions, animation, and events. In this chapter, we’ll add another piece of the puzzle: sound. Every video game and many other programs use sound to make them more interesting and enjoyable.

Sound can be both an input and an output. As an input, you’d connect a microphone or other sound source to the computer, and the program would record the sound or do something else with it (maybe send it over the Internet). But sound is much more common as an output, and that’s what we’ll cover in this book. We’ll learn how to play sounds like music or sound effects and add them to our programs, like PyPong.

More help from Pygame—mixer

Sound is another one of those things, like graphics, that can get complicated, because different computers have different hardware and software for playing sounds. To make things simpler, we’re going to get some help from Pygame again.

Pygame has a module for working with sound, called pygame.mixer. In the real, non-programming world, a device that takes in different sounds and merges them together is called a “mixer,” and that’s where Pygame got the name.

Making sounds versus playing sounds

Playing sounds

Controlling volume

Repeating music

Adding sounds to PyPong

More wacky sounds

Adding music to PyPong