[Tutor] Tutor Digest, Vol 96, Issue 12

myles broomes mylesbroomes at hotmail.co.uk
Sun Feb 5 12:56:33 CET 2012


The error message im getting is:
'AttributeError: 'Spikey_ball' object has no attribute 'handle_caught'.
 

> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Sat, 4 Feb 2012 23:43:32 +0000
> From: myles broomes <mylesbroomes at hotmail.co.uk>
> To: <tutor at python.org>
> Subject: [Tutor] Pizza panic game
> Message-ID: <DUB102-W52C53A0248D70E73CD099197760 at phx.gbl>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> 
> 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? 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20120204/5be275ca/attachment-0001.html>
> 
> ------------------------------
> 
> Message: 2
> Date: Sun, 05 Feb 2012 00:16:55 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Pizza panic game
> Message-ID: <jgkhpo$siu$1 at dough.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> On 04/02/12 23:43, myles broomes wrote:
> 
> > 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?
> 
> Please post the entire error message, do not summarize.
> There is often important information in the message.
> 
> Meantime can you try adding a print line in your last
> method to check that the attribute(i.e. method) does
> actually exist in the executing code at the point
> of use?
> ie.
> 
> def check_catch(self):
> """ Check if catch pizzas. """
> for pizza in self.overlapping_sprites:
> >>>> print (dir(pizza)) #<<< new line
> pizza.handle_caught()
> 
> hth,
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> 
> 
> ------------------------------ 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120205/53d9b5ff/attachment.html>


More information about the Tutor mailing list