有没有办法让 Pygame 中的矩形透明?

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

是否可以在 pygame 中使矩形透明? 我需要它,因为我在游戏中使用矩形作为粒子。 :P

pygame.draw 函数不会使用 alpha 绘制。文档说:

大多数参数接受颜色参数,即 RGB 三元组。这些也可以接受 RGBA 四元组。如果包含像素 alpha,alpha 值将直接写入 Surface,但绘制函数不会透明绘制。 您可以做的是创建第二个表面,然后将其 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

或者,

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))

请记住,在第一种情况下,您绘制到 s 的任何其他内容都将使用您指定的 alpha 值进行 blit。因此,例如,如果您使用它来绘制叠加控件,则最好使用第二种方法。

此外,考虑使用 pygame.HWSURFACE 创建表面硬件加速。

在 pygame 站点查看 Surface 文档,尤其是介绍。

Draw a transparent rectangle in pygame

我以前作为pygame用户遇到过这个问题,这是解决你问题的方法。