如何将 python 中乌龟的起始位置设置为屏幕左下角?

How do I set the starting position of my turtle in python to the bottom left of my screen?

我正在做一个涉及海龟的项目。我一直在努力寻找一种方法让乌龟从屏幕左下角开始,而不是坐标 (0,0)。

#My Code
import turtle
turtle.setworldcoordinates(-1, -1, 20, 20)

turtle.fd(250)
turtle.rt(90)
turtle.fd(250)

当我试图寻找解决方案时,我遇到了一个线程“Python turtle set start position”,它建议了多种解决问题的方法,例如我的代码中的 turtle.setworldcoordinates(-1, -1, 20, 20) 引用。如果有人有想法或解决方案,请告诉我。

如果我们知道您的 turtle.Screen

尺寸会有所帮助

您可以使用 penup()、goto() 方法。

import turtle

wn = turtle.Screen()
wn.setup(width=500, height=500)
wn.tracer(0)
wn.bgcolor('black')

bottom_left = turtle.Turtle()
bottom_left.shape('circle')
bottom_left.color('white')
bottom_left.shapesize(stretch_wid=5, stretch_len=5)
bottom_left.penup()
bottom_left.goto(-250, -250)


while True:
    wn.update()

如您所见,我绘制了一个 500x500 的黑色网格,并根据网格将乌龟放置在左下角。

希望这是您要找的东西,我在我的游戏中使用了这种方法而不是 turtle.setworldcoordinates() 方法。

你似乎想使用 setworldcoordinates() 我已经编辑了我的答案以包含该方法的解决方案。

import turtle

wn = turtle.Screen()
wn.setup(width=500, height=500)
wn.tracer(0)
wn.bgcolor('black')
wn.setworldcoordinates(-1, -1, 20, 20)

bottom_left = turtle.Turtle()
bottom_left.shape('circle')
bottom_left.color('white')
bottom_left.shapesize(stretch_wid=5, stretch_len=5)


while True:
    wn.update()

希望这对您有所帮助...

from turtle import Screen, Turtle

screen = Screen()
screen.setworldcoordinates(-1, -1, screen.window_width() - 1, screen.window_height() - 1)

turtle = Turtle('turtle')

# work with your turtle here

screen.exitonclick()

根据系统和角的不同,-1 常量可能需要高达 -10 以在显示整个 body.

时说明海龟的中心位置

您的后续代码毫无意义:

turtle.fd(250)
turtle.rt(90)
turtle.fd(250)

当乌龟开始向右行驶时,左下角的乌龟随后向右转,将使它离开屏幕。