python - 在pygame中堆叠不同类型的透明度

标签 python pygame

我有一个自定义 GUI 模块,它使用树中的对象来管理界面,并以正确的顺序将它们逐个传输。

现在在我的对象中,我有一些只是具有逐像素透明度的表面,而其他的则使用色键。

我的问题是,当在另一个填充有色键的表面上以逐像素透明度 blit 一个表面时,第一个表面改变了第二个中某些像素的颜色,这意味着它们不再透明。我怎样才能混合这些而不必摆脱每像素透明度?

最佳答案

您可以先将使用色键的 Surfaces 转换为使用每像素透明度,然后再使用 convert_alpha 在其上 blit 另一个每像素透明度 Surface .


示例:

COLORKEY=(127, 127, 0)
TRANSPARENCY=(0, 0, 0, 0)
import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
for x in xrange(0, 200, 20):
  pygame.draw.line(screen, (255, 255, 255), (x, 0),(x, 480))

# create red circle using a colorkey
red_circle = pygame.Surface((200, 200))
red_circle.fill(COLORKEY)
red_circle.set_colorkey(COLORKEY)
pygame.draw.circle(red_circle, (255, 0, 0), (100, 100), 25)

#create a green circle using alpha channel (per-pixel transparency)
green_circle = pygame.Surface((100, 100)).convert_alpha()
green_circle.fill(TRANSPARENCY)
pygame.draw.circle(green_circle, (0, 255, 0, 127), (50, 50), 25)

# convert colorkey surface to alpha channel surface before blitting
red_circle = red_circle.convert_alpha()
red_circle.blit(green_circle, (75, 75))

screen.blit(red_circle, (0, 0))

pygame.display.flip()

结果:

enter image description here

关于python - 在pygame中堆叠不同类型的透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11645926/

相关文章:

python - 放大 pygame 中的窗口导致滞后

Python:如何从屏幕的一侧反弹

python - pyttsx3 无法在新安装的 Windows 10 上运行

Python MySQL连接器-使用fetchone时发现未读结果

python - 如何在主Python文件中使用多个.ui文件

python - Jinja for 循环范围在递增变量时被重置

python - 在 Python 环境下使用 Pygame 和 OpenGL 绘制立方体

python - Pygame 类型错误 : update() takes 1 positional argument but 2 were given

Python:使用代码编写代码 - 初学者

python - 两个(双)轴上的线交织 zorder