Python中的 turtle.home()函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
乌龟.home()
此函数用于将海龟移动到原点,即坐标 (0,0)。无论如何,乌龟的位置是,它设置为(0,0),默认方向(面向东方)。
句法 :
turtle.home()
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# import package
import turtle
# check turtle position
print(turtle.position())
# motion
turtle.forward(100)
# check turtle position
print(turtle.position())
# set turtle to home
turtle.home()
# check turtle position
print(turtle.position())
# motion
turtle.right(90)
turtle.forward(100)
# check turtle position
print(turtle.position())
# set turtle to home
turtle.home()
# check turtle position
print(turtle.position())
Python3
# import package
import turtle
# set turtle speed to fastest
# for bettrt understandings
turtle.speed(10)
# method to draw a part
def fxn():
# motion
turtle.circle(50,180)
turtle.right(90)
turtle.circle(50,180)
# loop to draw pattern
for i in range(12):
fxn()
# set turtle at home
turtle.up()
turtle.home()
turtle.down()
# set position
turtle.left(30*(i+1))
输出 :
(0.0, 0.0)
(100.0, 0.0)
(0.0, 0.0)
(0.0, -100.0)
(0.0, 0.0)
示例 2:
Python3
# import package
import turtle
# set turtle speed to fastest
# for bettrt understandings
turtle.speed(10)
# method to draw a part
def fxn():
# motion
turtle.circle(50,180)
turtle.right(90)
turtle.circle(50,180)
# loop to draw pattern
for i in range(12):
fxn()
# set turtle at home
turtle.up()
turtle.home()
turtle.down()
# set position
turtle.left(30*(i+1))
输出 :