【问题标题】:draw lines from a set of coordinates pygame从一组坐标pygame中画线
【发布时间】:2013-10-22 03:07:06
【问题描述】:

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

【问题讨论】:

  • 请注意 Pygame 本身就是一个 2D 框架。因此,您将只能使用您的点的xy 坐标进行绘制。如果您想要一个 3D 应用程序,我建议您查看PyOpenGL
  • 是的,我确实知道这一点,我计划稍后切换到 openGL,我只想让程序先在 pygame 中使用 xy,然后切换到 OpenGL。感谢您的意见

标签: python list drawing pygame


【解决方案1】:

绘制点的二维分量,可以先生成需要绘制的线组,然后使用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)

【讨论】:

  • 这似乎应该可以工作,但是当我尝试运行它时,我得到一个 SyntaxError: lambda cannot contain assignment。我现在该如何解决这个问题,看看这段代码是否有效?
  • 哎呀,抱歉,lambda 表达式应该包含== 而不仅仅是=。答案已更新。
  • 好的,修复只是一个问题,我如何将我正在绘制的 x,y 坐标集乘以比例因子,以便它们显示在我的屏幕上
  • 我建议找到您拥有的最小和最大 xy 坐标,获取它们之间的范围,然后将该范围上的值映射到您的窗口大小。这将是一个“按比例调整”操作。
【解决方案2】:

试试这个

lines = []

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

    lines.append(point)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-04
    • 2017-12-28
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多