海龟图形中的海龟方向?

3

如何让海龟在海龟图形中面向一个方向? 我想让海龟转向并面向一个方向,无论它的原始位置在哪里,我该怎么做?


4
好的,总是有Python文档 - Mr. T
7个回答

6
我认为你需要的函数是 turtle.setheading(),也可以写作seth(),如果你想让它指向北方:
turtle.setheading(0)

或者

turtle.setheading(90)

根据您所处的“标准”或“标志”模式不同,此设置将有所不同。

如评论中所指出,您可以在此处找到此信息。


3
这是我用于我的游戏的东西:
#Functions
def go_up():
    head.direction="up"

def go_down():
    head.direction="down"

def go_left():
    head.direction="left"

def go_right():
    head.direction="right"


def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)

# Keyboard
win.listen()
win.onkeypress(go_up, "Up")
win.onkeypress(go_down, "Down")
win.onkeypress(go_left, "Left")
win.onkeypress(go_right, "Right")

1

我们当然可以通过 turtle.setheading() / turtle.seth() 来改变标题。

turtle.setheading() docs

我指的是这份文档 在这里

那个答案已经在5年前给出了。请在回答旧问题时,确保澄清您的答案与现有答案的区别,并且更好的是,避免发布文本图像。 - chrslg
我自己写的。 - Codermint
嗨。这不是我的观点。我没有说,甚至没有想到这是抄袭或类似的问题(那将是一个管理问题,而我不是管理员;只是一名评论员)。我相信你自己找到了答案。重点是最好避免发布冗余的答案。这是关于冗余,而不是关于价值的问题。 - chrslg
好的,但我会参考文档,这样我们可以从那里进行审查。 - Codermint

1

如果你计划将你的海龟移动到某个位置(x,y),并且你想先让你的海龟朝向那里,你可以使用:

turtle.setheading(turtle.towards(x,y))

这里还有几个 setheading 模式可用(指向另一个海龟,将方向设置为另一个海龟的方向)。链接 - ggorlen

0
你可以使用: turtle.right(angle) 或者: turtle.left(angle)。 希望这能帮到你!

1
这是相对于海龟当前方向的。 - ggorlen

0

使用setheading命令可以完成这个操作。

turtle.setheading(<degrees/radians>)

这是如何使用它的。如果不更改设置,您将处于标准模式下,因此
turtle.setheading(0)

让乌龟向右转,

turtle.setheading(90)

将海龟朝上

turtle.setheading(180)

会使海龟向左转,以及

turtle.setheading(270)

把海龟朝下放。希望这可以帮到你!


-1
import turtle

angle = 50

turtle.tiltangle(angle)

1
请不要仅仅发布代码作为答案,还要提供解释您的代码是如何解决问题的。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Mark Rotteveel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接