[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Newbie trouble.



On Fri, 2006-03-10 at 03:10 +0800, Rajesh Sathyamoorthy wrote:
> i am new to pygame . I created a game where a chef
> starts throwing down pizzas and the player uses a pan which is controlled 

> The problem is when a  new pizza appears the old pizza dissapears i
> really can't figure why.

This is a great first project for learning pygame. Looking at the code,
I can't see anything immediately wrong. It took a bit of detective work,
but I've found it.


class Pizza(pygame.sprite.Sprite):
    def __init__(self, chef):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.image, self.rect = Pizza.image
        self.rect.center = chef.rect.center


Every pizza object is being assigned the same rectangle object. This is
good for self.image, because they all share the same image data (which
doesn't change). This is bad for self.rect, because each Pizza is moving
the rectangle for its own position.

So you really do have all the pizzas on screen, they are just all
sharing the same positioning. The easiest fix at this point will be for
each pizza to copy the rectangle.

    def __init__(self, chef):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.image, self.rect = Pizza.image
        self.rect = pygame.Rect(self.rect)
        self.rect.center = chef.rect.center

This was a tricky problem. Only when I added some printing did I realize
that all the pizzas were onscreen, so I figured it must be some shared
resource like this.