furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

PyGame transparent Surface [GB]

If you use

surface.set_colorkey([0,0,0])

then later blit() will skip pixels which have color [0,0,0]

This way you can remove background from surface or loaded image.

In this example I remove black color [0, 0, 0] so it blits only rectangles on

python pygame - set_colorkey remove background

With this method you can use

surface.set_alpha(128)

to create semi transparent object

python pygame - set_colorkey remove background and set_alpha to make semi transparent
import pygame

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25

BLACK  = (0, 0, 0)
WHITE  = (255, 255, 255)
GREEN  = (0, 255, 0)
RED    = (255, 0, 0)
RED    = (255, 0, 0)
BLUE   = (0, 0, 255)
YELLOW = (255,255,0)

GRAY_1 = (128,128,128)
GRAY_2 = (192,192,192)

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

# ---

background = pygame.surface.Surface(screen.get_size())

color = GRAY_1
for x in range(0, SCREEN_WIDTH, 40):
    for y in range(0, SCREEN_HEIGHT, 40):
        pygame.draw.rect(background, color, [x, y, 40, 40])
        if color == GRAY_1:
            color = GRAY_2
        else:
            color = GRAY_1

# ---

surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0])

pygame.draw.polygon(surface, RED,    [(200,100), (400,300), (600,100)])
pygame.draw.polygon(surface, RED,    [(200,100), (400,300), (600,100)])
pygame.draw.polygon(surface, GREEN,  [(200,500), (400,300), (600,500)])
pygame.draw.polygon(surface, BLUE,   [(200,100), (400,300), (200,500)])
pygame.draw.polygon(surface, YELLOW, [(600,100), (400,300), (600,500)])

surface.set_alpha(128)

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    # --- draws ---

    screen.blit(background, (0,0))
    screen.blit(surface, (0,0))
    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)

# --- end ---

pygame.quit()

Changing set_alpha() you can create this

python pygame - set_colorkey remove background and set_alpha to make semi transparent
import pygame

# --- constants --- (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25

BLACK  = (0, 0, 0)
WHITE  = (255, 255, 255)
GREEN  = (0, 255, 0)
RED    = (255, 0, 0)
RED    = (255, 0, 0)
BLUE   = (0, 0, 255)
YELLOW = (255,255,0)

GRAY_1 = (128,128,128)
GRAY_2 = (192,192,192)

# --- main ---

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

# ---

background = pygame.surface.Surface(screen.get_size())

color = GRAY_1
for x in range(0, SCREEN_WIDTH, 40):
    for y in range(0, SCREEN_HEIGHT, 40):
        pygame.draw.rect(background, color, [x, y, 40, 40])
        if color == GRAY_1:
            color = GRAY_2
        else:
            color = GRAY_1

# ---

surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0])

pygame.draw.polygon(surface, RED,    [(200,100), (400,300), (600,100)])
pygame.draw.polygon(surface, RED,    [(200,100), (400,300), (600,100)])
pygame.draw.polygon(surface, GREEN,  [(200,500), (400,300), (600,500)])
pygame.draw.polygon(surface, BLUE,   [(200,100), (400,300), (200,500)])
pygame.draw.polygon(surface, YELLOW, [(600,100), (400,300), (600,500)])

surface.set_alpha(128) # semi transparent all pixels

lower = True
alpha = 255

# --- mainloop ---

clock = pygame.time.Clock()

running = True
while running:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    # --- changes ---

    if lower:
        alpha -= 10
        if alpha <= 10:
            lower = False
    else: 
        alpha += 10
        if alpha >= 250:
            lower = True

    surface.set_alpha(alpha)

    # --- draws ---

    screen.blit(background, (0,0))
    screen.blit(surface, (0,0))
    pygame.display.flip()

    # --- FPS ---

    ms = clock.tick(FPS)

# --- end ---

pygame.quit()

But using set_alpha() (and set_colorkey()) all pixels have the same transparency.

They can have differen transparency like with .convert_aplha()


You can use it also with images. In old games it was popular to use pink color for background.

If you don't know what color was used then you can get first pix in image

Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury