在Pygame中撤消颜色混合 [英] Undo colour Blending in Pygame

查看:95
本文介绍了在Pygame中撤消颜色混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用pygame的游戏中,我正在尝试添加Hitflash。对于不熟悉这种方法的人来说,击闪是一种效果,敌人会闪烁一种颜色(通常是白色)约半秒钟(不要在上面引用我的意思,这是我的描述方式)。这可以通过 Surface.fill()函数通过传入一个附加参数来完成。为了实现Hitflash效果,我用白色填充图像并将其混合。但是,我不知道如何将图像还原为与白色混合之前的图像。我可以轻松地创建原始图像的副本并加载未混合的原始图像,但是我发现这对我处理的图像效率太低。有没有一种方法/功能允许混合撤消(即,将混合图像恢复为正常)?

In a game that I'm working on using pygame, I'm trying add hitflash. To those who are unfamiliar with what this is, hitflash is an effect where an enemy flashes a colour - usually white - for about half a second (Don't quote me on that, that's my way of describing it). This can be done with the Surface.fill() function by passing in an additional argument. In order to achieve the hitflash effect, I fill the image with white and blend it. However, I am unaware of how I can revert the image to how it was before it was blended with white. I can easily create duplicates of the original images and load the original ones that weren't blended, but I find that this is too inefficient with what I'm working with. Is there a way/function that allows for blending to be undone (i.e, change blended image back to normal)?

推荐答案

填充填充表面会对其进行修改,因此,当对象受到损坏时,我建议将图像换成更亮的版本。在while循环之前创建明亮版本,或加载图像的另一个版本,然后通过将当前图像分配给另一个变量来交换它。

filling a surface will modify it, so I recommend swapping the image for a brighter version when the object takes damage. Create the bright version before the while loop or load another version of the image and then swap it by assigning the current image to another variable.

您可能还需要一个计时器来控制将图像重置为原始版本的速度。

You probably also need a timer to control how fast the image will be reset to the original version.

我建议您不要在while循环中那样在sprite类中执行此操作,如下面的最小示例所示。

I suggest that you do that in your sprite classes not in the while loop as in the minimal example below.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
image_normal = pg.Surface((30, 50))
image_normal.fill(pg.Color('dodgerblue'))
image_bright = image_normal.copy()
image_bright.fill((100, 100, 100, 0), special_flags=pg.BLEND_RGBA_ADD)
image = image_normal  # The currently selected image.
timer = 0
dt = 0

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN:
            image = image_bright  # Swap the image.
            timer = .5  # 0.5 seconds.

    timer -= dt
    if timer <= 0:
        image = image_normal  # Set the image back to the normal version.
        timer = 0

    screen.fill(BG_COLOR)
    screen.blit(image, (300, 200))
    pg.display.flip()
    dt = clock.tick(60) / 1000

pg.quit()

这篇关于在Pygame中撤消颜色混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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