海龟世界的坐标系¶
在海龟的世界中,我们使用如下的平面直角坐标系:
- 原点(0,0)位于作图窗口的中央。
 - x轴正向朝左。
 - y轴正向朝上。
 
设置海龟的位置¶
setxy(x,y)函数将海龟直接放置到点(x,y)处。该操作不会留下移动痕迹。
gotoxy(x,y)则将海龟移动到点(x,y)处。因此,如果海龟处于落笔状态,该操作会留下移动痕迹。
在进行上述操作时,海龟的朝向不会改变。
下面的程序展示了gotoxy()和setxy()的结果。
from easygraphics.turtle import *
def main():
    create_world(300,300)
    gotoxy(50,-100)
    for i in range(360):
        fd(1)
        lt(1)
    pause()
    cs()
    setxy(50,-100)
    for i in range(360):
        fd(1)
        lt(1)
    pause()
    close_world()
easy_run(main)
设置海龟朝向。¶
facing(x,y)让海龟的脑袋指向点(x,y)
from easygraphics.turtle import *
def main():
    create_world(300,300)
    # set the turtle's heading to top-left corner of the graphics window
    facing(-150,150)
    fd(100)
    pause()
    close_world()
easy_run(main)
set_heading(angle)让海龟指向angle角方向。
在海龟世界中,各个方向的角度如下图所示。按照逆时针方向,0°为正右方,90°为正上方,180°为正左方,270°为正下方。
from easygraphics.turtle import *
def main():
    create_world(300,300)
    # set the turtle's heading to top-left corner of the graphics window
    set_heading(30)
    fd(100)
    pause()
    close_world()
easy_run(main)
获取海龟当前状态¶
get_x()返回海龟当前位置的x坐标值
get_y()返回海龟当前位置的y坐标值
get_heading()返回海龟朝向的角度值。
from easygraphics.turtle import *
def main():
    create_world(300,300)
    # set the turtle heading to top-left corner of the graphics window
    facing(-150,150)
    fd(100)
    draw_text(-140, -130, "(%.2f, %.2f), heading(%.2f)" % (get_x(), get_y(), get_heading()))
    pause()
    close_world()
easy_run(main)