Search code examples
pythonpygamedraw

Pygame: Set alpha level for shapes


How would I go about setting the alpha value of a pygame shape: pygame.draw.circle()

I tried using an RGBA value, but to no avail.. here is the draw code:

pygame.draw.circle(screen, ((0,255,0,alpha)), (px,py), 15)

px and py are the player's (circle's) position. alpha is just a place holder for what I thought would be an alpha value.

Anyone got any ideas?


Solution

  • draw.circle() doesn't use alpha so you have to draw on Surface() (to get bitmap) and add alpha and colorkey to Surface()

    import pygame
    
    pygame.init()
    
    screen = pygame.display.set_mode((800, 600), 0, 32)
    
    surface1 = pygame.Surface((100,100))
    surface1.set_colorkey((0,0,0))
    surface1.set_alpha(128)
    pygame.draw.circle(surface1, (0,255,0), (50,50), 50)
    
    surface2 = pygame.Surface((100,100))
    surface2.set_colorkey((0,0,0))
    surface2.set_alpha(128)
    pygame.draw.circle(surface2, (255,0,0), (50,50), 50)
    
    screen.blit(surface1, (100,100))
    screen.blit(surface2, (120,120))
    
    pygame.display.update()
    
    RUNNING = True
    
    while RUNNING:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                RUNNING = False
    
    pygame.quit()