3

I'm creating a game with PyGame. There I want to create negative effect, where all screen becomes negative.

This is quite easy for simple shapes like rects: (and use rect instead of 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)

But I don't know how to do this effect on images. I tried to do negative image copy, but if this effect will not be instant and will fill screen gradually, then this will not help.

# 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
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Somebody
  • 115
  • 6
  • 1
    You may want to look at pixarray, an interface to the under-the-hood buffer which stores the pixel, to have faster read/write ops. – jthulhu Aug 31 '20 at 06:33

1 Answers1

3

You can use pygame.Surface.blit with the special_flag argument BLEND_SUB for this.
Create a completely white image with the same size and subtract the original image:

neg = pygame.Surface(image.get_size())
neg.fill((255, 255, 255))
neg.blit(image, (0, 0), special_flags=pygame.BLEND_SUB)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174