I\'m working on some Python code that uses Pygame, trying to display a small sprite (a ball) on top of a background. I have that part working, but I\'m trying to get the bac
There's per-pixel alpha, colorkey alpha, and per-surface alpha. You're asking for colorkey.
When you call convert_alpha() it creates a new surface for per-pixel alpha.
And from set_colorkey
The colorkey will be ignored if the Surface is formatted to use per pixel alpha values.
So: Load image with .convert()
Since you want to use a color key. Then call set_colorkey
.
Also, I saw nothing in the docs about passing "-1" as first argument to set_colorkey.
That's probably from a tutorial, which has a load_image function to grab the topleft pixel's color value.
How is your image created? Pygame transparecy for blitting should work if your "ball.png" file i s a ball on transparent background, and not a ball on a black square. That said, from Pygame's surface documentation for "set_colorkey":
"The colorkey will be ignored if the Surface is formatted to use per pixel alpha values. The colorkey can be mixed with the full Surface alpha value."
So, the idea of colorkey is to use it when yiour image does not have per pixel alpha - and you are just ensuring it does have when you call "convert alpha". Also, I saw nothing in the docs about passing "-1" as first argument to set_colorkey.
In short: My advice is to use proper transparent images to start with - and forget about "convert", "convert_alpha", "set_colorkey" and so on. If you have some reason for not using per pixel alpha from the PNG file, then check this answer a the right way to do it (and even for one reason not to want per pixel alpha when blitting to the screen): PyGame: Applying transparency to an image with alpha?