3

I'm currently making a game, in which I load the sprites and backgrounds from images (PNG) using the pygame.image.load() function.

My problem is that not all of my sprites are rectangular - and because of that they leave ugly white-space on the background that I blitted them on to. On the image below, the white is part of the ship sprite, however I would want it to blend in with the background.

enter image description here

Is there an easy way to solve this? Thanks in advance for any help.

Vladimir Shevyakov
  • 2,511
  • 4
  • 19
  • 40
  • Are you sure the ship image itself is transparent? I've managed to load transparent .pngs just fine. Also, what software did you use to make that image? – ASCIIThenANSI Jan 25 '16 at 20:54

1 Answers1

3

If image has transparent background (some pixels are transparent) (PNG) then you have to use convert_alpha()

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

If image has no transparent background (GIF, JPG) then you can use set_colorkey() and pygame will treat selected color as transparent.

 image = pygame.image.load(filename).set_colorkey(color)

Many sprites images use pink as key color

enter image description here

furas
  • 134,197
  • 12
  • 106
  • 148
  • I tried using the convert_alpha(), but it didn't make any difference. Could it be because I made it on mspaint? – Vladimir Shevyakov Jan 25 '16 at 21:36
  • You have to use PNG with transparent pixels. If you see white background using image viewer then you don't have transparent background and `convert_alpha()` will not work. And then you can only use `set_colorkey(WHITE)` (where `WHITE = (255,255,255)`) – furas Jan 25 '16 at 21:39
  • Ok, thank you... but how can I make an image with transparent pixels right in mspaint or some other software? – Vladimir Shevyakov Jan 25 '16 at 22:08
  • I don't have `mspaint` but I think you can't get transparent background in `mspaint`. You need [GIMP](https://www.gimp.org/), [paint.net](http://www.getpaint.net/index.html) or any other bitmap program (or [Inkscape](https://inkscape.org/en/) if you prefer to use vector object) – furas Jan 25 '16 at 23:26