Pages

duminică, 24 mai 2020

PyGame : Effects - part 004.

This is an simple example about how can be use palette colors.
This effect was used in the old game development.
The base of this python script is the Surface.
If you read the documentation then you can see this:
pygame object for representing images
Surface((width, height), flags=0, depth=0, masks=None) -> Surface
Surface((width, height), flags=0, Surface) -> Surface
In this example I used a new_color to change one color.
Let's see the example:
#!/usr/bin/python
import sys 
import pygame
from pygame.locals import *

pygame.init()
pygame.display.set_caption('swap color pallete')
screen = pygame.display.set_mode((640,480),0,32)

clock = pygame.time.Clock()

test_image = pygame.image.load('test.png').convert()
image_back = test_image.copy()
def palette_swap(img_surface, old_color, new_color):
    
    img_copy = pygame.Surface(test_image.get_size())
    img_copy.fill(new_color)
    img_surface.set_colorkey(old_color)
    img_copy.blit(img_surface,(0,0))
    return img_copy

test_image = palette_swap(test_image, (210,0,1), (0,0,255)) 
test_image = palette_swap(test_image, (154, 209, 59), (205, 124, 97))   
test_image.set_colorkey((0,0,0))

while True:
    screen.fill((0,0,0))
    screen.blit(pygame.transform.scale(test_image,(test_image.get_width()*2, 
                                                   test_image.get_height() * 2)), (0,0))
    screen.blit(pygame.transform.scale(image_back,(image_back.get_width()*2, 
                                                   image_back.get_height() * 2)), (256,0))
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
    pygame.display.update()
    clock.tick(60)
    # print the fps , time amd tick for this pygame test 
    #print(clock.get_fps(), clock.get_time(),clock.tick())
The test.png is a simple png file image with the size of 128 x 128 pixels.
This is the output of this simple example: