2

ok here is the peice of code

lives_group = pygame.sprite.Group()
life_pos = [603,566,529,492]
pos = 0
for life in range(0,ship.lives):
    live = game_display.Life([10, life_pos[pos]])
    lives_group.add(live)

the .Life ting goes like this:

class Life(pygame.sprite.Sprite):
    def __init__(self, location):
        self.image = pygame.image.load('Spaceship_life.png')
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location

and this is the error I get:

Traceback (most recent call last):
  File "C:\Users\------\My programs\old\GUI based games\Spaceblaster\0.1\main.py", line 17, in <module>
    lives_group.add(live)
  File "C:\Python33\lib\site-packages\pygame\sprite.py", line 360, in add
    sprite.add_internal(self)
  File "C:\Python33\lib\site-packages\pygame\sprite.py", line 163, in add_internal
    self.__g[group] = 0
AttributeError: 'Life' object has no attribute '_Sprite__g'

sorry for -ing out my name but I kept it's length in case it matters

Menachem Hornbacher
  • 2,080
  • 2
  • 24
  • 36

1 Answers1

9

Don't forget to call parent's constructor

class Life(pygame.sprite.Sprite):
    def __init__(self, location):
        super(Life, self).__init__()
        self.image = pygame.image.load('Spaceship_life.png')
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
Igonato
  • 10,175
  • 3
  • 35
  • 64