0

I am trying to read in images from a sprite sheet, the problem is when I use a Surface to get a single image the background becomes black and if I use pygame.Surface.set_colorkey(image, [0, 0, 0]) then it removes the black that is in the single image. The background of the images is transparent.

class SpriteSheet:
    def __init__(self, path):
        pygame.init()
        self.path = path
        self.sheet = pygame.image.load(path).convert_alpha()

    def get_images(self):
        sprite = pygame.Surface((IMAGE_SIZE, IMAGE_SIZE)).convert_alpha()
        sprite.blit(self.sheet, (0,0), (0, 0, IMAGE_SIZE, IMAGE_SIZE))
        pygame.Surface.set_colorkey(sprite, [0, 0, 0])
        print(pygame.Surface.get_colorkey(sprite))
        return sprite
Nick Ray
  • 1
  • 1

1 Answers1

0

if you create a new surface and use convert_alpha(), the alpha channels are initially set to maximum. The initial value of the pixels is (0, 0, 0, 255). You need to fill the entire surface with a transparent color before you can draw anything on it. Set the SRCALPHA flag to create a surface with an image format that includes a per-pixel alpha. The initial value of the pixels is (0, 0, 0, 0):

sprite = pygame.Surface((IMAGE_SIZE, IMAGE_SIZE)).convert_alpha()

sprite = pygame.Surface((IMAGE_SIZE, IMAGE_SIZE), pygame.SRCALPHA)

Also remove set_colorkey from your code as it is not necessary.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174