Undo colour Blending in Pygame

余生长醉 提交于 2019-12-24 10:27:55

问题


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


回答1:


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.

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


来源:https://stackoverflow.com/questions/53789031/undo-colour-blending-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!