So I am making an auto generated game where in each level there is a different color palette. For now, I want to switch one color inside my player sprite. I used this video: https://www.youtube.com/watch?v=MLrDR-Mks8I to make a function that takes in the color I want to swap and the new color to replace it. However, when I do this, it replaces the color in the sprite but also creates a rectangle around it that is the same color as the new color. This is probably because I am filling the new surface and creating a colorkey for the old surface, which fills in the whole hitbox of the sprite.
def palette_swap(self, surf, old_c, new_c):
img_copy = pygame.Surface(surf.get_size())
img_copy.fill(new_c)
surf.set_colorkey(old_c)
img_copy.blit(surf, (0, 0))
return img_copy
def animate(self):
image = self.palette_swap(image, (255, 148, 66), (249, 4, 99))
I don't want to upload all my code because it is a lot but just know the animate function updates the image. Here is what happens when I apply this function to every sprite:
For reference there isn't supposed to be rectangles around each sprite. Is there another approach to palette swapping in pygame that doesn't require this method? And no, this isn't the same as changing the hue of a sprite, I am changing the palette, which means I am changing the colors individually. I thought this wouldn't be difficult since I have a color palette with only 4 colors, and I am looking to change only 2 of them for each level.



You have to use
pygame.maskfro your task.Create a
Maskfrom the colorConvert the
Maskto aSurfacewith the new colorCopy the image and draw the color mask surface on top of it
Minimal example