如何使用python的乌龟创建绘图的网格
问题描述:
我用乌龟制作了字母“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)
答
在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()
答
这似乎是在那里冲压会更有效率比绘制的情况:
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()
我们只画信一次,即图形保存为龟光标,将我们的乌龟切换到该光标并开始复印。
如果其中一个答案解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这有两件事。它让每个人都知道你的问题已经得到解决,让你满意,并且它可以帮助你帮助你。请参阅[此处](http://meta.stackexchange.com/a/5235)以获取完整说明。 –