2013-10-22 23 views
-1

我使用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來實現這一點

+0

請注意, Pygame本身就是一個2D框架。所以你只能用你的點的x和y座標進行繪製。如果你想有一個3D應用程序,我建議看看[PyOpenGL](http://pyopengl.sourceforge.net/)。 – kevintodisco

+0

是的,我知道這一點,我打算切換到openGL後,我只是想先用xy在pygame中工作,然後切換到OpenGL。感謝您的輸入 – user2840327

回答

-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) 
+0

這似乎應該工作,但是當我嘗試並運行它時,我得到一個SyntaxError:lambda不能包含賦值。我現在如何解決這個問題,看看這段代碼的工作原理? – user2840327

+0

哎呀,對不起,lambda表達式應該包含'=='而不是'='。答案已更新。 – kevintodisco

+0

確定修復工作只是一個更多的問題我如何乘以我的x,y座標集,我正在繪製的比例因子,以便他們顯示在我的屏幕上 – user2840327

-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)