How can I draw a rectangle that has a color with an alpha? I have:
如何绘制具有alpha颜色的矩形?我有:
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.
但我希望白色矩形透明度为50%,但alpha值似乎不起作用。
2 个解决方案
#1
38
pygame.draw
functions will not draw with alpha. The documentation says:
pygame.draw函数不会用alpha绘制。文件说:
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.
大多数参数接受一个RGB三元组的颜色参数。这些也可以接受RGBA四联体。如果alpha值包含像素alpha,则alpha值将直接写入Surface,但绘制函数不会透明绘制。
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:
你可以做的是创建第二个表面,然后将其blit到屏幕。 Blitting将执行alpha混合和颜色键。此外,您可以在曲面级别(更快和更少的内存)或像素级别(更慢但更精确)指定alpha。你可以这样做:
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.
请记住,在第一种情况下,您绘制到s的任何其他内容都将使用您指定的alpha值进行blitting。因此,如果您正在使用它来绘制叠加控件,那么最好使用第二种替代方法。
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
另外,请考虑使用pygame.HWSURFACE来创建表面硬件加速。
Check the Surface docs at the pygame site, especially the intro.
检查pygame站点上的Surface文档,尤其是介绍。
#2
-8
You can add a fourth value to your color tuple to represent Alpha:
您可以向颜色元组添加第四个值来表示Alpha:
pygame.draw.rect(windowSurface, (255, 255, 255, 127), pygame.Rect(0, 0, 1000, 750))
#1
38
pygame.draw
functions will not draw with alpha. The documentation says:
pygame.draw函数不会用alpha绘制。文件说:
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.
大多数参数接受一个RGB三元组的颜色参数。这些也可以接受RGBA四联体。如果alpha值包含像素alpha,则alpha值将直接写入Surface,但绘制函数不会透明绘制。
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:
你可以做的是创建第二个表面,然后将其blit到屏幕。 Blitting将执行alpha混合和颜色键。此外,您可以在曲面级别(更快和更少的内存)或像素级别(更慢但更精确)指定alpha。你可以这样做:
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.
请记住,在第一种情况下,您绘制到s的任何其他内容都将使用您指定的alpha值进行blitting。因此,如果您正在使用它来绘制叠加控件,那么最好使用第二种替代方法。
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
另外,请考虑使用pygame.HWSURFACE来创建表面硬件加速。
Check the Surface docs at the pygame site, especially the intro.
检查pygame站点上的Surface文档,尤其是介绍。
#2
-8
You can add a fourth value to your color tuple to represent Alpha:
您可以向颜色元组添加第四个值来表示Alpha:
pygame.draw.rect(windowSurface, (255, 255, 255, 127), pygame.Rect(0, 0, 1000, 750))