The main() function creates objects and starts up the game: def main(): """ Play the game.
"""
wall_image = games.load_image("[Link]", transparent = False) [Link] = wall_image
the_chef = Chef() [Link](the_chef) the_pan = Pan() [Link](the_pan)
[Link].is_visible = False [Link].event_grab = True [Link]() # start it up!
main() First, I set the brick wall as the background. I create a chef and a pan. Then, I set the mouse
pointer to invisible and grab all of the input so the mouse pointer can’t leave the graphics window. I
invoke mainloop() to begin the game. Finally, I call main() to kick it all off! SUMMARY In this chapter, you
saw how to use the livewires multimedia package to add graphics to your programs. You learned how to
create a new graphics window and how to set a background image for it. You saw how to display text on
a graphics window. You learned about the sprite, a special graphics object with an image. Specifically,
you saw how to place and move a sprite on a graphics screen. You also saw how to test for collisions
between sprites. You learned how to get input from the mouse. Finally, you saw how to put everything
together in a fast-paced video game, complete with a computer-controlled opponent. Chapter 11 •
Graphics: The Pizza Panic Game 359 Challenges 1. Improve the Pizza Panic game by increasing its
difficulty as the game progresses. Think of different ways to accomplish this. You could increase the
speed of the pizzas and the speed of the chef. You could raise the player’s pan to a higher position on
the screen. You could even increase the number of crazy chefs flinging pizzas. 2. Write a game where the
player controls a character that must avoid falling debris. The player controls the character with the
mouse and objects fall from the sky. 3. Create a simple, one-player game of pong, where a player
controls a paddle and the ball bounces off three walls. If the ball gets by the player’s paddle, the game is
over. 360 Python Programming for the Absolute Beginner, Third Edition 12C HAP TE R SOUND,
ANIMATION, AND PROGRAM DEVELOPMENT: THE ASTROCRASH GAME n this chapter, you’ll expand
your multimedia programming skills to include sound and moving images. You’ll also see how to write a
large program in stages. Specifically, you’ll learn to do the following: • Read the keyboard • Play sound
files • Play music files • Create animations • Develop a program by writing progressively more complete
versions of it INTRODUCING THE ASTROCRASH GAME The project for this chapter, the Astrocrash game,
is my version of the classic arcade game Asteroids. In Astrocrash, the player controls a ship in a moving
field of deadly asteroids. The ship can rotate and thrust forward—most importantly, though, it can fire
missiles at the asteroids to destroy them. The player, however, has some work cut out for him or her as
large- and medium sized asteroids break apart into two smaller asteroids when destroyed. And just
when the player manages to obliterate all of the asteroids, a new, larger wave appears. The player’s
score increases with every asteroid he or she destroys, but once the player’s ship collides with a floating
space rock, the game is over. Figures 12.1 and 12.2 show the game in action. I FIGURE 12.1 The player
controls a spaceship and blasts asteroids to increase his or her score. (Nebula image is in the public
domain. Credit: NASA, The Hubble Heritage Team—AURA/ STScI) FIGURE 12.2 If an asteroid hits the
player’s ship, the game is over. 362 Python Programming for the Absolute Beginner, Third Edition
READING THE KEYBOARD You already know how to get strings from the user through the input()
function, but reading the keyboard for individual keystrokes is another matter. Fortunately, there’s a
new object from the games module that lets you do just this. Introducing the Read Key Program The
Read Key program displays the ship on the nebula background. The user can move the ship around with
different keystrokes. When the user presses the W key, the ship moves up. When the user presses the S
key, the ship moves down. When the user presses the A key, the ship moves left. When the user presses
the D key, the ship moves right. The user can also press multiple keys simultaneously for a combined
effect. For example, when the user presses the W and D keys simultaneously, the ship moves diagonally,
up and to the right. The program is illustrated in Figure 12.3. FIGURE 12.3 The ship moves around the
screen based on keystrokes. You can find the code for the program on the companion website
([Link]/ downloads) in the Chapter 12 folder; the file name is read_key.py. Chapter 12 •
Sound, Animation, and Program Development: The Astrocrash Game 363 Setting Up the Program As I do
with all programs that use the livewires package, I start by importing the modules I need and calling the
initialization method: # Read Key # Demonstrates reading the keyboard from livewires import games
[Link](screen_width = 640, screen_height = 480, fps = 50) Testing for Keystrokes Next, I write a class
for the ship. In the update() method, I check for keystrokes and change the position of the ship
accordingly. class Ship([Link]): """ A moving ship. """ def update(self): """ Move ship based on
keys pressed. """ if [Link].is_pressed(games.K_w): self.y -= 1 if
[Link].is_pressed(games.K_s): self.y += 1 if [Link].is_pressed(games.K_a): self.x -= 1
if [Link].is_pressed(games.K_d): self.x += 1 I use the new keyboard object from the games
module. You can use the object to test for specific keypresses. I invoke the object’s is_pressed() method,
which returns True if the key being tested for is pressed, and False if not. I use the is_pressed() method
in a series of if statements to test if any of the four keys—W, S, A, or D—is being pressed. If the W key is
pressed, I decrease the object’s y property by 1, moving the sprite up the screen by one pixel. If the S
key is pressed, I increase the object’s y property by 1, moving the sprite down the screen. If the A key is
pressed, I decrease the object’s x property by 1, moving the sprite left. If the D key is pressed, I increase
the object’s x property by 1, moving the sprite right. 364 Python Programming for the Absolute
Beginner, Third Edition Since multiple calls to is_pressed() can read simultaneous keypresses, the user
can hold down multiple keys for a combined effect. For example, if a user holds down the D and S keys
at the same time, the ship moves down and to the right because each time update() executes, the Ship
object adds one to its x-coordinate and one to its y-coordinate. The games module has a set of constants
that represent keys that you can use as an argument in is_pressed(). In this program, I use the
games.K_w constant for the W key; games.K_s for the S key; games.K_a for the A key; and games.K_d
for the D key. The naming of these constants is pretty intuitive. Here’s a quick way to figure out the
name of most key constants: • All keyboard constants begin with games.K_. • For alphabetic keys, add
the key letter, in lowercase, to the end of the constant name. For example, the constant for the A key is
games.K_a. • For numeric keys, add the key number to the end of the constant name. For example, the
constant for the 1 key is games.K_1. • For other keys, you can often add their name, in all capital letters,
to the end of the constant name. For example, the constant for the spacebar is games.K_SPACE. For a
complete list of keyboard constants, see the livewires documentation in Appendix B