1 回答

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊
如果要混合不同的圖層,則必須創(chuàng)建不同的pygame.Surface
s 或圖像??梢酝ㄟ^加載圖像 ( pygame.image
) 或構(gòu)造pygame.Surface
對象來生成 Surface 。
使用每像素 alpha創(chuàng)建一個完全透明的表面。使用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()
添加回答
舉報