Pygame BLEND_RGBA_ADD 和 screen.blit
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.blit
或 Surface.fill
时,addition 混合模式将用于 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))
我正在 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.blit
或 Surface.fill
时,addition 混合模式将用于 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))