3

I'm trying to draw a semi-transparent circle in Pygame. Here is my code:

import pygame
pygame.init()
clock = pygame.time.Clock()
screen=pygame.display.set_mode((width,height))

while True:
    msElapsed = clock.tick(100)
    screen.fill((0,0,0,255))
    pygame.draw.circle(screen,(30,224,33,100),(250,100),10)
    pygame.display.update()

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

I want to use RGBA as circle color. But the circle is fully colored. What's the problem with my code?

mokazemi
  • 58
  • 1
  • 6
  • 1
    According to [the docs for `pygame.draw`](https://www.pygame.org/docs/ref/draw.html): "These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently." – abarnert Jul 08 '18 at 08:16
  • Do you mean that there is no way to draw a transparent shape? @abarnert – mokazemi Jul 08 '18 at 08:27
  • 1
    Apparently not with `pygame.draw`. – abarnert Jul 08 '18 at 08:31
  • See here: https://stackoverflow.com/a/6350227/8881141 – Mr. T Jul 08 '18 at 08:32
  • How about refering to [this](https://stackoverflow.com/questions/31989468/how-to-make-a-circle-semi-transparent-in-pygame)? – Hoseong Jeon Jul 08 '18 at 08:39

2 Answers2

3

Fix:

First you need to create a surface which accepts transparency

surface = pygame.Surface((width,height), pygame.SRCALPHA)

Then from this line:

pygame.draw.circle(screen,(30,224,33,100),(250,100),10)

CHANGE screen TO surface SO IT SHOULD LOOK LIKE:

pygame.draw.circle(surface,(30,224,33,100),(250,100),10)

and after that add:

screen.blit(surface, (0,0))

Explanation:

When you were drawing the circle onto the screen it ignored the transparency values as the pygame screen cannot accept them. What I have done is created a new layer/screen which can accept transparency (due to this argument pygame.SRCALPHA) and then pasted that layer/screen on top of the original screen.

Ben
  • 3,160
  • 3
  • 17
  • 34
0

this one wokred for me

def draw_transparent_circle(win,x,y,radius,color,alpha_level):
    pygame.gfxdraw.filled_circle(win,x,y,radius,(color[0],color[1],color[2],alpha_level))