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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

def __init__(self, speed):<br />

SquishSprite.__init__(self, config.weight_image)<br />

self.speed = speed<br />

self.reset()<br />

def reset(self):<br />

"""<br />

Move the weight <strong>to</strong> the <strong>to</strong>p of the screen (just out of sight)<br />

and place it at a random horizontal position.<br />

"""<br />

x = randrange(self.area.left, self.area.right)<br />

self.rect.midbot<strong>to</strong>m = x, 0<br />

def update(self):<br />

"""<br />

Move the weight vertically (downwards) a distance<br />

corresponding <strong>to</strong> its speed. Also set the landed attribute<br />

according <strong>to</strong> whether it has reached the bot<strong>to</strong>m of the screen.<br />

"""<br />

self.rect.<strong>to</strong>p += self.speed<br />

self.landed = self.rect.<strong>to</strong>p >= self.area.bot<strong>to</strong>m<br />

class Banana(SquishSprite):<br />

"""<br />

A desperate banana. It uses the SquishSprite construc<strong>to</strong>r <strong>to</strong> set up<br />

its banana image, and will stay near the bot<strong>to</strong>m of the screen,<br />

with its horizontal position governed by the current mouse<br />

position (within certain limits).<br />

"""<br />

def __init__(self):<br />

SquishSprite.__init__(self, config.banana_image)<br />

self.rect.bot<strong>to</strong>m = self.area.bot<strong>to</strong>m<br />

# These paddings represent parts of the image where there is<br />

# no banana. If a weight moves in<strong>to</strong> these areas, it doesn't<br />

# constitute a hit (or, rather, a squish):<br />

self.pad_<strong>to</strong>p = config.banana_pad_<strong>to</strong>p<br />

self.pad_side = config.banana_pad_side<br />

def update(self):<br />

"""<br />

Set the Banana's center x-coordinate <strong>to</strong> the current mouse<br />

x-coordinate, and then use the rect method clamp <strong>to</strong> ensure<br />

that the Banana stays within its allowed range of motion.

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

Saved successfully!

Ooh no, something went wrong!