编码新手。我正在制作一个程序,旨在展示一个太阳系,并使用海龟可视化和动画行星。当我不得不拥有多个海龟时,问题就来了,因为我的代码结构方式。我尝试过改变它,例如,在循环和类似的动画中加入行星的值,但到目前为止还没有成功。代码最底部的值是行星的值,到目前为止,我只成功地制作了水星动画。
import turtle
import math
scale = 75000 #sätter skala till 7500 pixlar x,y.
screen = turtle.Screen()
screen.bgcolor("black")
screen.colormode(255)
sun = turtle.Turtle()
sun.setposition(0,0)
sun.color(255, 204, 51)
sun.begin_fill()
sunRadius = 432690 / scale
sunCircumference = 2*math.pi*sunRadius
for i in range(36):
sun.forward(sunCircumference / 36)
sun.right(10)
sun.end_fill()
sun.hideturtle()
planet = turtle.Turtle()
planet.speed(0)
planetScale = 100
orbit=turtle.Turtle()
orbit.color("white")
def drawPlanet(color, distance, radius):
radius = radius*planetScale/scale
circumference = 2 * math.pi * radius
print(circumference)
distFromSun = sunRadius + ((radius + distance) / scale) / 10
orbit.penup()
orbit.setposition(0,0)
orbit.showturtle()
orbit.setheading(0)
orbit.forward(distFromSun)
orbit.setheading(270)
orbitCircumference = 2*math.pi*distFromSun
orbit.pendown()
for i in range(36):
orbit.forward(orbitCircumference/36)
orbit.right(10)
orbit.hideturtle()
def move (planet):
r = color[0]
g = color[1]
b = color[2]
planet.setposition(0,0)
planet.showturtle()
planet.color(r,g,b)
planet.penup()
planet.forward(distFromSun)
planet.pendown()
planet.begin_fill()
for i in range(36):
planet.forward(circumference / 36)
planet.right(10)
planet.end_fill()
planet.hideturtle()
planet.penup()
if __name__ == "__main__":
while True:
planet.clear()
#call function to draw ball
move(planet)
#update screen
screen.update()
#make planet turn, move forward
planet.left(1)
planet.forward(0.5)
#Mercury
drawPlanet([151, 151, 159], 35980000, 1516)
#Venus
drawPlanet([211, 156, 126], 67240000, 3760)
#Tellus
drawPlanet([151, 151, 159], 92950000, 3959)
#Mars
drawPlanet([151, 151, 159], 141600000, 2106)
#Jupiter
drawPlanet([151, 151, 159], 483800000, 43441)
#Saturn
drawPlanet([151, 151, 159], 890000000, 15759)
#Neptune
drawPlanet([151, 151, 159], 2793000000, 15299)发布于 2021-10-30 01:35:22
我已经把你的程序拆开了,然后重新组装成一个基本上可以运行的精简版本。一个关键的区别是每个星球都有一只专门的海龟,而不是试图共享一个。一些问题仍然存在:
while True:迁移到ontimer()-based设计。看看这是否为您提供了完成代码所需的足够功能:
from turtle import Screen, Turtle
from math import pi
SCALE = 75_000
sunRadius = 432_690 / SCALE
planetScale = 100
planets = []
def drawPlanet(color, distance, radius):
radius *= planetScale/SCALE
distFromSun = ((radius + distance) / SCALE) / 10 # what is this number?
orbitCircumference = 2 * pi * distFromSun
orbit.home()
orbit.forward(distFromSun)
orbit.setheading(270)
orbit.pendown()
for parity in range(60):
if parity % 2 == 0: # dotted line
orbit.pendown()
else:
orbit.penup()
orbit.right(6)
orbit.forward(orbitCircumference/60)
orbit.penup()
planet = Turtle()
planet.hideturtle()
planet.penup()
planet.forward(distFromSun)
planet.setheading(270)
planets.append((planet, color, distFromSun, radius))
def move(data):
planet, color, distFromSun, radius = data
planet.clear()
circumference = 2 * pi * distFromSun
# make planet turn, move forward
planet.right(6)
planet.forward(circumference / 60)
planet.dot(radius * 2, color)
screen = Screen()
screen.setup(width=1.0, height=1.0)
screen.bgcolor('black')
screen.colormode(255)
screen.tracer(False)
sun = Turtle()
sun.hideturtle()
sun.dot(sunRadius * 2, (255, 204, 51))
orbit = Turtle()
orbit.hideturtle()
orbit.color('white')
orbit.penup()
# Mercury
drawPlanet([151, 151, 159], 35_980_000, 1516)
# Venus
drawPlanet([211, 156, 126], 67_240_000, 3760)
# Tellus
drawPlanet([151, 151, 159], 92_950_000, 3959)
# Mars
drawPlanet([151, 151, 159], 141_600_000, 2106)
# Jupiter
drawPlanet([151, 151, 159], 483_800_000, 43441)
# Saturn
drawPlanet([151, 151, 159], 890_000_000, 15759)
# Neptune
drawPlanet([151, 151, 159], 2_793_000_000, 15299)
if __name__ == '__main__':
while True:
for data in planets:
move(data)
screen.update()
screen.mainloop()

https://stackoverflow.com/questions/69753607
复制相似问题