2012-07-25 72 views
2

我有一个自定义的GUI模块,它使用树中的对象来管理接口,并将它们以正确的顺序粘贴到另一个上面。在pygame中堆叠不同类型的透明度

现在,在我的对象中,我有一些只是具有每像素透明度的曲面,其他使用了一个colorkey。

我的问题是,当一个表面上的每像素透明度与另一个用colorkey填充时,第一个表面会更改第二个表面中某些像素的颜色,这意味着它们不再透明。我怎么能混合那些,而不必摆脱每像素的透明度?

回答

3

您可以将使用colorkey的Surfaces转换为使用每像素透明度,然后使用convert_alpha对另一个像素透明度Surface进行blitting。


实施例:

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