Reputation: 100
I have got a base class called 'Block'
, that has its sprite groups set to game.all_sprites
and game.blocks
. I have a 'ground'
subclass inheriting from the block class, but I am unable to get the ground to change sprite groups. Does anyone know how to do this?
Block class:
class Block(pygame.sprite.Sprite):
def __init__(self, game, x, y, collidable=True):
self.groups = game.all_sprites, game.blocks
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = pygame.Surface((SCALE, SCALE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.collidable = collidable
def update(self):
self.rect.x = self.x * SCALE
self.rect.y = self.y * SCALE
Ground class:
class Ground(Block):
def __init__(self, game, x, y, collidable=False):
self.groups = game.all_sprites, game.grounds
Block.__init__(self, game, x, y, collidable)
Thanks!
Upvotes: 2
Views: 695
Reputation: 101122
You could use a method that you can override to define the groups:
class Block(pygame.sprite.Sprite):
def __init__(self, game, x, y, collidable=True):
pygame.sprite.Sprite.__init__(self, self.get_groups(game))
self.game = game
...
def get_groups(self, game):
return game.all_sprites, game.blocks
class Ground(Block):
def __init__(self, game, x, y, collidable=False):
Block.__init__(self, game, x, y, collidable)
def get_groups(self, game):
return game.all_sprites, game.grounds
or store the attribute names of the sprite groups as class attribute and look them up dynamically, like this:
class Block(pygame.sprite.Sprite):
groups = ['all_sprites', 'blocks']
def __init__(self, game, x, y, collidable=True):
pygame.sprite.Sprite.__init__(self, (getattr(game, g) for g in self.groups))
self.game = game
...
class Ground(Block):
groups = ['all_sprites', 'grounds']
def __init__(self, game, x, y, collidable=False):
Block.__init__(self, game, x, y, collidable)
or pass the groups as parameter from the sub class to the parent class:
class Block(pygame.sprite.Sprite):
def __init__(self, game, x, y, collidable=True, groups=None):
pygame.sprite.Sprite.__init__(self, groups or (game.all_sprites, game.blocks))
self.game = game
...
class Ground(Block):
def __init__(self, game, x, y, collidable=False, groups=None):
Block.__init__(self, game, x, y, collidable, groups or (game.all_sprites, game.grounds))
In fact, the possibilities are endless:
class Block(pygame.sprite.Sprite):
def __init__(self, game, x, y, collidable=True):
groups = game.all_sprites, getattr(game, self.__class__.__name__.lower() + 's')
pygame.sprite.Sprite.__init__(self, groups)
self.game = game
...
I would probably use option 3, because it's explicit and has no fancy clever parts involved.
Upvotes: 2