簡體   English   中英

在pygame中畫一條透明線

[英]Drawing a transparent line in pygame

我需要一種在 pygame 中繪制部分透明線的方法。 我找到了這個答案( python - Draw a transparent Line in pygame )但它只適用於直線,不適用於不同的線條粗細。

您必須創建具有任意大小和alpha通道的新表面

surface = pygame.Surface((width, height)).convert_alpha()

或使用主曲面創建具有相同大小的新曲面

surface = screen.convert_alpha()

用透明顏色(0,0,0,0)填充它。 重要的是最后一個零,這意味着alpha通道 - (R,G,B,A)

surfaces.fill([0,0,0,0])

使用小於255的 Alpha 通道在此表面上繪制

pygame.draw.line(surface, (0, 0, 0, 32), (0, 0), (800, 600), 5)

最后,您可以在任何地方的主頁上對其進行 blit

screen.blit(surface, (x, y))

對於與主表面具有相同尺寸的表面,它可以是(0,0)

screen.blit(surface, (0,0))

最小的例子

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))#, depth=32)

surface1 = screen.convert_alpha()
surface1.fill([0,0,0,0])
pygame.draw.circle(surface1, (255, 0, 0, 128), (325, 250), 100)

surface2 = screen.convert_alpha()
surface2.fill([0,0,0,0])
pygame.draw.circle(surface2, (0, 255, 0, 128), (475, 250), 100)

surface3 = screen.convert_alpha()
surface3.fill([0,0,0,0])
pygame.draw.circle(surface3, (0, 0, 255, 128), (400, 350), 100)

surface4 = screen.convert_alpha()
surface4.fill([0,0,0,0])
pygame.draw.line(surface4, (0, 0, 0, 32), (0, 0), (800, 600), 5)
pygame.draw.line(surface4, (0, 0, 0, 32), (0, 600), (800, 0), 5)

surface5 = screen.convert_alpha()
surface5.fill([0,0,0,0])
pygame.draw.polygon(surface5, (255, 0, 0, 128), [(400, 250), (450, 300), (400, 350), (350, 300)])

screen.fill([255,255,255]) # white background
screen.blit(surface1, (0,0))
screen.blit(surface2, (0,0))
screen.blit(surface3, (0,0))
screen.blit(surface4, (0,0))
screen.blit(surface5, (0,0))

pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False

pygame.quit()    

在此處輸入圖像描述

暫無
暫無

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

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