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

落花浮王杯 提交于 2021-02-10 14:23:20

问题


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:

When I draw the protection sprite on the tank sprite, the result is the following:

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?


回答1:


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))


来源:https://stackoverflow.com/questions/65751155/how-to-draw-sprite-on-a-sprite-so-that-both-of-them-will-be-seen-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!