阻止 Turtle 中的 `extent` 参数改变圆的方向

Stop the `extent` argument in Turtle from changing direction of circle

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

Using Turtle graphics, implement function planets(), which will simulate the planetary motion of Mercury, Venus, Earth, and Mars during one rotation of planet Mars. You can assume that:

  1. At the beginning of the simulation, all planets are lined up (say along the negative y-axis).
  2. The distances of Mercury, Venus, Earth, and Mars from the Sun (the center of rotation) are 58, 108, 150, and 228 pixels.
  3. For every 1 degree circular motion of Mars, Earth, Venus, and Mercury will move 2, 3, and 7.5 degrees, respectively.

The figure below shows the state of the simulation when Earth is about a quarter of the way around the Sun. Note that Mercury has almost completed its first rotation.

我得到的输出是:

这是我的代码:

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 参数改变圆的方向?

问题不在于 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()