5

I'm writing a predator-prey simulation using python and pygame for the graphical representation. I'm making it so you can actually "interact" with a creature (kill it, select it and follow it arround the world, etc). Right now, when you click a creature, a thick circle(made of various anti-aliased circles from the gfxdraw class) sorrounds it, meaning that you have succesfully selected it.

My goal is to make that circle transparent, but according to the documentation you can't set an alpha value for drawn surface. I have seen solutions for rectangles (by creating a separate semitransparent surface, blitting it, and then drawing the rectangle on it), but not for a semi-filled circle.

What do you suggest? Thank's :)

Pabce
  • 1,429
  • 2
  • 13
  • 12
  • If you create a surface, draw the circle on it, [Surface.set_alpha](http://www.pygame.org/docs/ref/surface.html#pygame.Surface.set_alpha) . Then blit that surface to screen. – ninMonkey Jul 10 '13 at 23:40

1 Answers1

11

Have a look at the following example code:

import pygame

pygame.init()
screen = pygame.display.set_mode((300, 300))
ck = (127, 33, 33)
size = 25
while True:
  if pygame.event.get(pygame.MOUSEBUTTONDOWN):
    s = pygame.Surface((50, 50))

    # first, "erase" the surface by filling it with a color and
    # setting this color as colorkey, so the surface is empty
    s.fill(ck)
    s.set_colorkey(ck)

    pygame.draw.circle(s, (255, 0, 0), (size, size), size, 2)

    # after drawing the circle, we can set the 
    # alpha value (transparency) of the surface
    s.set_alpha(75)

    x, y = pygame.mouse.get_pos()
    screen.blit(s, (x-size, y-size))

  pygame.event.poll()
  pygame.display.flip()

enter image description here

sloth
  • 99,095
  • 21
  • 171
  • 219
  • Thank you! The colorkey did the job (although the code has become a bit more complez than I wished). Just one thing: Is there any special reason you are using pygame.event.poll? Cheers – Pabce Jul 11 '13 at 23:11
  • 2
    Yes, because I call `pygame.event.get` with `pygame.MOUSEBUTTONDOWN` as argument to check only for mouse click events. If you do this, you have to call `pygame.event.poll`, otherwise the event queue gets filled up and the window becomes unresponsive after a while. There's no reason to do this if you just call `pygame.event.get()` without any parameter (what you usually do in a game). – sloth Jul 12 '13 at 06:43
  • 2
    Question: any particular reason to leave `s` creation and set up (lines from `s = pygame.Surface(...` until `s.set_alpha(...`) inside the main loop instead of before? They seem to be constant, so it seems only `x`, `y` and the `blit` need to be there – MestreLion Aug 04 '14 at 12:27
  • No, no particular reason at all :-) – sloth Aug 04 '14 at 14:29
  • Its works only when the surface background color is same color of screen, by example, set a background texture to screen, howto make same effect with surface? need make transparent the surface background but not the object. By example: a banana walk into house, when open door the banana make a fadeout. The answer to this problem is: http://stackoverflow.com/questions/328061/how-to-make-a-surface-with-a-transparent-background-in-pygame `surface = pygame.Surface((50, 50), pygame.SRCALPHA, 32) surface = surface.convert_alpha() pygame.draw.circle(...` – e-info128 Apr 15 '17 at 22:08