Swap color palette in Pygame (dafluffypotato)

I’ve put some changes in the code of dafluffypotato to change the color palette into a sprite in pygame. The image is by daFluffyPotato.

#!/usr/bin/python3.4
# Setup Python ----------------------------------------------- #
import pygame, sys

# Setup pygame/window ---------------------------------------- #
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('game base')
screen = pygame.display.set_mode((550, 500),0,32)

tree_img = pygame.image.load('tree.png').convert()

def palette_swap(surf, old_c, new_c):
    img_copy = pygame.Surface(tree_img.get_size())
    img_copy.fill(new_c)
    surf.set_colorkey(old_c)
    img_copy.blit(surf, (0, 0))
    return img_copy


def swap(tree_img):
    "Changes color of the image"
    tree_img = palette_swap(tree_img, (11, 70, 97), (17, 11, 96))
    tree_img = palette_swap(tree_img, (15, 106, 99), (83, 32, 145))
    tree_img = palette_swap(tree_img, (35, 152, 77), (167, 65, 131))
    tree_img = palette_swap(tree_img, (154, 209, 59), (205, 124, 97))
    tree_img.set_colorkey((0, 0, 0))
    return tree_img

tree_img2 = swap(tree_img)

# Loop ------------------------------------------------------- #
while True:
    
    # Background --------------------------------------------- #
    screen.fill((0,0,0))

    screen.blit(pygame.transform.scale(tree_img, (tree_img.get_width() * 2, tree_img.get_height() * 2)), (50, 50))
    screen.blit(pygame.transform.scale(tree_img2, (tree_img.get_width() * 3, tree_img.get_height() * 3)), (200, 100))
    
    # Buttons ------------------------------------------------ #
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
                
    # Update ------------------------------------------------- #
    pygame.display.update()
    mainClock.tick(60)
    

 

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.