【问题标题】:Turtle direction in turtle graphics?海龟图形中的海龟方向?
【发布时间】:2018-05-30 04:44:26
【问题描述】:

如何在海龟图形中告诉海龟朝向一个方向? 我想让乌龟无论原来的位置都转向并面向一个方向,我该如何实现?

【问题讨论】:

标签: python turtle-graphics python-turtle


【解决方案1】:

我认为 turtle.setheading() AKA seth() 是您正在寻找的功能,所以如果您希望它指向北方:

turtle.setheading(0)

turtle.setheading(90)

取决于您是处于“标准”模式还是“徽标”模式。

正如 cmets 中指出的,您可以找到此信息 here

【讨论】:

    【解决方案2】:

    这是我在游戏中使用的:

    #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")
    

    【讨论】:

      【解决方案3】:

      或者如果您计划将您的乌龟移动到某个地方 (x, y) 并且您想先将您的乌龟指向那里,您可以使用:

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

      【讨论】:

      • 更多setheading模式可用here(指向另一只海龟,将航向设置为另一只海龟的航向)。
      【解决方案4】:

      您可以使用: turtle.right(angle) 要么: turtle.left(angle)。 希望这会有所帮助!

      【讨论】:

      • 这是相对于海龟当前的方向。
      【解决方案5】:

      setheading 命令可以做到这一点。

      turtle.setheading(<degrees/radians>)
      

      是你将如何使用它。在不更改设置的情况下,您将处于标准模式,所以

      turtle.setheading(0)
      

      会面对乌龟,

      turtle.setheading(90)
      

      会让乌龟朝上,

      turtle.setheading(180)
      

      会面向左边的乌龟,并且

      turtle.setheading(270)
      

      会让乌龟朝下。希望这会有所帮助!

      【讨论】:

        【解决方案6】:
        import turtle
        
        angle = 50
        
        turtle.tiltangle(angle)
        

        【讨论】:

        • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
        【解决方案7】:

        无论在哪里都可以转一个角度,你可以使用

        turtle.setheading()
        

        把你的角度(转弯方向)放在括号里。

        【讨论】:

          最近更新 更多