在pygame中绘制透明矩形

时间:2011-06-14 04:28:30

标签: python pygame

如何绘制具有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))

但我希望白色矩形透明度为50%,但alpha值似乎不起作用。

5 个答案:

答案 0 :(得分:45)

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

  

大多数参数都接受一个RGB三元组的颜色参数。这些也可以接受RGBA四联体。如果alpha值包含像素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值进行blitting。因此,如果您使用它来绘制叠加控件,那么最好使用第二种替代方法。

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

检查pygame网站上的Surface docs,尤其是简介。

答案 1 :(得分:2)

不幸的是,没有好的方法可以绘制透明的形状。请参见pygame.draw模块:

颜色的Alpha值将直接写入表面[...],但是draw函数不会透明地绘制。

因此,您需要采取一种解决方法:

  1. 创建一个pygame.Surface对象,该对象的像素大小应足以覆盖该形状。
  2. 在_Surface上绘制形状。
  3. Surface 与目标 Surface 混合。 blit()默认情况下会混合2个表面

例如3个可以绘制透明矩形,圆形和多边形的函数:

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)
def draw_circle_alpha(surface, color, center, radius):
    target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
    shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
    pygame.draw.circle(shape_surf, color, (radius, radius), radius)
    surface.blit(shape_surf, target_rect)
def draw_polygon_alpha(surface, color, points):
    lx, ly = zip(*points)
    min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
    target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
    shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
    pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
    surface.blit(shape_surf, target_rect)

最小示例: repl.it/@Rabbid76/PyGame-TransparentShapes

import pygame

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)

def draw_circle_alpha(surface, color, center, radius):
    target_rect = pygame.Rect(center, (0, 0)).inflate((radius * 2, radius * 2))
    shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
    pygame.draw.circle(shape_surf, color, (radius, radius), radius)
    surface.blit(shape_surf, target_rect)

def draw_polygon_alpha(surface, color, points):
    lx, ly = zip(*points)
    min_x, min_y, max_x, max_y = min(lx), min(ly), max(lx), max(ly)
    target_rect = pygame.Rect(min_x, min_y, max_x - min_x, max_y - min_y)
    shape_surf = pygame.Surface(target_rect.size, pygame.SRCALPHA)
    pygame.draw.polygon(shape_surf, color, [(x - min_x, y - min_y) for x, y in points])
    surface.blit(shape_surf, target_rect)

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

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (160, 160, 160), (192, 192, 192)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
    pygame.draw.rect(background, color, rect)

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.blit(background, (0, 0))

    draw_rect_alpha(window, (0, 0, 255, 127), (55, 90, 140, 140))
    draw_circle_alpha(window, (255, 0, 0, 127), (150, 100), 80)
    draw_polygon_alpha(window, (255, 255, 0, 127), 
        [(100, 10), (100 + 0.8660 * 90, 145), (100 - 0.8660 * 90, 145)])

    pygame.display.flip()

pygame.quit()
exit()

答案 2 :(得分:0)

我能为您提供的最大帮助是向您展示如何绘制未填充的矩形。 矩形的线是:

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

“ 1”表示未填写

答案 3 :(得分:0)

一种方法是,不用pygame绘制矩形,您可以使用paint.net或Fire Alpaca等绘图程序来创建透明矩形的图像。

答案 4 :(得分:-8)

您可以在颜色元组中添加第四个值来表示Alpha:

pygame.draw.rect(windowSurface, (255, 255, 255, 127), pygame.Rect(0, 0, 1000, 750))