我最近开始学习 pygame
,这就是我的问题。无论如何,我可以为使用 pygame.draw.lines
绘制的每条线使用不同的颜色吗?这是我的代码。
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480), pygame.SCALED)
screen.fill('white')
pygame.draw.lines(screen, 'red', True, [(10, 100), (20, 200), (30, 100)]) # all lines are red
while True:
pygame.display.update()
我得到的输出
我想要的输出
我尝试了什么
pygame.draw.lines(screen, ['green', 'blue', 'orange'], True, [(10, 100), (20, 200), (30, 100)])
错误
ValueError: invalid color argument
换句话说,我想在下面的代码中实现我想要的但是使用 pygame.draw.lines
pygame.draw.line(screen, 'green', (10, 100), (20, 200))
pygame.draw.line(screen, 'blue', (20, 200), (30, 100))
pygame.draw.line(screen, 'orange', (30, 100), (10, 100))
我检查了 pygame.draw.lines
docs 但我不确定那里的 color
参数是否采用我给出的列表。它只提到“Color or int or tuple(int, int, int, [int]))
”
这个例子是一个三角形,但在我的实际用例中,我想画一个十边形,如果我想为每个使用不同的颜色,我不确定使用 pygame.draw.line
10 次是否合适线。
PS:如果需要任何说明,请告诉我。非常感谢。
答案 0 :(得分:4)
您必须分别绘制每条线段。编写一个函数,使用一系列点和颜色来画线:
def draw_colorful_line(surf, colors, closed, points, width=1):
for i in range(len(points)-1):
pygame.draw.line(surf, colors[i], points[i], points[i+1], width)
if closed:
pygame.draw.line(surf, colors[-1], points[-1], points[0], width)
使用函数画线:
colors = ['green', 'blue', 'red']
points = [(10, 100), (20, 200), (30, 100)]
draw_colorful_line(screen, colors, True, points)