如何使用python的乌龟创建绘图网格

时间:2017-11-11 07:04:16

标签: python turtle-graphics

我使用乌龟制作了字母“U”,但是现在我需要制作它的几个副本以形成3行和4列的网格。我试图使用嵌套循环,但我不知道如何使绘图移动位置来创建网格。它最终应该是这样的logo grid

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)

2 个答案:

答案 0 :(得分:0)

draw_Grid中,您需要使用行号和列号计算每个U的起始位置。但您还需要修复draw_U,以便在完成绘制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()

答案 1 :(得分: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()

我们只绘制一次这个字母,将该图形保存为乌龟光标,将我们的乌龟切换到该光标并开始标记副本。