• 4

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Draw a transparent rectangle in pygame

How can I draw a rectangle that has a color with an alpha? I have:

windowSurface = pygame.display.set_mode((1000, 750), pygame.DOUBLEBUF)
pygame.draw.rect(windowSurface, pygame.Color(255, 255, 255, 128), pygame.Rect(0, 0, 1000, 750))

But I want the white rectangle to be 50% transparent, but the alpha value doesn't appear to be working.

      • 1
    • Hmm, since you are not explicitly specifying bit depth, you might try windowSurface = pygame.display.set_mode((1000, 750), pygame.DOUBLEBUF, 32). Only 32bit surfaces support alpha channel.
      • 2
    • You need to add more code, as the above line seems fine. Either there is something wrong with window initialization so that it doesn't support alpha, or you are actually drawing the rect multiple times without clearing the screen so that the rect appears opaque.

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


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.

  • 55
Reply Report

the most i can do to help you is to show you how to draw a rectangle that is not filled in. the line for the rectangle is:

pygame.draw.rect(surface, [255, 0, 0], [50, 50, 90, 180], 1)

the "1" means that it is not filled in

  • 0
Reply Report

A way to do this is to instead of drawing a rectangle with pygame, you could create an image of a transparent rectangle by using a drawing program such as paint.net or Fire Alpaca.

  • 0
Reply Report

You can add a fourth value to your color tuple to represent Alpha:

pygame.draw.rect(windowSurface, (255, 255, 255, 127), pygame.Rect(0, 0, 1000, 750))
  • -9
Reply Report

Trending Tags