【问题标题】:How to draw sprite on a sprite so that both of them will be seen in pygame [duplicate]如何在精灵上绘制精灵,以便在pygame中看到它们[重复]
【发布时间】:2021-01-20 17:44:15
【问题描述】:

我有两个精灵,一个是坦克精灵,另一个是保护精灵。我想在坦克精灵上绘制保护精灵。精灵图片如下:

当我在坦克精灵上绘制保护精灵时,结果如下:

保护精灵的中心是空的(根据photoshop,我检查了多次)。我正在为保护精灵使用以下代码

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

我应该怎么做才能只绘制保护精灵的线条,以便在组合绘制的中间看到坦克?

【问题讨论】:

  • 为什么在使用 PNG 时要设置颜色键? PNG 具有每个像素的 alpha。

标签: python pygame pygame-surface


【解决方案1】:

PNGs 具有每个像素的 alpha。无需设置颜色键。只需blit 油箱上的保护装置。 convert() 将像素格式更改为每个像素没有 alpha 的格式并删除透明度信息。请改用convert_alpha()

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

【讨论】:

  • 你太棒了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多