Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

PyGame: Transparent circle or other shape

python

pygame.draw.circle() and other method in pygame.draw don't use alpha channel to draw so you have to draw it on new Surface() (to get it as bitmap) and add alpha and colorkey to Surface() before you blit this surface to screen.

import pygame

pygame.init()

screen = pygame.display.set_mode((400, 300), 0, 32)

surface1 = pygame.Surface((100,100))
surface1.set_colorkey((0,0,0))  # use `(0,0,0)` (black color) as transparent color
surface1.set_alpha(128)  # transparency 50% for other colors
pygame.draw.circle(surface1, (0,255,0), (50,50), 50)

surface2 = pygame.Surface((100,100))
surface2.set_colorkey((0,0,0))  # use `(0,0,0)` (black color) as transparent color
surface2.set_alpha(128)  # transparency 50% for other colors
pygame.draw.circle(surface2, (255,0,0), (50,50), 50)

screen.blit(surface1, (125,100))
screen.blit(surface2, (175,100))

pygame.display.update()

running = True

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

pygame.quit()
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