0

我正在使用 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 来实现这一点

4

2 回答 2

-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)
于 2013-10-22T15:23:06.370 回答
-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)
于 2013-10-22T09:45:33.100 回答