Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transparent rectangle in pygame [duplicate]

I am trying to draw simple semi-transparent rectangle in pygame. I have tried this code:

import pygame, sys,pygame.mixer

pygame.init()
size = (width, height) = (400, 400)
screen = pygame.display.set_mode(size)
screen.fill((255,255,255))
pygame.draw.rect(screen, (23, 100, 255, 50), (100,100,100,100),0)

running = True
while running:
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
            pygame.quit()
            running = False

But as you can see, the rectangle is not semi-transparent. It's like I inserted the color 23, 100, 255, 255) rather than (23, 100, 255, 50).

like image 761
user1767774 Avatar asked Dec 27 '22 15:12

user1767774


1 Answers

You can also use gfxdraw which has several options and accepts alpha in color.

You'll need to import:

import pygame.gfxdraw

And use with:

pygame.gfxdraw.box(screen, pygame.Rect(0,0,200,200), (100,0,0,127))
like image 72
Gust Jc Avatar answered Jan 03 '23 06:01

Gust Jc