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.

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

pygame.sprite<br />

This module contains two very important classes: Sprite and Group.<br />

The Sprite class is the base class for all visible game objects—in the case of this project,<br />

the banana and the 16-<strong>to</strong>n weight. To implement your own game objects, you subclass Sprite,<br />

override its construc<strong>to</strong>r <strong>to</strong> set its image and rect properties (which determine how the Sprite<br />

looks, and where it is placed), and override its update method, which is called whenever the<br />

sprite might need updating.<br />

Instances of the Group class (and its subclasses) are used as containers for Sprites. In<br />

general, using groups is a Good Thing. In simple games (such as in this project), simply create<br />

a group called sprites or allsprites or something similar, and add all your Sprites <strong>to</strong> it. When<br />

you call the Group object’s update method, the update methods of all your Sprite objects will<br />

then be called au<strong>to</strong>matically. Also, the Group object’s draw method can be used <strong>to</strong> draw all the<br />

Sprite objects it contains.<br />

In this project, I use the RenderUpdates subclass of Group, whose draw method returns a list<br />

of rectangles that have been affected. These may then be passed <strong>to</strong> pygame.display.update <strong>to</strong><br />

update only the parts of the display that need <strong>to</strong> be updated. This can greatly improve the<br />

performance of the game.<br />

pygame.mouse<br />

In Squish, I use this module for just two things: hiding the mouse cursor, and getting the mouse<br />

position. You hide the mouse with pygame.mouse.set_visible(False), and you get the position<br />

with pygame.mouse.get_pos().<br />

pygame.event<br />

This module keeps track of various events such as mouse clicks, mouse motion, keys that<br />

are pressed or released, and so on. To get a list of the most recent events, use the function<br />

pygame.event.get.<br />

■Note If you rely only on state information such as the mouse position returned by pygame.mouse.<br />

get_pos, you don’t have <strong>to</strong> use pygame.event.get. However, you need <strong>to</strong> keep the Pygame updated<br />

(“in sync”), which you can do by calling the function pygame.event.pump regularly.<br />

pygame.image<br />

This module is used <strong>to</strong> deal with images such as those s<strong>to</strong>red in GIF, PNG, or JPEG files (or<br />

indeed several other formats). In this project, you only need the load function, which reads an<br />

image file and creates a surface object containing the image.<br />

Preparations<br />

Now that you know a bit about what some of the different Pygame modules do, it’s almost time<br />

<strong>to</strong> start hacking away at the first pro<strong>to</strong>type game. There are, however, a couple of preparations

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

Saved successfully!

Ooh no, something went wrong!