2016-09-25 5 views
0

だから、まず、ここでの要件があります:オプションリスト(Python Turtle Graphics)から複数の図形を一度に描画しますか?

  1. ユーザーが6のリストから3つの形状を選びます。
  2. ユーザーは、サイズ、塗りつぶしの色、および線の色を選択します。
  3. ユーザは、各

画面の1/3を占め、二回

  • 形状が均等に配置描かれるべき同じ形状を選ぶことはできません。ここに私のコードは、これまでのところです:

    import turtle 
    turtle = turtle.Screen() 
    
    def circle(): 
    def triangle(): 
    def square(): 
    def pentagon(): 
    def hexagon(): 
    def heptagon(): 
    
    for list in ["1.Circle","2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]: 
        print(list) 
    shape1 = input("Choose one number from the following:") 
    
    if shape1 == "1": 
        for list in ["2.Triangle","3.Square","4.Pentagon","5.Hexagon","6.Heptagon"]: 
         print(list) 
        shape2 = input("Choose one number from the following:") 
        if shape2 == "2": 
        elif shape2 == "3": 
        elif shape2 == "4": 
        elif shape2 == "5": 
        elif shape2 == "6": 
        else: 
         print("Incorrect input. Please try again.") 
    if shape1 == "2": 
    if shape1 == "3": 
    if shape1 == "4": 
    if shape1 == "5": 
    if shape1 == "6": 
    else: 
        print("Incorrect input. Please try again.") 
    

    基本的には、私はひどく混乱している。私が見つけられる唯一の方法は、ユーザーが選択した行に3つの図形を描く唯一の方法です。123,124,125,126,134,134 ...など、すべての可能な結果を​​行うことです。これは永遠に見え、ひどく見えます。毎回カメコマンドを書かなければならないでしょう。ご覧のように、私はdefで遊んでみましたが、小さなテストコードでは全く動作しませんでしたので、正しく理解しているかわかりません。

    これらのすべてに加えて、私はどのようにしてすべての形状や位置を保証するのですか?私が見ることができる唯一の方法は、異なる "goto"を使ってそれぞれの結果に対して別々のコードを書くことです。

    ユーザーが3つのオプション(「123」、「231」など)を一度に入力し、プログラムが各番号を順番に描画する方法はありますか?シェイプを描画する一連のコードに各数値を割り当てる方法はありますか?私はこのすべてでかなり新しいです。私はあなたが私に与えることができるどんな助けにも感謝します。ありがとうございました!

  • 答えて

    0

    以下は、シェイプのリストからユーザにプロンプ​​トを表示し、キャンバスを分割して描画するフレームワークの例です。

    import turtle 
    
    CANVAS_WIDTH = 900 
    CANVAS_HEIGHT = 600 
    CHROME_WIDTH = 30 # allow for window borders, title bar, etc. 
    SHAPE_CHOICES = 3 
    
    def circle(bounds): 
        turtle.penup() 
        center_x = bounds['x'] + bounds['width'] // 2 
        bottom_y = bounds['y'] 
        turtle.setposition(center_x, bottom_y) 
        turtle.pendown() 
    
        turtle.circle(min(bounds['width'], bounds['height']) // 2) 
    
    def triangle(bounds): 
        circle(bounds) 
    
    def square(bounds): 
        circle(bounds) 
    
    def pentagon(bounds): 
        circle(bounds) 
    
    def hexagon(bounds): 
        circle(bounds) 
    
    def heptagon(bounds): 
        circle(bounds) 
    
    DESCRIPTION, FUNCTION = 0, 1 
    
    shapes = [("Circle", circle), ("Triangle", triangle), ("Square", square), ("Hexagon", hexagon), ("Heptagon", heptagon)] 
    
    choices = [] 
    
    turtle.setup(CANVAS_WIDTH + CHROME_WIDTH, CANVAS_HEIGHT + CHROME_WIDTH) 
    
    for _ in range(SHAPE_CHOICES): 
    
        for i, (description, function) in enumerate(shapes): 
          print("{}. {}".format(i + 1, description)) 
    
        choice = int(input("Choose one number from the above: ")) - 1 
    
        choices.append(shapes[choice][FUNCTION]) 
    
        del shapes[choice] 
    
    x, y = -CANVAS_WIDTH // 2, -CANVAS_HEIGHT // 2 
    
    width, height = CANVAS_WIDTH // SHAPE_CHOICES, CANVAS_HEIGHT // SHAPE_CHOICES 
    
    # I'm dividing the screen into thirds both horizontally and vertically 
    bounds = dict(x=x, y=y, width=width, height=height) 
    
    for choice in choices: 
        choice(bounds) 
    
        bounds['x'] += width 
        bounds['y'] += height 
    
    turtle.done() 
    
    +0

    あなたは命の恩人だ、ありがとう:それはあなたが他の形状に記入する必要があり、はるかに完成したコードから、あなたはエラーチェックや他の最後の仕上げを追加する必要があります、円を実装しています。これは大いに役立ちます。 – Seren

    関連する問題