pygame-ce
pygame-ce copied to clipboard
Transparency with pygame.draw
trafficstars
pygame.draw doesn't apply transparency, but the docs imply it should
reproducer
import pygame
screen = pygame.display.set_mode((500, 500))
OPAQUE = (255, 0, 0, 255)
TRANSLUCENT = (255, 0, 0, 128)
TRANSPARENT = (255, 0, 0, 0)
opaque_rect = pygame.Rect(0, 0, 100, 100)
translucent_rect = pygame.Rect(100, 0, 100, 100)
transparent_rect = pygame.Rect(200, 0, 100, 100)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("black")
pygame.draw.rect(screen, OPAQUE, opaque_rect)
pygame.draw.rect(screen, TRANSLUCENT, translucent_rect)
pygame.draw.rect(screen, TRANSPARENT, transparent_rect)
pygame.display.flip()
draw functions do apply the alpha, but in SDL's BLENDMODE_NONE fashion, which means it replaces target alpha. The display surface doesn't have an alpha channel so that gets ignored. If you draw to a SRCALPHA surface and blit that, it will blend properly (as is default behaviour for blitting).
I guess this might benefit from clarification in the docs (maybe an example), but it's not a bug.
Here is a relevant issue about adding a blendmode kwarg to draw functions: #2220