Python内置turtle海龟库函数讲解 2
一、以下函数是turtle海龟库内关于海龟的状态函数
1、position()或 pos()获取海龟的当前位置坐标
无参数
如:
turtle.pos()
返回当前位置坐标
2、towards(x,y)与参考点的夹角
参数:x,y坐标值,为towards测量夹角做参考
如:
turtle.goto(100,100)
移动到坐标点(100,100)
turtle.towards(0,0)
225
测量(0,0)点与(100,100)点的夹角是225度
3、xcor()返回当前点的x坐标
无参数
如
>>> turtle.goto(30,30)
移动到(30,30)的坐标点
>>> turtle.xcor()
30
返回当前点的x坐标值30
4、ycor()返回当前点的y坐标
无参数
如:
>>> turtle.goto(30,30)
>>> turtle.ycor()
30
返回当前点y的坐标值30
5、heading()返回海龟当前方位与(0,0)点(既原点)的夹角
无参数
如:
>>> turtle.right(80)
向右转80度
>>> turtle.heading()
280.0
返回夹角(360-80)
6、distance(x,y)测量与参考坐标点之间的长度
参数(x,y)为参考坐标点
如:
>>> turtle.setpos(100,0)
移动到(100,0)坐标点
>>> turtle.setpos(100,100)
继续移动到(100,100)坐标点
>>> turtle.setpos(0,100)
继续移动到(0,100)坐标点
>>> turtle.distance(0,0)
100.0
测量当前坐标点与(0,0)点的距离 为100
二、以下是设置单位的两个函数
1、degrees(fullcircle)设置圆或圆弧以角度为单位,默认360度。
参数:fullcircle 设置圆周多少度,默认圆一周360度
如:turtle.degrees(400)
释义:设置圆的单位为度,一圈是400度。
2、radians()设置圆或圆弧单位为弧度
无参数
如:
>>> turtle.home()
返回零点
>>> turtle.left(90)
海龟向左转90度
>>> turtle.heading()
90.0
返回海龟现在的夹角也是90
>>> turtle.radians()
设置单位为弧度
>>> turtle.heading()
1.5707963267948966
返回海龟现在的夹角是弧度1.57079632......