0

I have a function that renders sprites:

self.sheet = pygame.image.load(file).convert_alpha()

But the sprites with transparency still render with black backgrounds. I can't use colorkey because for some reason my sprites always export with random discrepancies in colour where the colour is a few RGB units off the original colour, causing dots to randomly appear around the sprite.

And yes, the sprites I'm using are .png files, not jpg or jpeg.

So my question is, why isn't convert_alpha working, what can I do to make it work, or what else can I do to reach the same goal?

EDIT: here's the spritesheet:

rover spritesheet

Rabbid76
  • 177,135
  • 25
  • 101
  • 146
kecenr
  • 31
  • 1
  • 8
  • The image needs to have a transparent background, but not a black background. `convert_alpha()` doesn't magically generate a trnaparent background. You have to "photoshop" an image with a transparent background. See [Pygame image transparency confusion](https://stackoverflow.com/questions/64704789/pygame-image-transparency-confusion/64704923#64704923) – Rabbid76 Jun 22 '21 at 19:31
  • @Rabbid76 the background is transparent and not black, but it displays as black anyway despite usage of convert_alpha() – kecenr Jun 22 '21 at 19:32
  • I linked the image in an edit – kecenr Jun 22 '21 at 19:35
  • The background is transparent. This means there is a bug in your code. Show your code. The problem is not caused by the line `self.sheet = pygame.image.load(file).convert_alpha()` . – Rabbid76 Jun 22 '21 at 19:38
  • class spritesheet: def __init__(self, file): self.sheet = pygame.image.load(file).convert_alpha() def get_sprite(self, x, y, width, height): sprite = pygame.Surface([width, height]) sprite.blit(self.sheet, (0, 0), (x, y, width, height)) return sprite – kecenr Jun 22 '21 at 19:39
  • also self.image = self.game.roverspritesheet.get_sprite(0, 0, self.width, self.height) – kecenr Jun 22 '21 at 19:40
  • You have to generate an _Surface_ with an per pixel alpha format. Change `sprite = pygame.Surface([width, height])` to `sprite = pygame.Surface([width, height], pygame.SRCALPHA)`. Read about [`pygame.SRCALPHA`](https://www.pygame.org/docs/ref/surface.html). – Rabbid76 Jun 22 '21 at 19:40
  • 1
    Thank you kind fellow, this has solved the issue – kecenr Jun 22 '21 at 19:42

0 Answers0