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.Sprite
s to draw the objects — you have to ensure that the pygame.sprite.Sprite
s 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