本章主要学习turtle库基础的函数
1.goto
turtle.goto(x,y)
如果以屏幕中心为坐标轴(0,0)画坐标轴,x表示横坐标,y表示纵坐标。
import turtle as t
t.goto(100,100)
t.color('red')
t.write('hello world')
# 示绘图窗口
t.mainloop()
结果:
在(100,100)处写hello world
2.penup
turtle.penup()
这个代码表示不绘制出鼠标的运动轨迹
同样拿上面的例子来说
import turtle as t
t.penup()
t.goto(100,100)
t.color('red')
t.write('hello world')
# 示绘图窗口
t.mainloop()
结果:
对比上图,你可以区别penup的作用。
3.mainloop
turtle.mainloop()
作用是保持绘图窗口
4.pencolor
turtle.pencolor('red')
修改路径颜色
5.fillcolor
turtle.fillcolor('red')
填充图像内部颜色
6.left,right,forward
turtle.forward(20)
turtle.left(90)
left方向向左转
right方向向右转
forward画直线直走
7.begin_fill,end_fill
begin_fill() 开始填充
end_fill() 结束填充
注:要同时使用
turtle.begin_fill()
for i in range(2):
turtle.forward(20)
turtle.left(90)
turtle.forward(20)
turtle.left(90)
turtle.end_fill()
最后画一个正方形结束本次学习
import turtle
# 绘制矩形
turtle.goto(0, 0)
turtle.pencolor('red')
turtle.fillcolor('red')
turtle.begin_fill()
for i in range(2):
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
# 示绘图窗口
turtle.mainloop()
结果: