从一组坐标pygame中绘制线条

时间:2013-10-22 03:07:07

标签: python list drawing pygame

我正在使用pygame在屏幕上绘制一组行,我有以下代码:

points = [list(map(int,elem.split())) if elem.strip().lower() != "j" else [-1, -1, -1] for elem in vlist]

此代码将采用我的xyz坐标并将其存储为以下格式的列表:

[[-1,-1,-1],[366,-1722,583],[366,356,1783],[566,789,1033],[866,-1289,-167],[366,-1722,583],[-1,-1,-1],[-500,-1472,-600],[0,-1039,-600].....]

每个等于[-1,-1,-1]的元素表示我需要停止绘制并移动到下一个点以继续绘制新线的点。

所以我需要画线

[366,-1722,583],[366,356,1783],[566,789,1033],[866,-1289,-167],[366,-1722,583]

然后我需要停止绘制并移动到新点并从我的新点开始绘制

[-500,-1472,-600],[0,-1039,-600]

并继续这样阅读,直到我的点数结束

那么如何使用pygame.draw.line来实现这个

2 个答案:

答案 0 :(得分:-1)

试试这个

lines = []

for point in points:
    if point == (-1,-1,-1):
        pygame.draw.lines(Surface, color, closed, lines, width=1)
        lines = []
        continue

    lines.append(point)

答案 1 :(得分:-1)

绘制点的2D组件,您可以先生成需要绘制的线组,然后使用pygame.draw.lines绘制它们:

from itertools import groupby

# Some itertools magic to split the list into groups with [-1,-1,-1] as the delimiter.
pointLists = [list(group) for k, group in groupby(points, lambda x: x == [-1,-1,-1]) if not k]
color = (255,255,255)
for pointList in pointLists:
    # Only use the x and y components of the points.
    drawPoints = [[l[0], l[1]] for l in pointList]
    # Assume 'screen' is your display surface.
    pygame.draw.lines(screen, color, False, drawPoints)