1

I'm trying to make a sprite have two separate rects/hitbox. Is there any way I can achieve this?

without using two separate sprites?

Here's my current code and the spritesheet:

I'm using

class Tree(pygame.sprite.Sprite):
    def __init__(self, pos):
        super(Tree, self).__init__()
        self.image = pygame.Surface((48 * 3, 48 * 4), pygame.SRCALPHA)
        self.image_path = "objects.png"
        self.sprite_sheet = pygame.image.load(self.image_path).convert_alpha()

        self.tree_top = pygame.Surface((16 * 3, 16 * 3), pygame.SRCALPHA)
        self.tree_top.blit(self.sprite_sheet, (0, 0), (0, 80, 16 * 3, 16 * 3))
        self.tree_top = pygame.transform.scale(self.tree_top, (48 * 3, 48 * 3))
        self.image.blit(self.tree_top, (0, 0))

        self.tree_trunk = pygame.Surface((48 * 3, 48), pygame.SRCALPHA)
        self.tree_trunk.blit(self.sprite_sheet, (0, 0), (0, 128, 16 * 3, 16))
        self.tree_trunk = pygame.transform.scale(self.tree_trunk, (48 * 3 * 3, 48 * 3))
        self.image.blit(self.tree_trunk, (0, 48 * 3))

        self.rect = self.image.get_rect()
        self.rect.topleft = pos
        self.hitbox = self.rect.copy()

Spritesheet:

spritesheet

Javad
  • 2,033
  • 3
  • 13
  • 23
  • 1
    A sprite has only 1 bounding rectangle. However have a look into [mask collision between two sprites](https://stackoverflow.com/questions/71535185/cant-figure-out-how-to-check-mask-collision-between-two-sprites/71536155#71536155) – Rabbid76 Dec 09 '22 at 11:18
  • I'm trying to go for a more retro look with rect based collision. Is it possible I could instead use sprite groups as the superclass? But I am rendering these sprites by adding them to a sprite group and I don't think nested groups work – Alex Ramirez Dec 09 '22 at 15:53
  • The easiest way is to split the sprite from the sheet into 2 parts and create 2 sprite objects. With this approach, you don't need to implement anything special. – Rabbid76 Dec 09 '22 at 15:59
  • I was wrong, you can draw sprite groups inside sprite groups, I followed your advice and separated them into two different sprites and then combining them in another custom sprite group class. – Alex Ramirez Dec 09 '22 at 16:16

1 Answers1

0

I don't know if this is safe way but I separated them into two different sprites and combining them again in a custom sprite group.

class Tree(pygame.sprite.Group):
   def __init__(self, pos):
       super(Tree, self).__init__()
       self.tree_top = TreeTop(pos)
       self.tree_trunk = TreeTrunk((pos[0], pos[1] + (48 * 3)))
       self.add(self.tree_top)
       self.add(self.tree_trunk)


class TreeTop(pygame.sprite.Sprite):
   def __init__(self, pos):
       super(TreeTop, self).__init__()
       self.image = pygame.Surface((48 * 3, 48 * 3), pygame.SRCALPHA)
       self.image_path = "objects.png"
       self.tree_top = pygame.Surface((16 * 3, 16 * 3), pygame.SRCALPHA)
       self.sprite_sheet = pygame.image.load(self.image_path).convert_alpha()
       self.tree_top.blit(self.sprite_sheet, (0, 0), (0, 80, 16 * 3, 16 * 3))
       self.tree_top = pygame.transform.scale(self.tree_top, (48 * 3, 48 * 3))
       self.image.blit(self.tree_top, (0, 0))

       self.rect = self.image.get_rect()
       self.rect.topleft = pos
       self.hitbox = self.rect.copy()


class TreeTrunk(pygame.sprite.Sprite):
   def __init__(self, pos):
       super(TreeTrunk, self).__init__()
       self.image = pygame.Surface((48 * 3, 48), pygame.SRCALPHA)
       self.image_path = "objects.png"
       self.sprite_sheet = pygame.image.load(self.image_path).convert_alpha()

       self.tree_trunk = pygame.Surface((48 * 3, 48), pygame.SRCALPHA)
       self.tree_trunk.blit(self.sprite_sheet, (0, 0), (0, 128, 16 * 3, 16))
       self.tree_trunk = pygame.transform.scale(self.tree_trunk, (48 * 3 * 3, 48 * 3))
       self.image.blit(self.tree_trunk, (0, 0))

       self.rect = self.image.get_rect()
       self.rect.topleft = pos
       self.hitbox = self.rect.copy()