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 539<br />

"""<br />

self.rect.centerx = pygame.mouse.get_pos()[0]<br />

self.rect = self.rect.clamp(self.area)<br />

def <strong>to</strong>uches(self, other):<br />

"""<br />

Determines whether the banana <strong>to</strong>uches another sprite (e.g., a<br />

Weight). Instead of just using the rect method colliderect, a<br />

new rectangle is first calculated (using the rect method<br />

inflate with the side and <strong>to</strong>p paddings) that does not include<br />

the 'empty' areas on the <strong>to</strong>p and sides of the banana.<br />

"""<br />

# Deflate the bounds with the proper padding:<br />

bounds = self.rect.inflate(-self.pad_side, -self.pad_<strong>to</strong>p)<br />

# Move the bounds so they are placed at the bot<strong>to</strong>m of the Banana:<br />

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

# Check whether the bounds intersect with the other object's rect:<br />

return bounds.colliderect(other.rect)<br />

Listing 29-4. The Main Game Module (squish.py)<br />

import os, sys, pygame<br />

from pygame.locals import *<br />

import objects, config<br />

"This module contains the main game logic of the Squish game."<br />

class State:<br />

"""<br />

A generic game state class that can handle events and display<br />

itself on a given surface.<br />

"""<br />

def handle(self, event):<br />

"""<br />

Default event handling only deals with quitting.<br />

"""<br />

if event.type == QUIT:<br />

sys.exit()<br />

if event.type == KEYDOWN and event.key == K_ESCAPE:<br />

sys.exit()<br />

def firstDisplay(self, screen):<br />

"""<br />

Used <strong>to</strong> display the State for the first time. Fills the screen<br />

with the background color.<br />

"""

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

Saved successfully!

Ooh no, something went wrong!