[Tutor] Why Won't My Pizza Fall?

Alan Gauld alan.gauld at btinternet.com
Sun Jan 13 02:05:08 CET 2008


"Tiger12506" <keridee at jayco.net> wrote

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

I totally agree and the answer is correct however...

> 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

I strongly disagree with this bit :-)

> (before you create a Pizza object) and let the Pizza object handle 
> just it's
> state, not it's change in state.

Objects manage their own state, any change to the state of
an object should be made by the object itself (via a method)
It is extremely bad OOP practice to have other objects (or
the "main" programme) determining the internal state of
another object. (This is why blanket getter/setter methods
are such a bad idea from an OOP point of view)

However, I do agree that an init method is probably the wrong
place to have a dx/dy pair since we should, I would ex[pect,
be positioning the object initially before trying to move it.
Thus I'd expect a move metjod someplasce that took the
dx and dy. Something like:

  class Pizza(games.Sprite):
    """A falling pizza."""
    def __init__(self, screen, x, y, image):
        """Initialize pizza object."""
        self.init_sprite(screen = screen, x = x, y = y, image = image)
        self.x = x
        self.y = y

    def fall(self, dx=0,dy=0):
        self.x += dx
        self.y += dy
        self.delete_sprite()    # clear the old
        self.init_sprite(self.x, self.y) # draw the new

However since in re-reading the description I think this Pizza
is supposed to fall automatically the default drop could be
passed in to the init, but we should try to arrange for the fall
to be called periodically(by mainloop?)

But in either case the calculation of the new x and y values
should be done by the object itself. If it isn't supposed to
know where it is then what does?

Alan G.




More information about the Tutor mailing list