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 :)

1
  • If you create a surface, draw the circle on it, Surface.set_alpha . Then blit that surface to screen.
    – ninMonkey
    Jul 10, 2013 at 23:40

1 Answer 1

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

5
  • 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, 2013 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, 2013 at 6: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 4, 2014 at 12:27
  • No, no particular reason at all :-)
    – sloth
    Aug 4, 2014 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: stackoverflow.com/questions/328061/… surface = pygame.Surface((50, 50), pygame.SRCALPHA, 32) surface = surface.convert_alpha() pygame.draw.circle(...
    – e-info128
    Apr 15, 2017 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.