2016-05-24 66 views
0

私はPythonを初めて使い、質問があります。私は、タートルを特定の開始位置に移動し、そこから形状を描きたい。形状にはあらかじめ決められた座標があるので、形状を作るために点を接続する必要があります。座標リストを使用してPython/Turtleで図形を描く方法

は、私は次のコードは、これら2つの機能を呼び出し三の形状を描くことが2つの機能は、行う必要があります。

def testPolyLines(): 
    # First square 
    squareShape = [(50, 0), (50, 50), (0, 50), (0, 0)] 
    drawPolyLine((200, 200), squareShape) 
    # Second square 
    drawPolyLine((-200, 200), squareShape, lineColour="green") 
    biggerSquareShape = generateSquarePoints(100) 
    # A triangle 
    triangleShape = [(200, 0), (100, 100), (0, 0)] 
    drawPolyLine((100, -100), triangleShape, fillColour="green") 

def main(): 
    testPolyLines() 
main() 

Iは、任意のサイズの正方形のための点を生成するために第1の関数を作った:

def generateSquarePoints(i): 
    squareShape = [(i, 0), (i, i), (0, i), (0, 0)] 

実際に形を描くことになると、私は立ち往生します。私は、タートルを開始位置に移動させることができますが、ポイントのリストを通り、それらを接続して形を形成する方法を知らない。これは私が持っているものです。

def drawPolyLine(start, squareShape, lineColour="black", fillColour = "white"): 
    pencolor(lineColour) 
    fillcolor(fillColour) 
    penup() 
    goto(start) 
    pendown() 
    begin_fill() 
    goto(squareShape) 
    end_fill() 

これは明らかに適切ではない...私はおよそ混乱している部分が形成するために、ポイントのリストに移動して、道に沿ってそれらを接続するためにカメを伝える方法ですかたち。私のプログラムは最初の位置に移動するだけですが、シェイプを描画しません。

本当にありがとうございました。前もって感謝します。

+0

あなたはすべての点のリストを持っています。すべてのポイントを使用しようとしています(「十分ではない」など)。だから、あなたは何をしようとしていても、順番に各ポイントを繰り返し処理する 'for'ループが必要です。ポイントを訪れてみてください。 –

+0

また、四角形の関数は "オフセット"を許さないので間違っています。位置100,50を中心とする20x20の正方形の座標を書き留めます。関数がそれを行うことができるかどうかを確認してください。 –

+0

あなたはどのライブラリを使用していますか? –

答えて

0

あなたのコードに問題があります:あなたはgoto()のポイントを調整する必要があります。そのポイントのx &yをdelta-x、delta-yより多く扱う必要があります。 generateSquarePoints()は点を返す必要があります。明らかに、forというループが必要です。シェイプを閉じるには、開始点に明示的に引き戻す必要があります。

はそれが何をしたいんかどうかを確認するためにあなたの次のコードの手直しをしてみてください。

import turtle 

def generateSquarePoints(i): 
    """ generate points for a square of any size """ 
    return [(i, 0), (i, i), (0, i), (0, 0)] 

def drawPolyLine(start, points, lineColour="black", fillColour="white"): 
    """ draw shapes using a list of coordinates """ 
    turtle.pencolor(lineColour) 
    turtle.fillcolor(fillColour) 

    turtle.penup() 

    turtle.goto(start) # make the turtle go to the start position 

    turtle.pendown() 
    turtle.begin_fill() 

    x, y = start 

    for point in points: # go through a list of (relative) points 
     dx, dy = point 
     turtle.goto(x + dx, y + dy) 
    turtle.goto(start) # connect them to start to form a closed shape 

    turtle.end_fill() 
    turtle.penup() 

if __name__ == "__main__": 

    def testPolyLines(): 
     """ test shapes shape drawing functions """ 
     # First square 
     squareShape = [(50, 0), (50, 50), (0, 50), (0, 0)] 
     drawPolyLine((200, 200), squareShape) 

     # Second square 
     biggerSquareShape = generateSquarePoints(100) 
     drawPolyLine((-200, 200), biggerSquareShape, lineColour="green") 

     # A triangle 
     triangleShape = [(200, 0), (100, 100), (0, 0)] 
     drawPolyLine((100, -100), triangleShape, fillColour="green") 


    def main(): 
     testPolyLines() 
     turtle.done() 

    main() 
関連する問題