[Tutor] Why Won't My Pizza Fall?

Tiger12506 keridee at jayco.net
Sun Jan 13 01:05:26 CET 2008

> Hey There Everyone,
>  I'm following an example in a book and I can't find the error that's 
> preventing this program from running.  It's just an example of how to get 
> a sprite moving.  The images are all in the right folder.  I can run the 
> program and get a stationary sprite to appear.  The trouble seems to come 
> up when I add "dx" and "dy"  Here's the code.

I thank you for sending your error messages and the code! It made this very 

>  from livewires import games
>  class Pizza(games.Sprite):
>    """A falling pizza."""
>    def __init__(self, screen, x, y, image, dx, dy):
>        """Initialize pizza object."""
>        self.init_sprite(screen = screen, x = x, y = y, image = image,
>                             dx = dx, dy = dy)
>  #main
> my_screen = games.Screen(SCREEN_WIDTH, SCREEN_HEIGHT)
>  wall_image = games.load_image("skywall.png", transparent = False)
> my_screen.set_background(wall_image)
>  pizza_image = games.load_image("pizza.png")
> Pizza(screen = my_screen, x = SCREEN_WIDTH/2, y = SCREEN_HEIGHT/2,
>          image = pizza_image, dx = 0, dy = 1)
>  my_screen.mainloop()
>  ___________
>  Here's the error message:
>  Traceback (most recent call last):
>  File "C:/Python25/Chapter 11/movingsprite.py", line 25, in <module>
>    image = pizza_image, dx = 0, dy =1)
>  File "C:/Python25/Chapter 11/movingsprite.py", line 15, in __init__
>    dx = dx, dy = dy)
> TypeError: init_sprite() got an unexpected keyword argument 'dx'

Ok. This last line says that init_sprite (which is the function you call 
within your Pizza.__init__ will not accept the dx argument. (I would also 
expect that it won't accept dy either).

This is an easy fix. I'm sure you are aware that dx and dy are short for 
"change in x", "change in y" and all you have to do therefore is this.

def __init__(self, screen, x, y, image, dx, dy):
    """Initialize pizza object."""
    x += dx
    y += dy
    self.init_sprite(screen = screen, x = x, y = y, image = image)

I will warn you that this probably could be structured a little better. It 
is better, I believe, to keep your dx and your dy out of your class 
altogether and handle all of the repositioning code in your application 
(before you create a Pizza object) and let the Pizza object handle just it's 
state, not it's change in state. This is entirely stylistic however i think 
you will agree eventually.

More information about the Tutor mailing list