At this point I don't really care if the sprites write over eachother...as
long as that doesn't cause the program to crash.  I'm just trying to get
four circles going diagonally from each corner.  The problem I'm having is
with the update function...Do I write two update functions?  How do I get
the update function to apply separate code for the two (or more) circles?
Here is my code as it stands:

import pygame
pygame.init()

screen = pygame.display.set_mode((640, 480))

class Circle(pygame.sprite.Sprite):
    def __init__(self, color):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill((255, 255, 255))
        pygame.draw.circle(self.image, (color), (25, 25), 25)
        self.rect = self.image.get_rect()
        self.startPosx = self.rect.centerx
        self.startPosy = self.rect.centery
        self.dx = 5
        self.dy = 5


    def update(self):
            self.rect.centerx += self.dx
            self.rect.centery += self.dy
            if self.rect.right > screen.get_width():
                self.rect.left = 0
            if self.rect.top > screen.get_height():
               self.rect.bottom = 0

    def update_circle2(self):
        self.rect.centerx -= self.dx
        self.rect.centery += self.dy
        if self.rect.left < 0:
            self.rect.right = 0
        if self.rect.top > screen.get_height():
            self.rect.bottom = 0

def main():
    pygame.display.set_caption("Diagonal Circle")

    background = pygame.Surface(screen.get_size())
    background.fill((255, 255, 255))
    screen.blit(background, (0, 0))

    circle1 = Circle(pygame.color.Color("yellow"))
    circle2 = Circle(pygame.color.Color("red"))
    circle1.startPosx = 0
    circle1.startPosy = 0
    circle2.startPosx = screen.get_width()
    circle2.startPosy = 0


    allSprites = pygame.sprite.Group(circle1, circle2)

    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

        allSprites.clear(screen, background)
        allSprites.update()
        allSprites.draw(screen)

        pygame.display.flip()

if __name__ == "__main__":
    main()

pygame.quit()







On Sat, Oct 2, 2010 at 6:43 PM, Ian Mallett <geometr...@gmail.com> wrote:

> Hi,
>
> Your code only makes one circle.  How can you possibly expect four circles,
> when you only make one?  From the spec, you need to add other instances of
> your circle sprite in your pygame.sprite.Group() call.  Hint: try
> "[circle1,circle2,circle3,circle4]"
>
> If you tried this, then you'll see (or rather won't, but that's what's
> happening) that all the sprites draw over each other.  You'll need to write
> separate logic for each sprite's update function, or all the sprites will
> draw on top of each other.  I've done this by passing (varying) extra
> arguments to your sprites' constructors to alter their internal states.
>
> Ian
>

Reply via email to