Tqst Drove

You've successfully downloaded/installed pygame and grabbed a copy of the Head First Programming sound files for this chapter. Now, test the pygame program in IDLE to see if things are working correctly:

oIibo!

Sounds like pygame is up and running!

«s, tW* y^b* worbasWl«^» fati DON'T trust us: \ust be sure to run the program yoursel tJiereictre no

Dumb Questions

Q/ So pygame is a library created by some programmer other than the Python folks?

A" Yes, it's what's called a third-party library. It wasn't created by you or the people that bring you Python. Some other programmer(s), a third party, created pygame.

Q/ And it's just given away for free?

A." Yes. Which is very nice of them, isn't it?

Q/ Are there other libraries like pygame that can be added into my Python environment?

A." Yes, there are lots. To see the current list, follow the Package Index link from the main Python website. Python refers to third-party libraries as "packages" and, as you'll see, there are packages available for every conceivable purpose, not just playing sounds or developing games (as is the case with pygame).

Q/ The sounds I downloaded are WAV files. I know that WAV is one of the standards for encoding sound, but is it the best format to use?

A." That depends on who you ask! We are using WAV files because they are used in lots of places and are well-supported on most operating systems. There are lots of file formats for sound and many of them claim to be "better" than WAV, but for what we are doing here, WAV is perfect.

Q/ What's the deal with the wait_ finish() function in the Ready Bake Code? I just don't get why it's there.

A." It's a function that waits for the sound to finish playing before continuing with the rest of the program.

Q/ What?! Surely the sound just plays?

A" Playing with pygame in this way, although fun, masks a problem that can surface when working with sound (in any programming language). It turns out that, when asked to play a sound, the main chip inside your computer (the CPU) doesn't even bother trying. Instead, there's another, smaller chip in your computer that is specifically designed to play sounds and it is to this chip that your main chip hands the sound file to and says: "play this for me." The main chip then goes back to running your code, sees another request to play a sound, doesn't bother, hands the new sound file off to the sound chip, and repeats until your program ends. The sound chip—and this is the important part—is designed to operate in parallel with your main chip. While your main chip is doing something else, the sound chip is busy playing any sounds it has been asked to play. And—here's the rub—if the sound chip has been asked to play more than one sound, it attempts to play each sound at the same time.

Q/ So the wait_finish() function is like an artificial delay after each sound?

A." No, not really a delay, more like a pause designed to let the sound effect play fully before trying to play anything else. The wait_finish() function forces your sound chip to finish with one sound before starting another. What happens is that when a sound is played, the play() method passes back the channel (or track) number that the sound is playing on. You can then use the channel number to ask pygame to wait for the channel to finish playing a sound before continuing, which is what the code in the Test Drive on the previous page does.

Q/ And if I don't use wait_finish(), what happens then?

A." All the sounds attempt to play at the same time and it sounds like a jumble of sounds as opposed to one sound playing, then another, then another, and so on.

beyond pseudo-code

ExiddSe

Now that you know how to generate a sound using pygame, it's time to write the code for TVN's program. Base your program on the pseudo-code you created earlier.

on with the show

pong Exercise

SoLutio H

Now that you know how to generate a sound using pygame, it's time to write the code for TVN's program. You were to base your program on the pseudo-code you created earlier.

You need to import

lmport pygamemi*er

Reuse the "wait_finishO" function from earlier.

def walt_flnlsh(channel): while channelget_busy(): pass

Create a mixer object and initialize the sounds = pygame.mixer pygame sound system.

sounds.lnlt()

Load each of the required sounds into its correct_s = sounds.Sound("correctwav ) own variable.

wrong_s = sounds.Sound("wrong wav )

This is what you'll ask the question master each time.

Make suve the Counts that you'll maintain 3« set "to 3 reasonable starting value-

l-t would be OK to move th«e three lines of code to the top of the program, just so long as they have starting values before the while loop starts.

prompt = "Press 1 for Correct, 2 for Wrong, or 0 to Quit "

"number_asked = 0 number_correct = 0 _number_wrong = 0

Wasn't ended-

If the answer is correct, increment the . nu.m.ber_ask.ed .=. numb.er_as.ke.d . + . I.

counters and then play the appropriate sound number_correct = number_correct + I

If the answer is incorrect, increment .if . .Ch.oiCe. . == . '2':

the counters and play the sound effect. number_asked = number_asked + 1

choice = input(prompi)

M the end of the program, display a summary <* the toun-ter values.

.p.ri.nt(w.You . .asked . ". .+. str.(nu.m.ber_ask.ed). + . ". .question?.".)

print(str(number_correct) + " were correctly answered") print(str(number_wrong) + " were answered incorrectly")

Tost DriVq

Type your code into IDLE and save it under the name gameshow. py. With the Head First Programming sounds stored in the same directory as your program, press F5 to give it a spin.

Python Shall

Tython 3.1.1 (r311:744S0, Aug lfi 2003, 071Ü3:Í5) J3CC 1.1.3] uii Hwirf

Type "copyright", "crcdita" or "1icchoc[)" tor norc mlornution.

■J» — »■>

RESTART —

Presa

1 for

Correct,

2

for

Wrong,

or

a

to

Quit:

Píese

L lor

Correctj

2

Tor

Wronq,

or

0

to

auiti

Fresa

1 for

Correct,

2

for

Wrong,

or

0

to

Quit:

Presa

1 1er

Correctj

2

lor

Hrsiu),

or

a

to

Quit:

Fresa

1 for

Correct,

2

for

Wrong,

or

0

to

Quit:

Presa

L lor

Correct,

2

lor

Wr onq,

or

a

to

Quit:

Presa

1 for

Correct,

2

for

Wrong,

or

0

to

Quit:

Presa

1 lor

Correct,

2

lor

Wr onq r

or

0

to

Quit:

Fresa

1 for

Correct,

2

for

Wrong,

or

0

to

Quit;

Presa

L lor

Correct,

2

for

Wronq r

or

a

to

Quit:

Presa

1 for

Correct,

2

for

Wrong,

or

to

Quit:

Presa

1 lor

Correct,

2

for

Wronq r

or

a

to

Quit:

Presa

1 for

Correct,

2

for

Wrung,

or

a

to

Quit;

Presa

L lor

Correct,

2

lor

Wronq r

or

a

to

Quit:

Fresa

1 for

Correct,

2

for

Wrong,

or

0

to

Quit:

fou 3t

íed 14 queétions

in wftrr rnrrrrt 1 y nri.ivrrrrí. 1 were answered incorrectly.

in wftrr rnrrrrt 1 y nri.ivrrrrí. 1 were answered incorrectly.

We can't show these sounds either, so go ahead and run the program to hear it working for yourself-

pygameExposed

This week's interview: Is it fun being pygame?

Head First: Hello, pygame. Nice of you to join us.

pygame: Hello. Thank you so much for giving me this opportunity to discuss my situation with you.

Head First: What situation is that?

pygame: Well, you know, just because I'm used for gaming, everyone expects me to be fun all the time. Life and soul of the party... never stopping to smell the roses... always on the go. <sigh> It's all too much fun, really.

Head First: Too much fun? Really?!?

pygame: Well... yes. Not a lot of people know this, but my life as pygame is hard. Not only do I have to help out regular folks with their programming problems, but there's all those gamers, too. Some of those guys never sleep... it's just play, play, play, play, play, play... I'm simply exhausted.

Head First: Oh, sorry to hear that. But, don't you feel good that all those programmers out there in the Python community are using you?

pygame: I guess so.

Head First: You've made a lot of programmers' lives easier. There's lots of great code written that would not have been written if it weren't for you.

pygame: Yeah, right. I do all the heavy lifting while everyone else is off doing other things.

Head First: Ah, come on, your life's not that bad, is it?

pygame: <sighs>

Head First: Surely you know what people are saying about you?

pygame: Now they're talking about me, too? How awful... <sobs>

Head First: Yeah, there's lots of talk, but it's all good. The Python programming community loves you, pygame.

pygame: They do? <sobs even more>

Head First: Yes. You are well-tested, well-written, and your documentation is first rate. Your support for all the major releases of Python is right on the money, too, and you work on Mac OS X, Windows, and Linux.

pygame: All I've ever tried to do is keep everyone happy.

Head First: And you do. We've heard so many great things about you that we are recommending you to all our friends.

pygame: Do they play games? I'm good at that, you know.

Head First: Yes, some of them do. But others just talk about the great things their applications can now do thanks to you, pygame.

pygame: I guess things aren't quite so bad after all?

Head First: Not at all. We're big fans!

pygame: Why, thanks. That's awesome. Do you have time for a quick game? There's this new Dungeons and Dragons that I've been dying to try...

dated interface

Was this article helpful?

0 0

Post a comment