1

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 :

  • how to load PNG with pygame
  • transparency with pygame (convert and convert_alpha)

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)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

1

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))
    
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Tried what you advised like this `surface = pygame.Surface((tileSize, tileSize)).convert_alpha()`. It ended up giving the same result. Edit : I uncommented `surface.set_colorkey(alpha)` else I get sprite on a black background – Febvre Antoine Feb 09 '21 at 19:28
  • @FebvreAntoine So you haven't read the full answer? What about `surface.fill((0, 0, 0, 0))`? Or simply `surface = pygame.Surface((tileSize, tileSize), pygame.SRCALPHA)`? – Rabbid76 Feb 09 '21 at 19:30
  • Sry, didn't see you updated answer, will check this – Febvre Antoine Feb 09 '21 at 19:32
  • I've vote up your answer, told me that I needed more than 15 reputation points for it to be display ... Edit : it exactly say Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. Edit 2 : how do you mark this post as resolvde ? – Febvre Antoine Feb 10 '21 at 20:48
  • @FebvreAntoine There is no "resolved" state. However, you have accepted an answer to your question. This is the equivalent of "resolved". – Rabbid76 Feb 10 '21 at 20:59