为什么 alpha 混合不能正常工作?

我正在尝试制作一个简单的 Pygame 应用程序,其中一些颜色与它们下面的颜色混合。这是我的代码:


代码清单1:


import pygame, sys, time

from pygame.locals import *


#define constants

WINDOW_WIDTH = 600

WINDOW_HEIGHT = 600

FPS = 60

    

pygame.init()

clock = pygame.time.Clock()

screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)

    

alpha = 0

increasing = True

    

while True:

    for event in pygame.event.get():

        if event.type == QUIT:

            pygame.quit()

            sys.exit()


    #fill screen with red

    screen.fill(pygame.Color(247, 25, 0,255))

    alpha_surface = pygame.Surface((screen.get_rect().width, screen.get_rect().height))

    alpha_surface = alpha_surface.convert_alpha()

    surfaceRect = alpha_surface.get_rect()


    #fill surface with orange

    pygame.draw.rect(alpha_surface, (247, 137, 0, 255), (0, 0, 480, 480))

    #fill surface with translucent yellow

    pygame.draw.rect(alpha_surface, (220, 247, 0, alpha), (120, 120, 360, 360))

    #fill surface with green

    pygame.draw.rect(alpha_surface, (0, 247, 4), (240, 240, 240, 240))

    #fill surface with translucent blue

    pygame.draw.rect(alpha_surface, (0, 78, 247, alpha), (360, 360, 120, 120))

    screen.blit(alpha_surface, (120,120))

    

    pygame.display.update()

    clock.tick(FPS)


    if increasing:

        alpha += 1

        if alpha > 255:

            alpha = 255

            increasing = False

    else:

        alpha -= 1

        if alpha < 0:

            alpha = 0

            increasing = True

代码应该使黄色矩形与橙色矩形和蓝色矩形与绿色矩形混合。相反,我得到了一些东西:


图 1:黄色和蓝色不透明度设置为 0% 的原始状态

http://img.mukewang.com/616e6c5c00013fc306010601.jpg

对此:

图 2:黄色和蓝色不透明度设置为 100% 的最终状态

http://img.mukewang.com/616e6c680001de5106030598.jpg

如您所见,黄色和蓝色矩形不仅与红色矩形(屏幕表面)混合,而且还在橙色和绿色矩形上打了一个洞,以便我们可以通过它们看到红色矩形。


慕运维8079593
浏览 172回答 1
1回答

慕田峪9158850

如果要混合不同的图层,则必须创建不同的pygame.Surfaces 或图像。可以通过加载图像 (&nbsp;pygame.image) 或构造pygame.Surface对象来生成 Surface&nbsp;。使用每像素 alpha创建一个完全透明的表面。使用pygame.Surface.convert_alpha改变的图像,包括每像素阿尔法的像素格式。用透明颜色填充Surface(例如pygame.Color(0, 0, 0, 0)):最小的例子:import pygamepygame.init()clock = pygame.time.Clock()screen = pygame.display.set_mode((600, 600), 0, 32)def transparentSurface(size):&nbsp; &nbsp; surface = pygame.Surface(size).convert_alpha()&nbsp; &nbsp; surface.fill((0, 0, 0, 0))&nbsp; &nbsp; return surfacealpha, increase = 0, 1run = Truewhile run:&nbsp; &nbsp; clock.tick(60)&nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run = False&nbsp; &nbsp; screen.fill(pygame.Color(247, 25, 0,255))&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; alpha_surface1 = transparentSurface(screen.get_size())&nbsp; &nbsp; pygame.draw.rect(alpha_surface1, (247, 137, 0, 255), (120, 120, 480, 480))&nbsp; &nbsp; alpha_surface2 =&nbsp; transparentSurface(screen.get_size())&nbsp; &nbsp; pygame.draw.rect(alpha_surface2, (220, 247, 0, alpha), (240, 240, 360, 360))&nbsp; &nbsp; alpha_surface3 =&nbsp; transparentSurface(screen.get_size())&nbsp; &nbsp; pygame.draw.rect(alpha_surface3, (0, 247, 4), (360, 360, 240, 240) )&nbsp; &nbsp; alpha_surface4 =&nbsp; transparentSurface(screen.get_size())&nbsp; &nbsp; pygame.draw.rect(alpha_surface4, (0, 78, 247, alpha), (480, 480, 120, 120) )&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; screen.blit(alpha_surface1, (0,0))&nbsp; &nbsp; screen.blit(alpha_surface2, (0,0))&nbsp; &nbsp; screen.blit(alpha_surface3, (0,0))&nbsp; &nbsp; screen.blit(alpha_surface4, (0,0))&nbsp; &nbsp; pygame.display.update()&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; alpha += increase&nbsp; &nbsp; if alpha < 0 or alpha > 255:&nbsp; &nbsp; &nbsp; &nbsp; increase *= -1&nbsp; &nbsp; &nbsp; &nbsp; alpha = max(0, min(255, alpha))pygame.quit()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python