Pygame 淡入黑功能 [英] Pygame fade to black function

查看:55
本文介绍了Pygame 淡入黑功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pygame 最新版本在 python 3 中编写游戏.我有一个功能可以让屏幕慢慢变暗,直到它完全变黑.它应该通过在屏幕上多次传输低 alpha 黑色表面来实现.

I'm coding a game in python 3 using pygame latest version. I have a function that is intended to slowly fade the screen until it is totally black. It should do so by blitting a low-alpha black surface many times on the screen.

但是当我测试它时,它只会阻塞游戏直到循环完成.我怀疑 black_surface 的 alpha 有问题.

But when I test it, it only blocks the game until the loop is finished. I suspect a problem with the alpha of black_surface.

我在论坛上看到了一些关于在 pygame 中淡入淡出的问题,但没有一个是直接在函数中淡入淡出.

I've seen some questions on forums about fade in pygame, but none of them concerned fade directly in functions.

代码如下:

def fade_to_black(screen):
    black_surface = Surface((screen.get_width(), screen.get_height()), flags= SRCALPHA)
    color = (255, 255, 255, 1)
    black_surface.fill(color)
    alpha_key = 1
    while alpha_key <= 255:
        screen.blit(black_surface, screen.get_rect())
        display.flip()
        alpha_key = alpha_key + 1
        time.wait(1)

我查看了文档和论坛,但找不到任何解决方案.希望这不是我会遗漏的明显问题...感谢您的帮助!

I have looked in documentation and forums but can't find any solution. Hope it isn't an obvious issue I would have missed... Thanks for the help!

推荐答案

您创建了一个名为 black_surface 的表面,但用白色填充它.用黑色填充它(例如 (0, 0, 0, 1))并且可能会起作用,但还有另一个问题:

You create a surface called black_surface, but you fill it with white. Fill it with black (eg. (0, 0, 0, 1)) and may work, but there's another problem:

当您在更改屏幕表面的循环内调用 display.flip() 时,如果您不让 pygame 处理事件(例如通过调用 pygame.event.get()),具体取决于您的操作系统.此外,当您的循环运行时,您无法手动处理事件,例如QUIT 事件.因此,当您的屏幕变黑时,您无法退出游戏.

When you call display.flip() inside a loop where you change the screen surface, the display may not actually update if you don't let pygame handle events (e.g. by calling pygame.event.get()), depending on your OS. Also, while your loop runs, you can't handle events manually, e.g. the QUIT event. So while your screen fades to black, you can't quit your game.

一般来说,你应该只有一个主循环,并且不要调用像pygame.time.sleep这样的阻塞函数,当然也有例外).

Generally, you should only have one main loop and not call blocking functions like pygame.time.sleep, but there are exceptions, of course).

这是一个基于 Sprite 的简单示例:

Here's simple Sprite-based example:

import pygame

class Fade(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.rect = pygame.display.get_surface().get_rect()
        self.image = pygame.Surface(self.rect.size, flags=pygame.SRCALPHA)
        self.alpha = 0
        self.direction = 1

    def update(self, events):
        self.image.fill((0, 0, 0, self.alpha))
        self.alpha += self.direction
        if self.alpha > 255 or self.alpha < 0:
            self.direction *= -1
            self.alpha += self.direction

def main():
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    sprites = pygame.sprite.Group(Fade())
    clock = pygame.time.Clock()

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return
        sprites.update(events)
        screen.fill((30, 30, 30))
        pygame.draw.rect(screen, pygame.Color('dodgerblue'), (100, 100, 100, 100))
        sprites.draw(screen)
        pygame.display.update()
        clock.tick(60)

if __name__ == '__main__':
    main()

这篇关于Pygame 淡入黑功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆