绘制同心圆
思路:
先在(0,0)处画半径为R = 15的圆,在去画半径增加r = 10的圆,
发现只要将圆心(0,0)更改为(0,10),半径更改为(R+r)
import turtle
t = turtle.Pen() #画笔
t.width(5) #画笔加粗
my_color = ('red','green','blue','pink','black') #给圆添加色彩
R = 15 #绘制的初始圆大小
r = 10 #每次圆的半径扩大10
t.speed(0) #加速绘制,参数1是最慢的,0是最快的
for i in range(20):
t.penup()
t.goto(0,-i*r)
t.pendown()
t.color(my_color[i%len(my_color)])
t.circle(R + i*r)
turtle.done() #绘制完后,窗口停留
运行结果:
绘制棋盘:
思路:
先画条横线,在将画笔提至下一行再画条横线,以此类推,绘制n条这样的横线,
此时,坐标变为刚开始处,再依据画横线的方式,绘制n条竖线
import turtle
t = turtle.Pen()
t.speed(0)
num = 15 #绘制 n * n 的棋盘
width = 30 #每条线之间的宽度
x = y = num/2*width #设置每一条线段的长度
#绘制横线
for i in range(num+1):
t.penup()
t.goto(-x,y - i*width)
t.pendown()
t.goto(x,y - i*width)
#绘制竖线
for j in range(num+1):
t.penup()
t.goto(-x + j*width,y)
t.pendown()
t.goto(-x + j*width,-y)
t.hideturtle() #隐藏画笔箭头
turtle.done()
运行结果: