在pygame中堆叠不同类型的透明度 [英] Stacking different types of transparency in pygame
问题描述
我有一个自定义 GUI 模块,它使用树中的对象来管理界面,并以正确的顺序将它们相互重叠.
I have a custom GUI module which uses objects in a tree to manage the interface, and blits them in the right order above one another.
现在在我的对象中,我有一些只是具有每像素透明度的表面,而其他则使用色键.
Now amongst my objects, I have some which are just surfaces with per-pixel transparency, and the others use a colorkey.
我的问题是,当在另一个填充有颜色键的表面上逐像素透明地进行 blitting 时,第一个表面会更改第二个表面中某些像素的颜色,这意味着它们不再透明.我怎样才能在不必摆脱每像素透明度的情况下混合这些?
My problem is that when blitting a surface with per-pixel transparency on another filled with a colorkey, the first surface changes the color of some of the pixels in the second, which means they are no longer transparent. How could I mix those without having to get rid of the per-pixel transparency ?
推荐答案
您可以将使用颜色键的 Surfaces
转换为使用每像素透明度,然后再将另一个每像素透明度 Surface
使用 convert_alpha
.
You could just convert your Surfaces
that use a colorkey to use per-pixel transparency before blitting another per-pixel transparency Surface
on it using convert_alpha
.
示例:
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()
<小时>
结果:
这篇关于在pygame中堆叠不同类型的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!