如何在Python的“Turtle”库中找到点的坐标?

问题描述 投票:0回答:1

我想使用 python 的 Turtle 库制作一个形状,为此,我必须找到形状中心的坐标,然后使用

goto()
命令。这样可以吗?

而且我也尝试通过移动乌龟而不是使用

goto()
命令来到达中心,但这是不可能的,或者我做错了什么。一些帮助将非常感激!

python turtle-graphics python-turtle
1个回答
0
投票

可以分享一下你的代码吗?

  1. 如果您想在屏幕上找到海龟形状的当前位置,请使用 turtle.pos()
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)

  1. 如果您尝试在中央屏幕中恢复形状,请使用 turtle.home()
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)

© www.soinside.com 2019 - 2024. All rights reserved.