Search code examples
pythonpygamepygame-surface

How to draw sprite on a sprite so that both of them will be seen in pygame


I have two sprites one of them is a tank sprite the other is a protection sprite. I want to draw the protection sprite on the tank sprite. The images of the sprites are as follows:

enter image description here

enter image description here

When I draw the protection sprite on the tank sprite, the result is the following: enter image description here

Center of the protection sprite is empty (according to photoshop, I checked multiple times). I am using the following code for the protection sprite

self.image = pygame.transform.scale(
        pygame.image.load(os.path.join(imgFolderExp, "Powerups", "protection-3.png")).convert(), (40, 48))
self.image.set_colorkey((0, 0, 0))

What should I do in order to draw only the lines of the protection sprite so that in the middle of the combined draw, the tank will be seen?


Solution

  • PNGs have per pixel alpha. It is not necessary to set a color key. Simply blit the protector on the tank. convert() changes the pixel format to a format without alpha per pixel and removes the transparency information. Use convert_alpha() instead:

    filepath = os.path.join(imgFolderExp, "Powerups", "protection-3.png") 
    
    protector_surf = pygame.image.load(filepath).convert_alpha()
    
    self.image = pygame.transform.scale(protector_surf, (40, 48))