【问题标题】:Stop the `extent` argument in Turtle from changing direction of circle阻止 Turtle 中的 `extent` 参数改变圆的方向
【发布时间】:2020-05-01 18:22:50
【问题描述】:

我正在做一项家庭作业,其中的说明是:

使用 Turtle 图形,实现函数 planets(),它将模拟火星行星旋转一圈期间水星、金星、地球和火星的行星运动。你可以假设:

  1. 在模拟开始时,所有行星都排成一列(比如沿着负 y 轴)。
  2. 水星、金星、地球和火星与太阳(自转中心)的距离分别为 58、108、150 和 228 像素。
  3. 火星每旋转 1 度,地球、金星和水星将分别移动 2、3 和 7.5 度。

下图显示了地球绕太阳公转四分之一圈时的模拟状态。请注意,水星几乎完成了它的第一次自转。

我得到的输出是:

这是我的代码:

import turtle
import math


s = turtle.Screen()
t = turtle.Turtle()

def jump(t,x,y):
    'makes turtle t jump to coordinates (x,y)'
    t.penup()
    t.goto(x,y)
    t.pendown()

def planets(t):

    #mercury
    jump(t,0,-58)
    t.circle(58,337.5)

    #venus
    jump(t,0,-108)
    t.circle(108,135)

#   earth
    jump(t,0,-150)
    t.circle(150,90)

#   mars
    jump(t,0,-228)
    t.circle(228,45)



planets(t)
turtle.done()

所以基本上,方向正在改变。如何获得所需的输出?如何阻止extent 参数改变圆的方向?

【问题讨论】:

    标签: python python-3.x turtle-graphics python-turtle


    【解决方案1】:

    问题不在于circle()extent 参数,而是当乌龟完成上一个轨道时,它以任意方向开始每个新轨道。在绘制每个轨道之前,您需要将海龟设置为已知方向:

    from turtle import Screen, Turtle
    
    def jump(t, x, y):
        ''' makes turtle t jump to coordinates (x, y) '''
    
        t.penup()
        t.goto(x, y)
        t.pendown()
    
    def planets(t):
    
        # mercury
        t.setheading(0)
        jump(t, 0, -58)
        t.circle(58, 337.5)
        t.stamp()
    
        # venus
        t.setheading(0)
        jump(t, 0, -108)
        t.circle(108, 135)
        t.stamp()
    
        # earth
        t.setheading(0)
        jump(t, 0, -150)
        t.circle(150, 90)
        t.stamp()
    
        # mars
        t.setheading(0)
        jump(t, 0, -228)
        t.circle(228, 45)
        t.stamp()
    
    turtle = Turtle()
    turtle.shape('circle')
    turtle.shapesize(0.5)
    turtle.hideturtle()
    
    planets(turtle)
    
    screen = Screen()
    screen.exitonclick()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      相关资源
      最近更新 更多