Joe Doe
Joe Doe

Reputation: 193

Pygame sprites group

While developing my game I came across slight problem with group of sprites. What I am trying to do is to be able to detect collision of the character(dog) with sprite(bone) and when that collision occurs, add points and change coordinates on the screen. At this stage, I am able to add points and place the bone in different location on the screen but the first instance of the bone stays in the same spot. How can I remove my sprite from one place of the screen to another?

My code:

sprites = pygame.sprite.Group()
sprites_no = 20

...

class MySprite(pygame.sprite.Sprite):
    def __init__(self, name):
        self.health = 3
        self.name = name
        self.points = 0

    def printName(self):
        print (self.name)


class Dog(MySprite):
    def __init__(self, pos, name):
        super(Dog, self).__init__(name)
        self.image = pygame.image.load("dog_left.png")
        self.rect = self.image.get_rect(topleft=pos)
        self.move_x = 0
        self.move_y = 0



    def draw(self, screen):
        screen.blit(self.image, self.rect)

    def takeBone(self):
        takebone = pygame.sprite.spritecollide(player, sprites, True)
        if takebone == True:
            self.points += 1
            bone.changePos()

...

class Bone(MySprite):
    def __init__(self, pos, name):
        super(Bone, self).__init__(name)
        self.name = name
        self.image = pygame.image.load("bone.png")
        self.rect = self.image.get_rect(topleft=pos)

...

for i in range(sprites_no):
    sp1_x = random.randrange(0, 475)
    sp1_y = random.randrange(0, 275)
    sp1 = Bone([sp1_x, sp1_y],"Bone")
    sprites.append(sp1)

The error I am getting at the moment is: Traceback (most recent call last): File "E:\game\new.py", line 86, in sprites.append(sp1) AttributeError: 'Group' object has no attribute 'append'

Any suggestions are welcome

Upvotes: 0

Views: 453

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122090

A pygame.sprite.Group is not the same as a list; you cannot append to it. Instead, per the documentation, you should add to it:

sprites.add(sp1)

Upvotes: 2

Related Questions