How to add sprites to a PyGame Group

时间:2015-07-28 16:11:00

标签: python pygame

I've been at this a while, and essentially this is all the relevant code;

hero=pygame.sprite.Group()
snowballs=pygame.sprite.Group()

class main_character(pygame.sprite.Sprite):
    def __init__(self,pos,*groups):
       pygame.sprite.Sprite.__init__(self,*groups)
       self.image=direction
       self.rect=self.image.get_rect(topleft=pos)
       self.add(hero)


class my_snowball(pygame.sprite.Sprite):
    def __init__(self,pos,*groups):
        pygame.sprite.Sprite.__init__(self,*groups)
        self.image=snowball
        self.rect=self.image.get_rect(topleft=pos)
        self.add(snowballs)

print(hero)
print(snowballs)

where direction is the character image and snowball is the snowball image.

The print(hero) and print(snowballs) say that there aren't any sprites in those groups, and I was wondering why. The logic seems to be there, as I am adding the sprites with the rect attribute to the group.

I havn't found a tutorial which fixes this, thanks

1 个答案:

答案 0 :(得分:0)

你想要snowballs.add(self)和英雄一样。 pygame.sprite.Group.add(Sprite)。或者,也许你可以说global snowballsglobal hero