Python中的turtle.pos()方法
Turtle 模块以面向对象和面向过程的方式提供Turtle图形基元。因为它使用 Tkinter 作为底层图形,它需要安装一个支持 Tk 的 Python 版本。
turtle.pos()
该方法用于查找Turtle的当前位置(x, y),是一个Vec2D-向量。这个方法的别名是:pos | position。
语法: turtle.pos() or turtle.position()
返回:turtle的当前位置(x,y)坐标。
该函数不需要任何参数,以(x,y)的格式返回Turtle的当前位置,其中x和y代表2D矢量。默认值是(0.0, 0.0)。
下面是上述方法的实现和一些例子。
例子1 :
# import turtle package
import turtle
# print the default
# position i.e; (0.0, 0.0)
print(turtle.pos())
# forward turtle by 150 pixels
turtle.forward(150)
# print current position
# i.e; (150.0, 0.0)
print(turtle.pos())
# forward turtle by 150 pixels
# after taking turn right
# by 90 degrees
turtle.right(90)
turtle.forward(150)
# print position (after next move)
# i.e; (150.0, -150.0)
print(turtle.pos())
输出:
(0.0, 0.0)
(150.0, 0.0)
(150.0, -150.0)
示例 2:
# import turtle package
import turtle
# print position (by default)
# i.e; (0.0, 0.0)
print(turtle.pos())
# turtle move forward
# by 40 pixels
turtle.forward(40)
# print position (after move)
# i.e; (150.0, 0.0)
print(turtle.position())
# turtle move forward by 40 pixels
# after taking right turn
# by 45 degrees
turtle.right(45)
turtle.forward(40)
# print position
# (after next move)
print(turtle.pos())
# turtle move forward by 80
# pixels after taking left
# turn by 90 degrees
turtle.left(90)
turtle.forward(80)
# print position
# (after next move)
print(turtle.pos())
# turtle move forward
# by 40 pixels after taking
# right turn by 90 degrees
turtle.right(90)
turtle.forward(40)
# print position (after next move)
print(turtle.position())
# turtle move forward by
# 40 pixels after taking
# left turn by 45 degrees
turtle.left(45)
turtle.forward(40)
# print position
# (after final move)
print(turtle.pos())
输出 :
(0.0, 0.0)
(40.0, 0.0)
(68.2842712475, -28.2842712475)
(124.852813742, 28.2842712475)
(153.13708499, 0.0)
(193.13708499, 0.0)