Search code examples
pygamebordersprite

Pygame - add border to sprite


I'm a bit confused. I have a class that inherits from the pygame.sprite.Sprite class. I want to add a border around the sprite when it shows on the screen, but the border does not appear

class Bird(pygame.sprite.Sprite):
    def __init__(self, filename, x, y):
        super().__init__()
        
        self.image = pygame.image.load('bird.png')
        self.rect = self.image.get_rect(topleft = (x, y))

    def draw(self, screen):
        pygame.draw.rect(screen, red, self.rect, 2)

When I call the sprite in the game loop, no border appears

bird_group.draw(screen)
bird_group.update()

I guess I'm doing something wrong but not sure what!


Solution

  • pygame.sprite.Group.draw does not call the draw method of the Sprite. [pygame.sprite.Group.draw() uses the image and rect attributes of the contained pygame.sprite.Sprites to draw the objects — you have to ensure that the pygame.sprite.Sprites have the required attributes. See pygame.sprite.Group.draw():

    Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect. [...]

    Therefore you must draw the rectangle on the image:

    class Bird(pygame.sprite.Sprite):
        def __init__(self, filename, x, y):
            super().__init__()
            
            self.image = pygame.image.load('bird.png')
            self.rect = self.image.get_rect(topleft = (x, y))
            pygame.draw.rect(self.image, red, self.image.get_rect(), 2) # image.get_rect() not rect!
    

    The border is drawn on self.image. It can't be removed. You have to use 2 images and switch the images:

    class Bird(pygame.sprite.Sprite):
        def __init__(self, filename, x, y):
            super().__init__()
            
            self.image_no_border = pygame.image.load('bird.png')
            self.image_border = self.image.copy()
            pygame.draw.rect(self.image_border, red, self.image.get_rect(), 2) 
    
            self.border = True
            self.image = self.image_border if self.border else self.image_no_border
            self.rect = self.image.get_rect(topleft = (x, y))
    
        def click(self, pos):         
            if self.rect.collidepoint(pos):
                self.border = not self.border 
                self.image = self.image_border if self.border else self.image_no_border