问题描述
我正在用PyGame创建游戏。我要在其中创建负面效果,使所有屏幕变为负面效果。
这对于简单的矩形(例如rects)非常简单:(并使用rect
代替pygame.draw.rect
)
# set negative to 1 to activate effect
negative = 0
def rect(win,color,area,width=0):
if negative:
pygame.draw.rect(win,(255 - color[0],255 - color[1],255 - color[2]),width)
else:
pygame.draw.rect(win,width)
但是我不知道如何在图像上产生这种效果。我曾尝试进行负片复制,但是如果这种效果不会立即发生并且会逐渐填满整个屏幕,那么这将无济于事。
# negative image copy
def negcopy(image):
neg = pygame.Surface(image.get_size())
for y in range(image.get_height()):
for x in range(image.get_width()):
c = image.get_at((x,y)
neg.set_at((x,y),255 - color[2],255))
return neg
解决方法
为此,您可以将 pygame.Surface.blit
与 special_flag 参数BLEND_SUB
一起使用。
创建具有相同大小的全白图像,并减去原始图像:
neg = pygame.Surface(image.get_size())
neg.fill((255,255,255))
neg.blit(image,(0,0),special_flags=pygame.BLEND_SUB)