文档说“对于aapolygon,请使用aalines和‘闭合’参数。”,但是pygame.draw.aalines不允许我指定宽度(0 =填充),使其不填充表面。这看起来很糟糕:
这些圆圈看起来好多了:
我该怎么做?
我使用二次贝塞尔曲线生成表面,其坐标被附加到列表中。然后我像这样将其绘制到表面上(在上图中我做了两次,一次用于外圆,一次用于内圆):
pygame.draw.polygon(self.image,fclr,self.points)
以及绘图代码(shape.image与上面代码中的self.image是同一个表面):
screen.fill((0,0,0))
screen.blit(shape.image,(100,100))
pygame.display.flip()
martineau 的评论很中肯:为了用 pygame 绘制抗锯齿填充形状,请使用模块
gfxdraw
并绘制一个常规填充形状和一个抗锯齿轮廓。来自 http://www.pygame.org/docs/ref/gfxdraw.html:
“要绘制抗锯齿和填充形状,请首先使用该函数的 aa* 版本,然后使用填充版本。”
请注意,您需要显式导入
gfxdraw
,即 from pygame import gfxdraw
。
我做了一些测试,用我的 pygame 版本(2.1.2)在多边形(锯齿状边缘)顶部绘制多边形(线条)会导致右侧和下侧出现锯齿,因为这些线条位于锯齿状多边形区域内。我对一种解决方法函数进行了快速测试,该函数使用超级采样将锯齿状多边形转换为平滑多边形。对于任何对此感兴趣的人来说,这是代码:
def draw_aapolygon(surface, color, points, scale=2):
"""
Draw antialiased polygon using supersampling.
"""
# Calculate minimum x and y values.
x_coords = tuple(x for x, _ in points)
x_min, x_max = min(x_coords), max(x_coords)
y_coords = tuple(y for _, y in points)
y_min, y_max = min(y_coords), max(y_coords)
# Calculate width and height of target area.
w = x_max - x_min + 1
h = y_max - y_min + 1
# Create scaled surface with properties of target surface.
s = pygame.Surface((w * scale, h * scale), 0, surface)
s_points = [((x - x_min) * scale, (y - y_min) * scale)
for x, y in points]
pygame.draw.polygon(s, color, s_points)
# Scale down surface to target size for supersampling effect.
s2 = pygame.transform.smoothscale(s, (w, h))
# Paint smooth polygon on target surface.
surface.blit(s2, (x_min, y_min))
请随意指出错误,但它使用了相当多的资源,这是显而易见的。
更新:
smoothscale
函数不支持透明度,当我再次使用它时,我将编写一个使用透明度的版本。同时您可以使用此函数作为概念证明。
请保持透明度! 😢🙏