1 回答
TA贡献1794条经验 获得超7个赞
如果要混合不同的图层,则必须创建不同的pygame.Surface
s 或图像。可以通过加载图像 ( pygame.image
) 或构造pygame.Surface
对象来生成 Surface 。
使用每像素 alpha创建一个完全透明的表面。使用pygame.Surface.convert_alpha
改变的图像,包括每像素阿尔法的像素格式。用透明颜色填充Surface(例如pygame.Color(0, 0, 0, 0)
):
最小的例子:
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((600, 600), 0, 32)
def transparentSurface(size):
surface = pygame.Surface(size).convert_alpha()
surface.fill((0, 0, 0, 0))
return surface
alpha, increase = 0, 1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill(pygame.Color(247, 25, 0,255))
alpha_surface1 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface1, (247, 137, 0, 255), (120, 120, 480, 480))
alpha_surface2 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface2, (220, 247, 0, alpha), (240, 240, 360, 360))
alpha_surface3 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface3, (0, 247, 4), (360, 360, 240, 240) )
alpha_surface4 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface4, (0, 78, 247, alpha), (480, 480, 120, 120) )
screen.blit(alpha_surface1, (0,0))
screen.blit(alpha_surface2, (0,0))
screen.blit(alpha_surface3, (0,0))
screen.blit(alpha_surface4, (0,0))
pygame.display.update()
alpha += increase
if alpha < 0 or alpha > 255:
increase *= -1
alpha = max(0, min(255, alpha))
pygame.quit()
添加回答
举报