簡體   English   中英

在 pygame 中繪制透明的矩形和多邊形

[英]Draw a transparent rectangles and polygons in 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 值似乎不起作用。

pygame.draw函數不會使用 alpha 繪制。 文件說:

大多數 arguments 接受一個顏色參數,它是一個 RGB 三元組。 這些也可以接受 RGBA 四元組。 如果包含像素alphas,alpha值將直接寫入Surface,但繪制function不會透明繪制。

您可以做的是創建第二個表面,然后將其 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 文檔,尤其是介紹。

不幸的是,沒有繪制透明形狀的好方法。 pygame.draw模塊:

顏色的 alpha 值將直接寫入表面 [...],但繪制 function 不會透明繪制。

因此你需要做一個解決方法:

  1. 創建一個pygame.Surface object,每像素 alpha 格式大到足以覆蓋形狀。
  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()

我能做的最多的就是向您展示如何繪制一個未填充的矩形。矩形的線是:

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

“1”表示未填寫

一種方法是不使用 pygame 繪制矩形,而是使用 paint.net 或 Fire Alpaca 等繪圖程序創建透明矩形的圖像。

如何繪制具有 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 值似乎不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM