Is it possible to make a rect transparent in pygame? I need it because I'm using rects as particles for my game. :P
-
Rectangles aren't transparent, surfaces can be. Please elaborate. – Malik Brahimi Jul 01 '15 at 21:46
-
Hey, I was just wondering if it was possible. – Larry McMuffin Jul 01 '15 at 21:48
-
3Voting to close as too broad because there's so little substance to this question. What's going to happen is there will be half a dozen comments asking what you want to do, then you will respond little by little, and eventually all the information that should have been in your question in the first place might be there, but at that point it is a mess. Please read the help topics on how/what to ask, then put together a question with code samples, what you are trying to do, etc. – Two-Bit Alchemist Jul 01 '15 at 21:54
-
Seems like a duplicate: [Draw a transparent rectangle in pygame](https://stackoverflow.com/questions/6339057/draw-a-transparent-rectangle-in-pygame) – sloth Jul 02 '15 at 07:09
1 Answers
pygame.draw functions will not draw with alpha. The documentation says:
Most of the arguments accept a color argument that is an RGB triplet. 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. What you can do is create a second surface and then blit it to the screen. Blitting will do alpha blending and color keys. Also, you can specify alpha at the surface level (faster and less memory) or at the pixel level (slower but more precise). You can do either:
s = pygame.Surface((1000,750)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
windowSurface.blit(s, (0,0)) # (0,0) are the top-left coordinates
or,
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128)) # notice the alpha value in the color
windowSurface.blit(s, (0,0))
Keep in mind in the first case, that anything else you draw to s will get blitted with the alpha value you specify. So if you're using this to draw overlay controls for example, you might be better off using the second alternative.
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
Check the Surface docs at the pygame site, especially the intro.
Draw a transparent rectangle in pygame
I have had this question as a pygame user before, and this is a method of solving your problem.

- 786
- 7
- 17