PyGame反色/表面

3

我正在使用PyGame创建一个游戏。我想要创建一个负面效果,使整个屏幕变成负片。

对于简单的形状(如矩形),这非常容易实现:(使用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]), area, width)
    else:
        pygame.draw.rect(win, color, area, 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[0], 255 - color[1], 255 - color[2], 255))

    return neg

1
你可能想要查看pixarray,这是一个接口,用于访问存储像素的底层缓冲区,以实现更快的读写操作。 - jthulhu
1个回答

3

您可以使用 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接