Pygame BLEND_RGBA_ADD with screen.blit



我正在python中创建一个游戏,并希望在敌人受到任何伤害时使其闪烁白色。为了做到这一点,我试图用混合的特殊旗帜将敌人闪电般地打到屏幕上。然而,我不知道如何指定图像应该与什么颜色和alpha值混合。当我试图在BLEND_RGBA_add之后添加RGBA的值时,它会创建一个错误"TypeError:"int"object is not callable",我不理解,因为颜色必须是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()

pygame.BLEND_RGBA_ADD是一个常量整数,因此不能将参数传递给它。当将其作为special_flags参数传递给Surface.blitSurface.fill时,将使用加法混合模式进行blitting/filling。如果要使图像更亮,可以使用非黑色填充图像,并使用0作为alpha值,这样alpha通道不受影响。(在本例中,按A可增加亮度。(

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

请注意,fill修改曲面将更改原始曲面。我认为你真正想做的是在物体受损时将图像换成更亮的版本。

在while循环之前创建明亮的版本,或者加载图像的另一个版本,然后通过将当前图像分配给另一个变量进行交换。(顺便说一下,调用convert()convert_alpha()可以大幅提高blit性能(。

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

最新更新