如何在精灵上绘制精灵,以便在 pygame 中都能看到它们 [英] 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:
当我在坦克精灵上绘制保护精灵时,结果如下:
When I draw the protection sprite on the tank sprite, the result is the following:
保护精灵的中心是空的(根据photoshop,我检查了多次).我使用以下代码作为保护精灵
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?
推荐答案
PNGs像素阿尔法.没有必要设置颜色键.只需blit
坦克上的保护器.convert()
将像素格式更改为没有每像素 alpha 的格式并删除透明度信息.使用 convert_alpha()
代替:
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))
这篇关于如何在精灵上绘制精灵,以便在 pygame 中都能看到它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!