Febvre Antoine
Febvre Antoine

Reputation: 77

Using image transaprency make appear black "things"

I'm having troubles to correctly load and blit PNG image with Pygame. Code is actually working, but it display some strange and black "things" around my sprite :

black things around my sprite

To load tileset, I do:

    def TilesRessourceFile(self, filename=None, tileSize=None, tiles=None):
        logging.info("Loading ressources from %s", filename)
        self._tileSize = tileSize

        res = pygame.image.load(filename)
        res.convert_alpha()

        for tile in tiles:
            name, (x, y), alpha = tile.values()
            surface = pygame.Surface((tileSize, tileSize))
            surface.blit(res, (0, 0), area=(x * tileSize, y * tileSize, (x + 1) * tileSize, (y + 1) * tileSize))
            surface.convert_alpha() # set_colorkey(alpha)
            self._tiles.update({name: surface})

Then I blit the sprite like this

    def _implGameObjectRender(self, screen):
    # logging.info("Render map %s", self._mapping)
    for i in range(len(self._mapping)):
        for j in range(len(self._mapping[i])):
            screen.blit(self._mapping[i][j], (j * 128, i * 128))

It's probably not much, but I don't find the solution by myself. I already tried to check :

I'm using this tileset : https://www.gamedevmarket.net/asset/2d-hand-painted-town-tileset-6626/

The tileset provide a json file to load with Tiled. Also tried this, and it perfectly works so I guess the problem is on my side

Thanks for helping me !

Python 3.9.1 Pygame 2.0.1 (SDL 2.0.14)

Upvotes: 1

Views: 261

Answers (1)

Rabbid76
Rabbid76

Reputation: 210946

convert_alpha() does not convert the format of the surface itself, but creates a new surface with a format that provides a per pixel alpha format.

either

surface = pygame.Surface((tileSize, tileSize))
surface = surface.convert_alpha() 

or

surface = pygame.Surface((tileSize, tileSize)).convert_alpha() 

There are 3 ways to create a transparent Surface:

  • Set a transparent color key with set_colorkey()

    The color key specifies the color that is treated as transparent. For example, if you have an image with a black background that should be transparent, set a black color key:

    surface.set_colorkey((0, 0, 0))
    
  • You can enable additional functions when creating a new surface. 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):

    surface = pygame.Surface((tileSize, tileSize), pygame.SRCALPHA)
    
  • Use convert_alpha() to create a copy of the Surface with an image format that provides alpha per pixel.

    However, 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:

    surface = pygame.Surface((tileSize, tileSize))
    surface = surface.convert_alpha()
    surface.fill((0, 0, 0, 0))
    

Upvotes: 1

Related Questions