gpt4 book ai didi

pygame - 如何在pygame中绘制透明矩形

转载 作者:行者123 更新时间:2023-12-04 00:03:49 24 4
gpt4 key购买 nike

我可以画一个内部透明但轮廓不透明的矩形吗?如果我可以设置轮廓的宽度会更好。

like this

最佳答案

如果您只想绘制矩形的轮廓,您可以将整数作为第四个(width)参数传递给 pygame.draw.rect。 :

pygame.draw.rect(screen, (0, 100, 255), (50, 50, 162, 100), 3)  # width = 3

这种方法的问题是边角看起来不清晰和干净,轮廓也不透明。

您也可以使用 gfxdraw使用 for 绘制多个轮廓的模块环形:
def draw_rect_outline(surface, rect, color, width=1):
x, y, w, h = rect
width = max(width, 1) # Draw at least one rect.
width = min(min(width, w//2), h//2) # Don't overdraw.

# This draws several smaller outlines inside the first outline. Invert
# the direction if it should grow outwards.
for i in range(width):
pygame.gfxdraw.rectangle(screen, (x+i, y+i, w-i*2, h-i*2), color)

draw_rect_outline(screen, (250, 50, 162, 100), (0, 100, 255, 155), 9)

这也允许您通过 Alpha channel 传递颜色以使轮廓透明。

也可以创建一个透明表面并在其上绘制一个矩形(您也可以在此处传递透明颜色):
surf = pygame.Surface((162, 100), pygame.SRCALPHA)
pygame.draw.rect(surf, (0, 100, 255, 155), (0, 0, 162, 100), 21)

如果你想要一个填充的透明矩形,只需 fill完整的表面:
surf.fill((0, 100, 255, 155))

关于pygame - 如何在pygame中绘制透明矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53246660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com