Pygame : image.convert_alpha() won't work

Question:

So, I’m making a prototype of game using Pygame, Pytmx, and PyScroll (I’m following "Graven" Turorial on youtube)

This is my problem : I have a spritesheet (96×38 pixels), and I want to get only one image of it..

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
    super(Player, self).__init__()

    self.spritesheet = pygame.image.load('assets/imgs/player0.png')

    self.image = pygame.Surface([12, 19]).convert_alpha()
    self.image.blit(self.spritesheet.convert_alpha(), (0, 0))

But when I launch it, it works, but there’s a black background, instead of a transparent background.. Is there a problem with it ? I tried using "set_colorkey(color)" on "self.spritesheet" AND on "self.image", I tried using "convert_alpha()" on both of them, and also tried making the pygame.Surface background transparent..

(Code):
Player Class:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Player, self).__init__()

        self.spritesheet = pygame.image.load('assets/imgs/player0.png')

        self.image = pygame.Surface([12, 19]).convert_alpha()
        self.image.blit(self.spritesheet.convert_alpha(), (0, 0))

        self.position = [x, y]

        self.rect = self.image.get_rect()
        self.speed = 3

    def update(self, *args, **kwargs) -> None:
        keys = pygame.key.get_pressed()

        if any((keys[pygame.K_UP], keys[pygame.K_w])):
            self.move_up()
        if any((keys[pygame.K_DOWN], keys[pygame.K_s])):
            self.move_down()
        if any((keys[pygame.K_LEFT], keys[pygame.K_a])):
            self.move_left()
        if any((keys[pygame.K_RIGHT], keys[pygame.K_d])):
            self.move_right()

        self.rect.x, self.rect.y = self.position[0], self.position[1]

    def move_right(self): self.position[0] += self.speed

    def move_left(self): self.position[0] -= self.speed

    def move_up(self): self.position[1] -= self.speed

    def move_down(self): self.position[1] += self.speed

Game Class :

class Game:
def __init__(self, title, state):
    self.MainMenu = MenusClass.MainMenu(800, 600)
    self.Window = WindowClass.Window((800, 600), title, [self.MainMenu,])
    self.Window.menu2alpha()

    self.state = state
    self.colors = StaticsClass.Color

    self.player = PlayerClass.Player(0, 0)
    self.mapmanager = MapClass.MapManager(self.Window.scr, self.player)


# Game Events
def _events(self):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if self.state == "__mainmenu__":
            self.MainMenu.is_activated = True

def update(self):
    self.Window._fillscreen(self.colors.BLUE0)
    self._events()

    curr_group = self.mapmanager.get_group()
    curr_group.center(self.player.position)
    curr_group.update()
    curr_group.draw(self.Window.scr)

    self.Window._blitmenus()

I think the problem is only in the player class’s construtor, and the update function of the Game Class… Hope someone can help me !

Asked By: Cold Fire

||

Answers:

The instruction

self.image = pygame.Surface([12, 19]).convert_alpha()

does not generate a transparent image. It generates a Surface with per pixel alpha format, that is completely opaque and black.

Use the SRCALPHA flag to generate a completely transparent Surface:

self.image = pygame.Surface([12, 19], pygame.SRCALPHA)

See also How to make a surface with a transparent background in pygame


Player:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super(Player, self).__init__()

        self.spritesheet = pygame.image.load('assets/imgs/player0.png')

        self.image = pygame.Surface([12, 19], pygame.SRCALPHA)
        self.image.blit(self.spritesheet.convert_alpha(), (0, 0))
Answered By: Rabbid76