0

I had a look online at quite a few tutorials but I can't seem to get a translucent red circle to be drawn on a surface.

I want this method to be used to highlight a sprite with a translucent red circle:

def highlight(self):
    s = pygame.Surface((self.rect[2]*2,self.rect[3]*2)) #create a surface to draw circle on twice as big as the sprite rectangle)
    s.fill((0,0,0)) #fills the surface with black
    pygame.draw.circle(s, (255,0,0), self.rect.center, self.rect[3], 0) # draws red circle on the surface
    s.set_colorkey((0,0,0)) # makes the black surface background transparent
    s.set_alpha(50) # makes the whole surface translucent (i.e. the red circle remaining)
    variables.screen.blit(s, (self.rect[0]-self.rect[2]/2,self.rect[1]-self.rect[3]/2)) #blit's the red circle centered on my sprite.

I really don't know what I do wrong... I can get the red circle alright by just using:

def highlight(self):
    pygame.draw.circle(DISPLAYSURF, (255,0,0), self.rect.center, self.rect[3], 0) # with display surf as my background for the whole game

But I can't get it translucent. I'm also reluctant to use gfxdraw as it says it might not be supported in future Pygame versions.

Thank you for your help.

Dan Gallo
  • 63
  • 5
Sorade
  • 915
  • 1
  • 14
  • 29
  • do you use 24 bit or 32 bit surfaces? anyway for transparency you need 32 bit surface. then forget about colorkey, just fill **alpha** channel with black (full transparency) around the circle. then fill the circle area alpha with some mid-value. – Mikhail V May 02 '16 at 21:18
  • Also, I'd want want to say, there are enough posts about how to use colorkey and alpha, and why you don't mix these two concepts. To start, just make simple program without any classes and try minimal cases, but first decide what type of surfaces you will use. – Mikhail V May 02 '16 at 21:29
  • Thank you Mikhail V. Do you know how I can check my surfaces are 32 or 64 bit ? If that helps I'm using python and pygame in 32 bit. – Sorade May 03 '16 at 06:52
  • By transparent you mean partially transparent? – kmaork May 03 '16 at 10:03
  • Well, the red circle needs to be partially transparent. The surface background needs to be completely transparent. – Sorade May 03 '16 at 15:29
  • sorry I was not exact. You can do it like you do it, with colorkey and then setting alpha value. Actually the code seems to be correct. most probably you mess up with coordinates and just don't see the result. As said try to test a minimal code. Also if you call your method repeatdly, you should init the surface and colorkey/alpha only once. After that just draw a circle and it must work – Mikhail V May 03 '16 at 21:57
  • So in your case you deal with deafult 24 bit surface, but there is another way to make transparent things. Then you init the surface like: `pygame.Surface((w, h), pygame.SRCALPHA, 32)` and draw circle with color (255,0,0,50) where 50 is alpha. In this case you just draw a circle without using `set_colorkey()` or `set_alpha()`. – Mikhail V May 03 '16 at 22:02
  • Also you could try to put draw circle function **after** you set colorkey and alpha. – Mikhail V May 03 '16 at 22:28

0 Answers0