Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blitting non-rectangular sprites with Pygame, Python 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.

like image 291
Vladimir Shevyakov Avatar asked Apr 10 '26 10:04

Vladimir Shevyakov


1 Answers

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

like image 161
furas Avatar answered Apr 12 '26 00:04

furas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!