Python中的 turtle.pos() 方法
Turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
乌龟.pos()
此方法用于查找海龟的当前位置 (x, y),作为 Vec2D 向量。此方法具有别名:pos |位置。
Syntax: turtle.pos() or turtle.position()
Return: turtle’s current location in terms of (x, y) coordinate
此函数不需要任何参数,并以 (x,y) 格式返回海龟的当前位置,其中 x 和 y 表示 2D 向量。默认值为 (0.0, 0.0)。
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# 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())
Python3
# 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)
(150.0, 0.0)
(150.0, -150.0)
示例 2:
Python3
# 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)