Pygame is cross platform

Before continuing with the rest of this chapter, you need to take five to download and then install the pygame technology onto your computer. The pygame technology is an example of what's known as a third-party library: that's extra functionality that can be added into your Python environment, but which isn't part of the standard library.

As installing pygame tends to be a very platform-specific thing, we've uploaded a set of instructions onto the Head First Programming website for you to follow.

STOP! Don't proceed with the rest of this chapter until you've installed pygame for Python 3 on your computer.

Reaps Bake

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 tta library you want to use.

Create a "pygame.mixer" object and initialize the sound system.

The "waitJmishO" function loops until the "/ channel's wget__busyO" method returns False.

The value returned fr m the >layO" method gets ^ passed to "wait_finish()".

import pygame.mixer sounds = pygame.mixer sounds.init()

def wait finish(channel):

while channel.get busy() pass s = sounds.Sound("heartbeat.wav") wait finish( s2 = sounds.Sound("buzz.wav") wait finish( s3 = sounds.Sound("ohno.wav") wait finish( s4 = sounds.Sound("carhorn.wav") wait finish(

The wget__busyO" method checks to see if the sound is still playing.

_>ass" is a P#on tonsWt that does nothing-

Load in the sound file you want to play.

Identify and play each of the sounds.

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.

Was this article helpful?

0 0

Post a comment