I am trying to make a sprite which spawns multiple times across the screen. The sprite will be used in a collision. The sprite previously had a randrange issue which I believe has been fixed. But none the less there is an issue with the sprite group.
AttributeError: 'Mob' object has no attribute '_Sprite__g'
class Mob(pygame.sprite.Sprite):
def __init__(self, x, y):
self.image = pygame.Surface((90, 90)).convert_alpha()
self.image = pygame.image.load(badguy_file).convert_alpha()
self.image = pygame.transform.scale(self.image, (100, 100))
self.rect = pygame.Rect(x, y, 100, 100)
self.x = x
self.y = y
self.rect.x = random.randrange(800 - 100)
self.rect.y = random.randrange(-100, -40)
self.speedx = random.randrange(4)
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect.y += self.speedy
if self.rect.top > height + 10 or self.rect.left < -25 or self.rect.right > width + 20:
self.rect.x = random.randrange(width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedx = random.randrange(1,0)
def render(self, screen):
screen.blit(self.image, (self.x, self.y))
mobs = pygame.sprite.Group()
for i in range(8):
mob = Mob(200,300)
mobs.add(mob)
You have to call the __init__
function of Sprite
in Mob's __init__
function (or use super()
, depending on your python version), like
class Mob(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__() # Preferred
...
...
or
class Mob(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self) # For older versions
...
...