2017-11-11 224 views
0

我用烏龜製作了字母「U」,但現在我需要製作它的幾個副本來形成3行4列的網格。我試圖使用嵌套循環,但我不知道如何使繪圖移動位置來創建網格。它應該結束了這樣的事情logo grid如何使用python的烏龜創建繪圖的網格

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

如果其中一個答案解決了您的問題,您應該接受它(單擊相應答案旁邊的複選標記)。這有兩件事。它讓每個人都知道你的問題已經得到解決,讓你滿意,並且它可以幫助你幫助你。請參閱[此處](http://meta.stackexchange.com/a/5235)以獲取完整說明。 –

回答

0

draw_Grid你需要使用的行數和列數來計算每個U的首發位置,但你還需要它畫完U形後把draw_U使它將龜放回原來的方向。

下面是您的代碼的修復版本。

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

我們只畫信一次,即圖形保存爲龜光標,將我們的烏龜切換到該光標並開始複印。