Search code examples
pythonpygamepygame-surface

Transparency in pygame sprites


I can't figure out why the code below renders the sprite without transparency (with the rest of the sprite filled with black). I'm almost a beginner to programming so it might be very obvious but no other answers to similar questions here helped me. The file has transparency and I used convert_alpha() when loading the file.

import pygame


class Piece(pygame.sprite.Sprite):
    def __init__(self, kind, posx, posy, sheet, color):
        # Call the parent class (Sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        # Create an image for the piece and fill it with the image
        # TO DO: cut the right type of piece from a sheet, preferably before constructing the object
        self.image = pygame.Surface([128, 128])
        self.image.blit(sheet, [0, 0])

        self.kind = kind  # piece type, designated with an uppercase letter, eg. Q for queen
        self.color = color  # W or B for white or black

        # Fetch the rectangle object that has the dimensions of the image
        # Update the position of this object by setting the values of rect.x and rect.y
        self.rect = self.image.get_rect()
        self.rect.x = posx
        self.rect.y = posy


class App:
    def __init__(self):
        self._running = True
        self._display_surf = None
        self.size = self.weight, self.height = 1024, 768
        self.sprites = pygame.sprite.Group()  # all sprites in the app to iterate on
        self.spritesheet = 0  # this will be loaded later
        self.bgcolor = [200, 200, 200]

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._running = True
        self.spritesheet = pygame.image.load("resources/w_queen_png_shadow_128px.png", "r").convert_alpha()
        self.sprites.add(Piece("Q", 64, 64, self.spritesheet, "W"))

    def on_event(self, event):
        if event.type == pygame.QUIT:
            self._running = False

    def on_loop(self):
        pass

    def on_render(self):
        self._display_surf.fill(self.bgcolor)
        self.sprites.draw(self._display_surf)
        pygame.display.flip()

    def on_cleanup(self):
        pygame.quit()

    def on_execute(self):
        if self.on_init() is False:
            self._running = False

        while self._running:
            for event in pygame.event.get():
                self.on_event(event)
            self.on_loop()
            self.on_render()
        self.on_cleanup()


if __name__ == "__main__":
    game = App()
    game.on_execute()

Solution

  • If you are copying an image with a per pixel alpha format on another pygame.Surface onto another surface, you need to ensure that the target Surface has a per pixel alpha format, too. If the target cannot save the alpha channel, the transparency will be lost. Set the SRCALPHA flag to create a surface with an image format that includes a per-pixel alpha.

    self.image = pygame.Surface([128, 128])

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

    class Piece:

    class Piece(pygame.sprite.Sprite):
        def __init__(self, kind, posx, posy, sheet, color):
            # Call the parent class (Sprite) constructor
            pygame.sprite.Sprite.__init__(self)
    
            # Create an image for the piece and fill it with the image
            # TO DO: cut the right type of piece from a sheet, preferably before constructing the object
            self.image = pygame.Surface([128, 128], pygame.SRCALPHA)
            self.image.blit(sheet, [0, 0])
    
            self.kind = kind  # piece type, designated with an uppercase letter, eg. Q for queen
            self.color = color  # W or B for white or black
    
            # Fetch the rectangle object that has the dimensions of the image
            # Update the position of this object by setting the values of rect.x and rect.y
            self.rect = self.image.get_rect()
            self.rect.x = posx
            self.rect.y = posy