Python中的 turtle.setpos() 和 turtle.goto() 函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
海龟.setpos()
此方法用于将海龟移动到绝对位置。此方法有别名:setpos、setposition、goto。
Syntax: turtle.setpos(x, y=None) or turtle.setposition(x, y=None) or turtle.goto(x, y=None)
Parameters:
x: x coordinate of a Vec2D-vector
y: y coordinate of a Vec2D-vector
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# import package
import turtle
# forward turtle by 100
turtle.forward(100)
# stamp the turtle shape
turtle.stamp()
# set the position by using setpos()
turtle.up()
turtle.setpos(-50,50)
turtle.down()
# forward turtle by 100
turtle.forward(100)
# stamp the turtle shape
turtle.stamp()
# set the position by using goto()
turtle.up()
turtle.goto(-50,-50)
turtle.down()
# forward turtle by 100
turtle.forward(100)
Python3
# import package
import turtle
# method to raw pattern
# of circle with rad radius
def draw(rad):
# draw circle
turtle.circle(rad)
# set the position by using setpos()
turtle.up()
turtle.setpos(0,-rad)
turtle.down()
# loop for pattern
for i in range(5):
draw(20+20*i)
输出 :
示例 2:
Python3
# import package
import turtle
# method to raw pattern
# of circle with rad radius
def draw(rad):
# draw circle
turtle.circle(rad)
# set the position by using setpos()
turtle.up()
turtle.setpos(0,-rad)
turtle.down()
# loop for pattern
for i in range(5):
draw(20+20*i)
输出 :