2017-11-11 16 views
0

私はカメを使って "U"という文字を作ったが、今度はそれをコピーして3行4列のグリッドを作る必要がある。ネストされたループを使用しようとしていましたが、グリッドを作成するために図面を移動位置にする方法はありません。それはあなたがそれぞれのU.の開始位置を計算するために、行番号と列番号を使用する必要がありますしかし、あなたはまた、それはUを描き終えています後draw_Uように修正する必要がありdraw_Gridでこのlogo gridPythonのカメを使って描画グリッドを作る方法

def draw_U(posx, posy, color): 
t = turtle.Turtle() 
t.speed(10) 
t.ht() #hides the turtle/pen 
t.penup() 
t.setposition(posx,posy) 
t.pendown() 
t.color(color) 
t.begin_fill()#starts filling 
t.forward(60)#line 1 starting at top left corner of the 'U' 
t.right(90) 
t.forward(25) #line 2 
t.right(90) 
t.forward(8) #line 3 
t.left(90) 
t.forward(138)#line 4 
t.left(45) 
t.forward(13) #line 5 
t.left(45) 
t.forward(75) #line 6 
t.left(45) 
t.forward(13) #line 7 
t.left(45) 
t.forward(138) #line 8 
t.left(90) 
t.forward(8) #line 9 
t.right(90) 
t.forward(25) #line 10 
t.right(90) 
t.forward(60) #line 11 
t.right(90) 
t.forward(25) #line 12 
t.right(90) 
t.forward(8) #line 13 
t.left(90) 
t.forward(163)#line 14 
t.right(45) 
t.forward(35) #line 15 
t.right(45) 
t.forward(133) #line 16 
t.right(45) 
t.forward(35) #line 17 
t.right(45) 
t.forward(163) #line 18 
t.left(90) 
t.forward(8) #line 19 
t.right(90) 
t.forward(25) #line 20 
t.end_fill() #completely fills shape 

def draw_Grid(posx, posy, rows, cols): 
t= turtle.Turtle() 
t.ht() 
for i in range(cols): 
    for j in range(rows): 
     print(draw_UH(posx, posy, 'red')) 

draw_Grid(-300, 300, 3, 4) 
+0

以下のいずれかの回答が問題を解決する場合は、それを受け入れる必要があります(該当する回答の横にあるチェックマークをクリックしてください)。それは2つのことをします。あなたの問題があなたの満足のために解決されたことを誰にでも知らせることができます。詳しい説明は[here](http://meta.stackexchange.com/a/5235)を参照してください。 –

答えて

0

のようなものに終わる必要がありますそれは元の方向に向かって、それが開始された場所にカメを戻します。

修正されたバージョンのコードです。

import turtle 

def draw_U(t, posx, posy, color): 
    t.penup() 
    t.setposition(posx,posy) 
    t.pendown() 
    t.color(color) 
    t.begin_fill()#starts filling 
    t.forward(60)#line 1 starting at top left corner of the 'U' 
    t.right(90) 
    t.forward(25) #line 2 
    t.right(90) 
    t.forward(8) #line 3 
    t.left(90) 
    t.forward(138)#line 4 
    t.left(45) 
    t.forward(13) #line 5 
    t.left(45) 
    t.forward(75) #line 6 
    t.left(45) 
    t.forward(13) #line 7 
    t.left(45) 
    t.forward(138) #line 8 
    t.left(90) 
    t.forward(8) #line 9 
    t.right(90) 
    t.forward(25) #line 10 
    t.right(90) 
    t.forward(60) #line 11 
    t.right(90) 
    t.forward(25) #line 12 
    t.right(90) 
    t.forward(8) #line 13 
    t.left(90) 
    t.forward(163)#line 14 
    t.right(45) 
    t.forward(35) #line 15 
    t.right(45) 
    t.forward(133) #line 16 
    t.right(45) 
    t.forward(35) #line 17 
    t.right(45) 
    t.forward(163) #line 18 
    t.left(90) 
    t.forward(8) #line 19 
    t.right(90) 
    t.forward(25) #line 20 
    t.end_fill() #completely fills shape 

    # Reset the original direction and position 
    t.right(90) 
    t.penup() 
    t.setposition(posx,posy) 
    t.pendown() 

def draw_Grid(t, ox, oy, rows, cols): 
    t.ht() 
    patwidth, patheight = 200, 220 
    for i in range(cols): 
     posx = ox + patwidth * i 
     for j in range(rows): 
      posy = oy + patheight * j 
      draw_U(t, posx, posy, 'red') 

# Make the turtle window 90% off the screen width & height 
turtle.setup(width=0.9, height=0.9) 
width, height = turtle.window_width(), turtle.window_height() 

t = turtle.Turtle() 
t.speed(10) 
t.ht() #hides the turtle/pen 

#draw_U(t, 0, 0, 'red') 
draw_Grid(t, 10-width//2, 230-height//2, 3, 4) 

turtle.done() 
0

これはを刻印することを描くよりも効率的であるような状況のように思える:私たちは、一度だけ文字を描く

from turtle import Turtle, Screen 

BORDER = 20 
LETTER_WIDTH, LETTER_HEIGHT = 200 + BORDER, 210 + BORDER 
COLUMNS, ROWS = 4, 3 

def draw_U(t): 
    t.forward(60) # line 1 starting at top left corner of the 'U' 
    t.right(90) 
    t.forward(25) # line 2 
    t.right(90) 
    t.forward(8) # line 3 
    t.left(90) 
    t.forward(138) # line 4 
    t.left(45) 
    t.forward(13) # line 5 
    t.left(45) 
    t.forward(75) # line 6 
    t.left(45) 
    t.forward(13) # line 7 
    t.left(45) 
    t.forward(138) # line 8 
    t.left(90) 
    t.forward(8) # line 9 
    t.right(90) 
    t.forward(25) # line 10 
    t.right(90) 
    t.forward(60) # line 11 
    t.right(90) 
    t.forward(25) # line 12 
    t.right(90) 
    t.forward(8) # line 13 
    t.left(90) 
    t.forward(163) # line 14 
    t.right(45) 
    t.forward(35) # line 15 
    t.right(45) 
    t.forward(133) # line 16 
    t.right(45) 
    t.forward(35) # line 17 
    t.right(45) 
    t.forward(163) # line 18 
    t.left(90) 
    t.forward(8) # line 19 
    t.right(90) 
    t.forward(25) # line 20 

def stamp_Grid(turtle, x, y, rows, cols): 
    for c in range(cols): 
     turtle.setx(x + LETTER_WIDTH * c) 

     for r in range(*((0, rows) if c % 2 == 0 else (rows - 1, -1, -1))): 
      turtle.sety(y + LETTER_HEIGHT * r) 
      turtle.stamp() 

screen = Screen() 
screen.setup(COLUMNS * LETTER_WIDTH, ROWS * LETTER_HEIGHT) 
screen.setworldcoordinates(0, 0, screen.window_width(), screen.window_height()) 

turtle = Turtle(visible=False) 
turtle.speed('fastest') 
turtle.penup() 

turtle.setheading(90) 
turtle.begin_poly() 
draw_U(turtle) 
turtle.end_poly() 

screen.register_shape('U', turtle.get_poly()) 

turtle.reset() 
turtle.penup() 
turtle.shape('U') 
turtle.color('red') 

stamp_Grid(turtle, BORDER/2, LETTER_HEIGHT - BORDER/2, 3, 4) 

turtle.hideturtle() 

screen.exitonclick() 

亀カーソルとしてその図面を保存タートルをそのカーソルに変えてコピーを打ち抜く。

関連する問題