So i started doing a game to pass the time and i don’t know how to solve this issue: my player is a part of a sprite sheet, the sprite sheet has a alpha layer so its transparent but when i divide my sprite sheet into small sprite, this alpha layer disapear and i have a black bg instead… I tried using set_colorkey([0, 0, 0]) to remove the black bg, but beceause my player is dark skinned, my player partially disappear. Any suggestion?
import pygame class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.image.load("assets/img/plr.png") self.image = self.get_image(0, 0) self.image = pygame.transform.scale(self.image, (96, 96)) self.image.set_colorkey([0, 0, 0]) self.rect = self.image.get_rect(center=(250, 250)) def get_image(self, x, y): image = pygame.Surface([48, 48]) image.blit(self.image, (0, 0), (x, y, 48, 48)) return image def update(self): pass
Advertisement
Answer
The alpha channel “disappears” because you create a Surface without alpha channel (RGB). You have to use the SRCALPHA
flag to create a Surface with an alpha channel (RGBA). Also see pygame.Surface
:
image = pygame.Surface([48, 48])
image = pygame.Surface([48, 48], pygame.SRCALPHA)
For a surface without alpha channels you can set the transparent color key with set_colorkey
:
image = pygame.Surface([48, 48]) image.set_colorkey((0, 0, 0))