【问题标题】:how to draw transparent rect in pygame如何在pygame中绘制透明矩形
【发布时间】:2018-11-11 07:21:10
【问题描述】:

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

【问题讨论】:

  • 你想知道如何只绘制轮廓或如何使轮廓透明吗?要绘制轮廓,您只需将确定宽度的 int 作为第四个参数传递给pygame.draw.rect。问题是它对于大宽度看起来相当难看,因为角落会有间隙。您还可以创建自己的矩形轮廓函数并绘制四条线。

标签: pygame transparent rect outline


【解决方案1】:

如果您只想绘制矩形的轮廓,可以将整数作为第四个(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 通道传递颜色以使轮廓透明。


还可以创建一个透明表面并在其上绘制一个矩形(您也可以在此处传递透明颜色):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-05
    • 2016-05-25
    • 2012-06-20
    • 2012-01-16
    • 2021-04-10
    • 2016-03-12
    • 2013-09-13
    • 1970-01-01
    相关资源
    最近更新 更多