16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 29 ■ PROJECT 10: DO-IT-YOURSELF ARCADE GAME 531<br />

you need <strong>to</strong> make before you can get the pro<strong>to</strong>type up and running. First of all, you should<br />

make sure that you have Pygame installed, including the image and font modules. (You might<br />

want <strong>to</strong> import both of these in an interactive <strong>Python</strong> interpreter <strong>to</strong> make sure they are available.)<br />

You also need a couple of images. If you want <strong>to</strong> stick <strong>to</strong> the theme of the game as presented in<br />

this chapter, you need one image depicting a 16-<strong>to</strong>n weight, and one depicting a banana, both<br />

of which are shown in Figure 29-1. Their exact sizes aren’t all that important, but you might<br />

want <strong>to</strong> keep them in the range 100×100 through 200×200 pixels. (You might also want a separate<br />

image for the splash screen, the first screen that greets the user of your game. In this project,<br />

I simply use the weight symbol for that as well.) You should have these two images available in<br />

a common image file format such as GIF, PNG, or JPEG.<br />

Figure 29-1. The weight and banana graphics used in my version of the game<br />

First Implementation<br />

When you use a new <strong>to</strong>ol such as Pygame, it often pays off <strong>to</strong> keep the first pro<strong>to</strong>type as simple<br />

as possible and <strong>to</strong> focus on learning the basics of the new <strong>to</strong>ol, rather than the intricacies of the<br />

program itself. Let’s restrict the first version of Squish <strong>to</strong> an animation of 16-<strong>to</strong>n weights falling<br />

from above. The steps needed for this are as follows:<br />

1. Initialize Pygame, using pygame.init, pygame.display.set_mode, and<br />

pygame.mouse.set_visible. Get the screen surface with pygame.display.get_surface.<br />

Fill the screen surface with a solid white color (with the fill method) and call<br />

pygame.display.flip <strong>to</strong> display this change.<br />

2. Load the weight image.<br />

3. Create an instance of a cus<strong>to</strong>m Weight class (a subclass of Sprite) using the image. Add<br />

this object <strong>to</strong> a RenderUpdates group called (for example) sprites. (This will be particularly<br />

useful when dealing with multiple sprites.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!