Page 256 -
P. 256
building a graphical user interface
Ready Bake
Code
Take a look at this handy pygame program, which shows how to play
four sounds one after another:
Just like with other
libraries, you import the
library you want to use.
The “get_busy()”
import pygame.mixer method checks to
Create a “pygame.mixer” object sounds = pygame.mixer see if the sound
and initialize the sound system. sounds.init() is still playing.
The “wait_finish()" def wait_finish(channel): “pass" is a Python
function loops until the
channel's “get_busy()" while channel.get_busy(): construct that
method returns False. pass does nothing.
Load in the
s = sounds.Sound("heartbeat.wav") sound file you
The value returned from wait_finish(s.play()) want to play.
the “play()" method gets
passed to “wait_finish()". s2 = sounds.Sound("buzz.wav") Identify and play
each of the sounds.
wait_finish(s2.play())
s3 = sounds.Sound("ohno.wav")
wait_finish(s3.play())
s4 = sounds.Sound("carhorn.wav")
wait_finish(s4.play())
Load this!
To run this program on your computer, you obviously need pygame installed and you
need to download the sound files for this chapter from the Head First Programming
website. Be sure to put the sound files in the same directory/folder as your program.
you are here 4 221