0

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 Groups, 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)
awe lotta
  • 101
  • 3
  • Are you intending anything fancy with your subclass? The constructor already takes any number of sprites to add to the group and provides an iterator. E.g. see [`SpriteGroup.has(*sprites)`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.has) for `in` equivalence. – import random Jun 27 '22 at 22:46
  • @importrandom I am not overly concerned with testing/handling multiple sprites at a time. I am more concerned with creating a group that stores its sprites in an organized manner; the existing `Group` class seems to be something like a set of sprites, rather than a 2d array or something more structured – awe lotta Jun 28 '22 at 06:57
  • 1
    Don't the sprites know their own location, via their `self.rect` attribute? Why should the group care? I don't understand what you're trying to accomplish, it's sounding like an [X Y Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – import random Jun 28 '22 at 07:26
  • It might be an XY problem. Since all the sprites are related in that they lie on a grid (and in my case they're all the same type of object), I figured they should all be handled by one "Grid" object. Since the Grid holds sprites, it is like a `Group`, and if I were to create a subclass, it would be convenient to call `GridGroup.draw()` and `GridGroup.update()` to handle all the sprites together. But maybe that's not what `Group` is for , and maybe it's not necessary to equate the grid and group. – awe lotta Jun 28 '22 at 18:45
  • That's a great pun, but I'm worried it might be unintentional. I mean that you've focused on a solution to the problem but the real problem is something else. It's still not clear to me what extra functionality you want to include in your custom `Group`. – import random Jun 29 '22 at 22:59
  • The extra feature I want is for the collection of sprites to be represented as a 2D array. Then I can refer to specific sprites by their position in the array. – awe lotta Jul 01 '22 at 18:49

0 Answers0