Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i remove the black bg color of an image with no alpha layer

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading