I have a grid of sprites, so I was thinking that I should create a Group
subclass that stores the sprites as a 2D array, but properly interacts with the sprites as elements when I call .draw()
, etc., i.e. group.draw()
should be similar to calling for sprite in group.sprites(): sprite.draw()
.
The PyGame documentation mentions a class called AbstractGroup
, but doesn't actually say what it is. I found more of the same information here as well.
When I've been inheriting the Sprite
class, it is sufficient to assign self.rect
and self.image
. So I was expecting something similar for Group
s, i.e. maybe I simply implement something like self.sprites
or __iter__
in order to correctly implement contains()
, sprites()
, draw()
, etc.
Here's some code that I was in the middle of writing which might illustrate what I'm trying to achieve:
class SpriteGrid(pg.sprite.Group):
def __init__(self, list_of_lists_of_sprites):
self.sprites = list_of_lists_of_sprites
def __iter__(self):
# concatenate the iterators for each row of self.sprites
return iter(functools.reduce(lambda a,b: itertools.chain(a, b), self.sprites)