
turtle实际上是一个画板,它为你提供了一个画笔。让我们先设置基本属性:
1,运动属性
forward(d) 向前移动距D代表距离
backward(d) d代表向后移动距离d代表距离
right(degree) 向右转多少度?
left(degree) 左转多少度?
goto(x,y) 将画笔移动到坐标上(x,y)的位置
stamp() 绘制当前图形
speed(speed) 画笔画的速度范围[0,10]整数
2,画笔属性
down()画笔落下,移动时绘制图形
up()提起画笔,移动时不绘制图形
reset()恢复所有设置
pensize(width)画笔的宽度
pencolor(colorstring)画笔的颜色
fillcolor(colorstring)绘制图形的填充颜色
fill(Ture)开始
fill(False)结束
也可以color(”color,’color’)
circle(radius, extent)
画一个圆,其中radius为半径,extent为度数。例如,如果extent为180,则画一个半圆;如果你想画一个圆,你不必写第二个参数
3,例子
下面是一箭穿心的图像
fromturtleimport*
fromtimeimportsleep
defgo_to(x,y):
up()
goto(x,y)
down()
defbig_Circle(size):#用来画心的大圆函数
speed(1)
foriinrange(150):
forward(size)
right(0.3)
defsmall_Circle(size):#用来绘制心的小圆的函数
speed(1)
foriinrange(210):
forward(size)
right(0.786)
defline(size):
speed(1)
forward(51*size)
defheart(x,y,size):
go_to(x,y)
left(150)
begin_fill()
line(size)
big_Circle(size)
small_Circle(size)
left(120)
small_Circle(size)
big_Circle(size)
line(size)
end_fill()
defarrow():
pensize(10)
setheading(0)
go_to(-400,0)
left(15)
forward(150)
go_to(339,178)
forward(150)
defarrowHead():
pensize()
speed(1)
color('red','red')
begin_fill()
left(120)
forward(20)
right(150)
forward(35)
right(120)
forward(35)
right(150)
forward(20)
end_fill()
defmain():
pensize(2)
color('red','pink')
#getscreen().tracer(30,0)#取消注释后,快速显示图案
heart(200,0,1)#画出第一颗心,前两个参数控制心的位置,最后一个参数控制心的大小
setheading(0)#使画笔的方向朝向x轴的正方向
heart(-80,-100,1.5)#画第二颗心
arrow()#画出穿过两颗心的直线
arrowHead()#画箭头
go_to(400,-300)
write("author:超哥",move=True,align="left",font=("宋体",30,"normal"))
done()
main()更多技术请关注Python视频教程。