【问题标题】:How to fade IN text/screen using pygame alpha [duplicate]如何使用pygame alpha淡入文本/屏幕[重复]
【发布时间】:2021-03-17 17:16:50
【问题描述】:

我可以让我的文本在“场景”结束时淡出,但我不知道如何让文本或屏幕淡入。我看过许多不同的视频和线程,但它们要么处理图像,这与我尝试做的不同,说如何淡出(我已经知道如何淡出,我不知道淡入),或者它们已过时。我也尝试过颠倒 alpha 值和计数器,但从我测试的结果来看,它并没有改变它,而且主要是给我错误。

我也很想看看是否有人有更简单的方法来做到这一点,例如。有什么我可以从 pygame 调用的可以为我褪色的东西...例如: pygame.fadeout(screen, 2)

import pygame
from pygame.locals import *
pygame.init()

import pygame as pg


def title():
    clock = pg.time.Clock()
    screen = pygame.display.set_mode((1395, 855), pygame.FULLSCREEN)
    font = pygame.font.Font("/Users/ACCOUNTNAME/Library/Fonts/ARIALN.TTF", 70)
    
    color = pg.Color('#FFFFFF')
    orig_surf = font.render("Filler: ", True, color)
    color2 = pg.Color('#FFFFFF')
    orig2_surf = font.render("Very Cool", True, color2)
    
    txt_surf = orig_surf.copy()
    txt2_surf = orig2_surf.copy()
    alpha_surf = pg.Surface(txt_surf.get_size(), pg.SRCALPHA)
    alpha2_surf = pg.Surface(txt2_surf.get_size(), pg.SRCALPHA)
    
    alpha = 255
    counter = 0

    while True:
        for e in pygame.event.get():
            if e.type == QUIT or e.type == KEYDOWN and e.key == pygame.K_ESCAPE:
                pygame.mixer.music.fadeout(1000)
                return

        if alpha > 0 and counter <= 80:
            alpha = max(alpha-0, 0)
            txt_surf = orig_surf.copy()
            alpha_surf.fill((255, 255, 255, alpha))
            txt_surf.blit(alpha_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
            
            txt2_surf = orig2_surf.copy()
            alpha2_surf.fill((255, 255, 255, alpha))
            txt2_surf.blit(alpha2_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)
            
            counter += 1
            
        elif alpha > 0 and counter > 80:
            alpha = max(alpha-1.2, 0)
            txt_surf = orig_surf.copy()
            alpha_surf.fill((255, 255, 255, alpha))
            txt_surf.blit(alpha_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)

            txt2_surf = orig2_surf.copy()
            alpha2_surf.fill((255, 255, 255, alpha))
            txt2_surf.blit(alpha2_surf, (0, 0), special_flags=pg.BLEND_RGBA_MULT)



        #screen.fill((9, 47, 68, alpha))
        screen.fill((0, 66, 99, alpha))
        
        screen.blit(txt_surf, (302, 348))
        screen.blit(txt2_surf, (509, 418))

        pg.display.flip()
        clock.tick(60)


if __name__ == '__main__':
    pg.init()
    title()
    pg.quit()

【问题讨论】:

  • 这段代码有很多卷积,但很明显,你要做的就是让 alpha 从 0 增加到 255,而不是像上面的代码在计数器超过 80 后开始做的那样减少跨度>

标签: python pygame fadein fade


【解决方案1】:

你需要做的就是用set_alpha设置全文Surface的alpha值:

alpha = 0
counter = 0

while True:
    # [...]

    if counter <= 80:
        alpha = min(alpha + 255/80, 255)            
    elif alpha > 0 and counter > 80:
        alpha = max(alpha - 255/80, 0)
    counter += 1
        
    txt_surf = orig_surf.copy()
    txt_surf.set_alpha(alpha)    
    txt2_surf = orig2_surf.copy()
    txt2_surf.set_alpha(alpha)

如果您想淡出整个显示,请创建一个 Surface 窗口大小和您选择的颜色:

fadeScreen = pygame.Surface(screen.get_size())
fadeScreen.fill((0, 0, 0))

在显示更新之前混合屏幕顶部的Surface

while True:
    # [...]

    fadeScreen.set_alpha(255-alpha)
    screen.blit(fadeScreen, (0, 0))
    pg.display.flip()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-22
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多