Draw a Transparent Rectangles and Polygons in Pygame

Draw a transparent rectangles and polygons in pygame

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.

make a rect transparent in pygame

If you want to make the images transparent, you need to make sure that the alpha channel of the images is set. Additionally you must use convert_alpha() instead of convert():

if ball_color:
ball = pygame.image.load("ball.png").convert_alpha()
else:
ball = pygame.image.load("medicine-ball.png").convert_alpha()

See also the answers to the questions:

  • How to convert the background color of image to match the color of Pygame window?
  • Pygame image transparency confusion
  • pygame image background does not match main background
  • How can I make an Image with a transparent Backround in Pygame?


make a rect transparent in pygame

Unfortunately there is no good way to draw a transparent shape. See the answers to the question Draw a transparent rectangle in pygame and see pygame.draw module:

A color's alpha value will be written directly into the surface [...], but the draw function will not draw transparently.

Hence you need to do a workaround:

  1. Create a pygame.Surface object with a per-pixel alpha format large enough to cover the shape.
  2. Draw the shape on the _Surface.
  3. Blend the Surface with the target Surface. blit() by default blends 2 Surfaces

For example 3 functions, which can draw transparent rectangles, circles and polygons:

def draw_rect_alpha(surface, color, rect):
shape_surf = pygame.Surface(pygame.Rect(rect).size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, color, shape_surf.get_rect())
surface.blit(shape_surf, rect)

Use the function in your code instead of pygame.draw.rect, alpha is a value in range [0, 255]:

def rect(x1, y1, x2, y2, alpha = 255):
#pygame.draw.rect(screen, (0,0,0), (x1,y1,x2,y2))
draw_rect_alpha(screen, (0, 0, 0, alpha), (x1, y1, x2, y2))

how to draw transparent rect in pygame

If you just want to draw the outline of a rect, you can pass an integer as the fourth (the width) argument to pygame.draw.rect:

pygame.draw.rect(screen, (0, 100, 255), (50, 50, 162, 100), 3)  # width = 3

The problem with this approach is that the corners won't look sharp and clean and the outline can't be transparent.


You could also use the gfxdraw module to draw several outlines with a for loop:

def draw_rect_outline(surface, rect, color, width=1):
x, y, w, h = rect
width = max(width, 1) # Draw at least one rect.
width = min(min(width, w//2), h//2) # Don't overdraw.

# This draws several smaller outlines inside the first outline. Invert
# the direction if it should grow outwards.
for i in range(width):
pygame.gfxdraw.rectangle(screen, (x+i, y+i, w-i*2, h-i*2), color)

draw_rect_outline(screen, (250, 50, 162, 100), (0, 100, 255, 155), 9)

That also allows you to pass a color with an alpha channel to make the outline transparent.


It would also be possible to create a transparent surface and draw a rect onto it (you can pass a transparent color here as well):

surf = pygame.Surface((162, 100), pygame.SRCALPHA)
pygame.draw.rect(surf, (0, 100, 255, 155), (0, 0, 162, 100), 21)

If you want a filled, transparent rectangle, just fill the complete surface:

surf.fill((0, 100, 255, 155))

How to make transparent pygame.draw.circle

You've to use a pygame.Surface. Create a Surface with a per pixel alpha image format. e.g:

radius = 100
circle = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)

And draw a transparent circle on it. The color of the circle has to have an alpha channel < 255 (e.g. 128):

pygame.draw.circle(circle, (255, 0, 0, 128), (radius, radius), radius)

blit() the Surface to the window. e.g.:

window.blit(circle, (100, 100))

Example:

Sample Image

import pygame

pygame.init()
wndsize = (400, 400)
window = pygame.display.set_mode(wndsize)
clock = pygame.time.Clock()

run = True
while run:
clock.tick(60)

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

window.fill(0)
pygame.draw.rect(window, (0, 0, 255), (0, 0, 200, 400))

radius = 100
circle = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)
pygame.draw.circle(circle, (255, 0, 0, 128), (radius, radius), radius)
window.blit(circle, (100, 100))

pygame.display.flip()

Is There a Way to Make a Rect Transparent in Pygame?

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.

I can't draw a filled rectangle

You just need to do it in two steps. First create the surface, then fill it. Creating a surface makes a separate image, so it takes a size not a rectangle.

my_color = (0, 0, 0)

my_rect = (100, 100, 200, 150)

my_size = ( 200, 150 )

box = pygame.Surface( my_size )
box.fill( my_colour )

surf.blit( box, my_rect )

If you just want to draw a rectangle, you can use the drawing functions, which use the base surface directly:

pygame.draw.rect( surf, my_color, my_rect )


Related Topics



Leave a reply



Submit