简体   繁体   English

使用 Pygame 生成颜色淡入效果

[英]Generation of color fade-in effect using Pygame

When playing around with opacity in pygame, I noticed that when you neglect to fill the primary Surface created by pygame.display.set_mode() with a color.在 pygame 中使用不透明度时,我注意到当您忽略用颜色填充由pygame.display.set_mode()创建的主 Surface 时。 You can create a pretty neat color fade-in effect by blitting another Surface on top of it.您可以通过在其上添加另一个 Surface 来创建非常整洁的颜色淡入效果。 Oddly, it seems this affect is controlled by the value of alpha and the tick rate of pygame.time.Clock()奇怪的是,这种影响似乎是由 alpha 的值和pygame.time.Clock()的滴答率控制的

If the value of alpha is >= the tick rate, the effect is instant, and when the value of alpha is < the tick rate, the speed of the effect seems to lengthen as the difference between tick rate - alpha increases.如果 alpha 的值 >= 滴答率,则效果是即时的,而当 alpha 的值 < 滴答率时,效果的速度似乎会随着tick rate - alpha之间的差异的增加而延长。 I currently have no idea why this is the case, so I'm hoping someone more experienced with the library can provide insight into the cause of the effect.我目前不知道为什么会这样,所以我希望对图书馆有更多经验的人可以深入了解造成这种影响的原因。

import pygame 

def main():
    button_rect = pygame.Rect(0, 0, 200, 40)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                exit()

        # When the window is not filled with a color you get a cool
        # fade-in effect. 
        # window.fill((0, 0, 0))

        container = pygame.Surface((848, 480), pygame.SRCALPHA)
        container.fill((30, 30, 30))
        
        # Place button in center of screen.
        button_rect.center = window.get_rect().center

        mouse_pos =  pygame.mouse.get_pos()

        # The value of alpha affects the speed of the effect
        alpha = 10
        if button_rect.collidepoint(mouse_pos):
            button = pygame.draw.rect(container, 
                                      (255, 0, 0, alpha), 
                                      button_rect)
        else:
            button = pygame.draw.rect(container, 
                                      (255, 255, 255, alpha), 
                                      button_rect)

        window.blit(container, (0, 0))
        window.blit(text, text.get_rect(center=button.center))

        pygame.display.update(container.get_rect())

        # Clock speed affects fade-in time as well.
        clock.tick(60)

        
if __name__ == "__main__":
    initalized, unintialized = pygame.init()
    window = pygame.display.set_mode((848, 480), pygame.RESIZABLE)    
    clock = pygame.time.Clock()

    font = pygame.font.Font("../assets/fonts/terminal.ttf", 20)
    text = font.render("Hello", 0, (0,0,0))
    main()

You draw a transparent surface over the window surface in each frame.在每一帧的窗口表面上绘制一个透明表面。 This creates a fade in effect.这会产生淡入效果。 The drawing in the window becomes more and more solid the more often you put the transparent surface over it.将透明表面放在窗口上的次数越多,窗口中的图形就会变得越来越实体。 Increasing the tick rate and doing it more often will make the fade in faster.增加滴答率并更频繁地执行将使淡入更快。 Increasing the alpha value makes the Surface you're laying over the window less transparent, and the fad-in becomes faster as well.增加 alpha 值会使您放置在窗口上的 Surface 变得不那么透明,并且淡入也变得更快。
See also How to fade in a text or an image with PyGame .另请参阅如何使用 PyGame 淡入文本或图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM