Yarn Saw
Yarn Saw

Reputation: 77

Pygame BLEND_RGBA_ADD with screen.blit

I am creating a game in python, and want to make the enemies flash white when they are damaged in any way. To do this I am trying to blit the enemies to the screen with the special flag of blending. However I do not know how to specify what color and alpha value the image should be blended with. When I try to add values for RGBA after the BLEND_RGBA_ADD it creates an error 'TypeError: 'int' object is not callable', which I do not understand because the color has to be an int.

import pygame as py

py.init()
clock = py.time.Clock()

screen = py.display.set_mode((400,700))

image =py.image.load('image.png')
alpha = 0

while True:
    clock.tick(60)
    blending = (255,255,255,alpha)
    for event in py.event.get():
        if event.type == py.QUIT:
            py.quit()
            exit()
        if event.type == py.KEYDOWN and event.key == py.K_a:
            if alpha < 245:
                alpha +=10
        if event.type == py.KEYDOWN and event.key == py.K_d:
            if alpha >10:
                alpha -=10

    screen.fill((0,0,0))
    screen.blit(image,(100,100),area = None,special_flags=py.BLEND_RGBA_ADD(blending))
    py.display.flip()

Upvotes: 2

Views: 3597

Answers (1)

skrx
skrx

Reputation: 20438

pygame.BLEND_RGBA_ADD is a constant integer so you can't pass arguments to it. When you pass it as the special_flags argument to Surface.blit or Surface.fill, the addition blend mode will be used for the blitting/filling. If you want to make your image brighter you can fill it with a non-black color and use 0 as the alpha value, so that the alpha channel is unaffected. (Press A to increase the brightness in this example.)

import pygame as py


py.init()
clock = py.time.Clock()
screen = py.display.set_mode((400, 700))

image = py.image.load('image.png').convert_alpha()

while True:
    clock.tick(60)

    for event in py.event.get():
        if event.type == py.QUIT:
            py.quit()
            exit()
        if event.type == py.KEYDOWN and event.key == py.K_a:
            image.fill((100, 100, 100, 0), special_flags=py.BLEND_RGBA_ADD)

    screen.fill((0,0,0))
    screen.blit(image, (100,100))
    py.display.flip()

Note that filling a surface will change the original surface. I think what you actually want to do is to swap 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. (By the way, call convert() or convert_alpha() to improve the blit performance drastically).

image = py.image.load('image.png').convert_alpha()
bright_image = image.copy()
bright_image.fill((100, 100, 100, 0), special_flags=py.BLEND_RGBA_ADD)
current_image = image  # Assign the current image to another variable.

# In the main `while` loop.
# Swap the image when something happens.
current_image = bright_image
screen.blit(current_image, (100,100))

Upvotes: 2

Related Questions