基本命令¶
在本教程中,我们将介绍海龟作图的基本命令。
移动海龟¶
forward(x)函数让海龟前进x步。fd(x)是它的简写形式。
from easygraphics.turtle import *
def main():
create_world(250,250)
fd(100)
pause()
close_world()
easy_run(main)
backward(x)函数让海龟后退x步。back(x)和bk(x)是它的简写形式。
from easygraphics.turtle import *
def main():
create_world(400,400)
bk(100)
pause()
close_world()
easy_run(main)
旋转海龟¶
right_turn(x)让海龟顺时针旋转x度。right(x)和rt(x)是它的简写形式。
left_turn(x)让海龟逆时针旋转x度。left(x)和lt(x)是它的简写形式。
下面的程序画了一个30度的角。
from easygraphics.turtle import *
def main():
create_world(400,400)
fd(80)
bk(80)
rt(30)
fd(80)
pause()
close_world()
easy_run(main)
海龟移动速度¶
我们可以使用set_speed()函数来控制海龟的移动速度。速度值越高,海龟移动越快。1是海龟速度的最小值。如果你不想要动画,那么可以用set_immediate(True)来取消动画。
抬笔和落笔¶
如果你希望海龟移动时不留痕迹,可以使用抬笔(pen_up())和落笔(pen_down()) 。
缺省状态下海龟处于落笔状态(pen_down()),因此其移动会留下痕迹。
如果海龟处于提笔状态(pen_up()),它的移动不留痕迹。
在下面的程序中,我们使用提笔和落笔来绘制两个嵌套的正方形。
from easygraphics.turtle import *
def main():
create_world(400,400)
# draw the inside rectangle
for i in range(4):
fd(100)
lt(90)
# use pen_up to move the turtle without a trace
pen_up()
rt(135)
fd(70)
lt(135)
pen_down()
# draw the outside rectangle
for i in range(4):
fd(200)
lt(90)
pause()
close_world()
easy_run(main)
显示和隐藏海龟¶
当绘画结束时,我们可以使用hide()隐藏海龟。
show()函数重新显示海龟。
from easygraphics.turtle import *
def main():
create_world(400,400)
for i in range(4):
fd(100)
lt(90)
hide()
pause()
close_world()
easy_run(main)