pygame-community / pygame-ce

🐍🎮 pygame - Community Edition is a FOSS Python library for multimedia applications (like games). Built on top of the excellent SDL library.
https://pyga.me
939 stars 155 forks source link

Sprites and Sprite Groups paired into 1 class #3097

Closed CraftyRyte closed 2 months ago

CraftyRyte commented 2 months ago

What have I done?

I have combined the Sprite class and GroupSingle into 1 SingleSpriteObject class.

Objective

Code Added:

class SingleSpriteObject(Sprite):
    def __init__(self, position, hitbox_dimensions, color=None, image_path=None):
        super().__init__()
        self.pos = pygame.math.Vector2(position)
        self.dimensions = hitbox_dimensions

        if isinstance(image_path, str) and image_path:
            self.image = pyg.image.load(image_path).convert()
            self.image = pyg.transform.scale(self.image, self.dimensions)
        elif color:
            self.image = pyg.Surface(self.dimensions)
            self.image.fill(self.color)
            self.color = color

        self.rect = self.image.get_frect(topleft=self.pos)
        self.group_single = GroupSingle()
        self.group_single.add(self)

    def update(self):
        if self in self.spr_group:
            self.spr_group.remove(self)
            self.spr_group.update()
            self.spr_group.add(self)
        else:
            self.spr_group.update()

        self.spr_group.draw(pygame.display.get_surface())

class SpriteGroupObject(SingleSpriteObject):
    def __init__(self, position, hitbox_dimensions, *sprites, color=None, image_path=None):
        super().__init__(position, hitbox_dimensions, color, image_path)
        del self.group_single
        self.group = Group(*sprites)
MyreMylar commented 2 months ago

I don't see any good justification for this.

If you want to draw a sprite without a sprite group you can already do that.