[Tutor] Pizza panic game
Dave Angel
d at davea.name
Sun Feb 5 02:23:09 CET 2012
On 02/04/2012 06:43 PM, myles broomes wrote:
> Im currently using a book called 'Programming in Python for the complete beginner' and at the end of each chapter, the reader is given challenges to do. The current chapter im on, one of the challenges is to take the source code for a 'Pizza Panic' game , and find a way to make the game more difficult. Basically, the premise of the game is the player controls a pan and has to try and catch falling pizzas that a computer controlled chef is dropping, and failure to drop any results in game over. The idea I came up with is as well as the chef dropping the pizzas, he also drops hazardous spikey balls that the player has to try and avoid. I'll try to explain the part that im having trouble with:
>
> Heres the code for the 'Spikey ball' class that creates the spikey ball object:
>
> class Spikey_ball(games.Sprite):
> """A hazardous spikey ball that falls to the ground."""
> image = games.load_image("Spikey_ball.png")
> speed = 0.5
>
> def __init__(self, x, y = 90):
> """Initialise the spikey ball object."""
> super(Spikey_ball, self).__init__(image = Spikey_ball.image,
> x = x, y = y,
> dy = Spikey_ball.speed)
> def update(self):
> """Check if botton edge has reached the screen."""
> if self.bottom> games.screen.height:
> self.destroy()
>
> def handle_caught(self):
> """Destroy if caught and end game."""
> if self.bottom> Pan.top:
> Pizza.end_game()
> self.destroy()
>
>
> ...And heres the code for the pan class:
>
> class Pan(games.Sprite):
> """
> A pan controlled by player to catch falling pizzas.
> """
> image = games.load_image("pan.bmp")
>
> def __init__(self):
> """ Initialize Pan object and create Text object for score. """
> super(Pan, self).__init__(image = Pan.image,
> x = games.mouse.x,
> bottom = games.screen.height)
>
> self.score = games.Text(value = 0, size = 25, color = color.black,
> top = 5, right = games.screen.width - 10)
> games.screen.add(self.score)
>
> def update(self):
> """ Move to mouse x position. """
> self.x = games.mouse.x
>
> if self.left< 0:
> self.left = 0
>
> if self.right> games.screen.width:
> self.right = games.screen.width
>
> self.check_catch()
>
> def check_catch(self):
> """ Check if catch pizzas. """
> for pizza in self.overlapping_sprites:
> pizza.handle_caught()
>
> As you can see, when a sprite overlaps the pan object, the handle_caught attribute of said object is invoked. But the weird thing is, when a pizza is caught, it works fine and the pizza's handle_caught attribute is invoked without complaint. However, when a spikey ball is caught, the game comes up with an error and claims that the Spikey_ball object has no handle_caught attribute, but as you can see, it clearly does. Can anyone explain this to me?
>
There is a function handle_caught(), but it's not a method of the class,
it's a nested function of method __init__()
Unless of course, you haven't posted correctly, and the indentation
doesn't match what you're really using.
--
DaveA
More information about the Tutor
mailing list