python turtle画一个网格

代码:

import turtle

# 网格规格
s = 30
# 间隔
n = 30
# 初始点位置
x = -(s / 2 * n)
y = -(s / 2 * n)
print(x, y)

# 定义初始网格线条颜色
turtle.pencolor('gray')
# 加快画线速度
turtle.speed(99999)
# 画横线
for i in range(s):
    turtle.penup()
    turtle.goto(x, y + n * i)
    turtle.pendown()
    turtle.forward(n * (s - 1))

# 画竖线
turtle.left(90)
for i in range(s):
    turtle.penup()
    turtle.goto(x + n * i, y)
    turtle.pendown()
    turtle.forward(n * (s - 1))

# 隐藏画笔箭头
turtle.hideturtle()
# ------------网格绘制完成--------------
# 开始画线
# 抬笔
turtle.penup()
# 进入到初始位置
turtle.goto(0, 0)
# 落笔
turtle.pendown()
# 定义线条颜色
turtle.pencolor("#DC143C")
# 画线速度
turtle.speed(2)
# 线条宽度
turtle.pensize(5)
# 展示箭头
turtle.showturtle()


# 根据你自己算法来实现画线
# forward() 前进
# backward()后退
# right()   右转
# left()    左转
turtle.forward(300)
turtle.left(90)
turtle.forward(60)
turtle.right(90)
turtle.forward(60)
turtle.left(90)
turtle.forward(360)
turtle.left(90)
turtle.forward(480)

# 固定住屏幕
turtle.done()
image.png

你可能感兴趣的:(python turtle画一个网格)