第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

慕運(yùn)維8079593 2021-10-19 14:56:05
我正在嘗試制作一個簡單的 Pygame 應(yīng)用程序,其中一些顏色與它們下面的顏色混合。這是我的代碼:代碼清單1:import pygame, sys, timefrom pygame.locals import *#define constantsWINDOW_WIDTH = 600WINDOW_HEIGHT = 600FPS = 60    pygame.init()clock = pygame.time.Clock()screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)    alpha = 0increasing = 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代碼應(yīng)該使黃色矩形與橙色矩形和藍(lán)色矩形與綠色矩形混合。相反,我得到了一些東西:圖 1:黃色和藍(lán)色不透明度設(shè)置為 0% 的原始狀態(tài)對此:圖 2:黃色和藍(lán)色不透明度設(shè)置為 100% 的最終狀態(tài)如您所見,黃色和藍(lán)色矩形不僅與紅色矩形(屏幕表面)混合,而且還在橙色和綠色矩形上打了一個洞,以便我們可以通過它們看到紅色矩形。
查看完整描述

1 回答

?
慕田峪9158850

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊

如果要混合不同的圖層,則必須創(chuàng)建不同的pygame.Surfaces 或圖像??梢酝ㄟ^加載圖像 ( pygame.image) 或構(gòu)造pygame.Surface對象來生成 Surface 。

使用每像素 alpha創(chuàng)建一個完全透明的表面。使用pygame.Surface.convert_alpha改變的圖像,包括每像素阿爾法的像素格式。用透明顏色填充Surface(例如pygame.Color(0, 0, 0, 0)):


最小的例子:

http://img1.sycdn.imooc.com//616e6c7e00018b7e03010301.jpg

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()


查看完整回答
反對 回復(fù) 2021-10-19
  • 1 回答
  • 0 關(guān)注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號