我想使用 python 的 Turtle 库制作一个形状,为此,我必须找到形状中心的坐标,然后使用
goto()
命令。这样可以吗?
而且我也尝试通过移动乌龟而不是使用
goto()
命令来到达中心,但这是不可能的,或者我做错了什么。一些帮助将非常感激!
可以分享一下你的代码吗?
from turtle import Turtle, Screen
import random as rnd
screen = Screen()
my_shape = Turtle(shape='turtle')
my_shape.penup()
my_shape.goto(x=rnd.randint(1, 50), y=rnd.randint(1, 50)) # send turtle to random position ve+ x and ve+ y coordinate
curr_pos = my_shape.pos() # get current turtle position by turtle.pos() method
print(f"current position: {curr_pos}")
screen.exitonclick()
输出-->
current position: (45.00,5.00)
from turtle import Turtle, Screen
import random as rnd
screen = Screen()
my_shape = Turtle(shape='turtle')
my_shape.penup()
my_shape.goto(x=rnd.randint(1, 50), y=rnd.randint(1, 50)) # send turtle to random position ve+ x and ve+ y coordinate
curr_pos = my_shape.pos() # get current turtle position by turtle.pos() method
print(f"current position: {curr_pos}")
my_shape.home() # send turtle to home position (0,0)
home_pos = my_shape.pos()
print(f"Home position: {home_pos}")
screen.exitonclick()
输出-->
current position: (48.00,41.00) Home position: (0.00,0.00)