我有一个文本文件,其中包含行星及其相应的卫星/卫星以及它们的轨道半径和周期,我想用它来创建quickdraw
类似于下面的动画:
文本文件如下:
RootObject: Sun
Object: Sun
Satellites: Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris
Radius: 20890260
Orbital Radius: 0
Object: Miranda
Orbital Radius: 5822550
Radius: 23500
Period: 1.413
Object: Ariel
Orbital Radius: 8595000
Radius: 60000
Period: 2.520379
Object: Umbriel
Orbital Radius: 11983500
Radius: 60000
Period: 4.144177
Object: Titania
Orbital Radius: 19575000
Radius: 75000
Period: 8.7058
Object: Oberon
Orbital Radius: 26235000
Radius: 75000
Period: 13.463
Object: Uranus
Orbital Radius: 453572956
Radius: 2555900
Period: 30799
Satellites: Puck,Miranda,Ariel,Umbriel,Titania,Oberon
Object: Neptune
Orbital Radius: 550000000
Radius: 2476400
Period: 60190
Satellites: Triton
Object: Triton
Orbital Radius: 40000000
Radius: 135300
Period: -5.8
Object: Mercury
Orbital Radius: 38001200
Period: 87.9691
Radius: 243900.7
Object: Venus
Orbital Radius: 57477000
Period: 224.698
Radius: 605100.8
Object: Earth
Orbital Radius: 77098290
Period: 365.256363004
Radius: 637100.0
Satellites: Moon
Object: Moon
Orbital Radius: 18128500
Radius: 173700.10
Period: 27.321582
Object: Mars
Orbital Radius: 106669000
Period: 686.971
Radius: 339600.2
Satellites: Phobos,Deimos
Object: Phobos
Orbital Radius: 3623500.6
Radius: 200000
Period: 0.31891023
Object: Deimos
Orbital Radius: 8346000
Period: 1.26244
Radius: 200000.2
Object: Jupiter
Orbital Radius: 210573600
Period: 4332.59
Radius: 7149200
Satellites: Io,Europa,Ganymede,Callisto
Object: Ceres
Orbital Radius: 130995855
Period: 1679.67
Radius: 48700
Object: Io
Orbital Radius: 22000000
Period: 1.7691377186
Radius: 182100.3
Object: Europa
Orbital Radius: 36486200
Period: 3.551181
Radius: 156000.8
Object: Ganymede
Orbital Radius: 47160000
Period: 7.15455296
Radius: 263400
Object: Callisto
Orbital Radius: 69700000
Period: 16.6890184
Radius: 241000
Object: Saturn
Orbital Radius: 353572956
Period: 10759.22
Radius: 6026800
Satellites: Mimas,Enceladus,Tethys,Dione,Rhea,Titan,Iapetus
Object: Mimas
Orbital Radius: 8433396
Radius: 20600
Period: 0.9
Object: Enceladus
Orbital Radius: 10706000
Radius: 25000
Period: 1.4
Object: Tethys
Orbital Radius: 13706000
Radius: 50000
Period: 1.9
Object: Dione
Orbital Radius: 17106000
Radius: 56000
Period: 2.7
Object: Rhea
Orbital Radius: 24000000
Radius: 75000
Period: 4.5
Object: Titan
Orbital Radius: 50706000
Radius: 257600
Period: 15.945
Object: Iapetus
Radius: 75000
Orbital Radius: 72285891
Period: 79
我已将我的原始代码(更长的可笑)更改为更短的代码:(感谢 sudo_O 提供的所有出色帮助)
file = open("data1.txt","r")
def data(file):
d = {}
for line in file:
if line.strip() != '':
key,value = line.split(":")
if key == 'RootObject':
continue
if key == 'Object':
obj = value.strip()
d[obj]={}
else:
d[obj][key] = value.strip()
return d
planets = data(file)
print(planets)
我最大的问题是我不知道如何编写从文件中导入数据并利用它recursion
来创建类似于所示动画的代码。我一直被告知解决方案非常简单,代码实际上非常短,但我不知道该怎么做,这实际上非常令人沮丧。
这是我绕着一个圆运行的圆的代码,但它似乎太复杂了......
import math
print("circle",400,300,50)
print("flush false")
centreX=400
centreY=300
radius=100
angle=math.pi/2
while True:
print("color 0 0 0")
print("clear")
print("color 255 0 255")
print("circle",centreX,centreY,radius)
childX=math.sin(angle)*radius*1.5+centreX
childY=math.cos(angle)*radius*1.5+centreY
print("circle",childX,childY,radius/2)
print("refresh")
angle+=0.01
规模代码:
scale=250/max([dict[planets]["Orbital Radius"] for x in dict if "Orbital Radius" in dict[planets]])
环绕行星的代码:
print("flush false")
scale=250/max([dict[planets]["Orbital Radius"] for x in dict if "Orbital Radius" in dict[planets]])
t=0
x=400
y=300
while True:
print("refresh")
print("colour 0 0 0")
print("clear")
print("colour 255 255 255")
print("fillcircle",x,y,dict['Sun']['Radius']*scale)
print("text ", "\"Sun\"",x+dict['Sun']['Radius']*scale,y)
r_earth=dict['Earth']['Orbital Radius']*scale;
print("circle",x,y,r_earth)
r_X=x+math.sin(t*2*math.pi/dict['Earth']['Period'])*r_earth
r_Y=y+math.cos(t*2*math.pi/dict['Earth']['Period'])*r_earth
print("fillcircle",r_X,r_Y,dict['Earth']['Radius']*scale)
print("text ", "\"Earth\"",r_X+dict['Earth']['Radius']*scale,r_Y)
t+=0.02
这段代码不能很好地工作,但是作为一个初学者,我在这个问题上处于我的极限......
最后的努力!!这行得通吗?
print("flush false")
scale = 250/max([dict[planets]["Orbital Radius"] for x in dict if "Orbital Radius" in dict[planets]])
t = 0
x = 400
y = 300
print("fillcircle",400,300,dict['Sun']['Radius']*scale)
print("text ", "\"Sun\"",x+dict['Sun']['Radius']*scale,y)
while True:
r_new = dict['Object']['Orbital Radius']*scale
print("circle",x,y,r_new)
r_X = x + math.sin(t*2*math.pi/dict['Object']['Period'])*r_new
r_Y = y + math.cos(t*2*math.pi/dict['Object']['Period'])*r_new
print("fillcircle",r_X,r_Y,dict['Object']['Radius']*scale)
print("text ",Object,r_X+dict['Object']['Radius']*scale,r_Y)
t += 0.02
if planets['Object']['Satellites'] = 0
return
else:
r_sat = dict['Object']['Satellites']['Orbital Radius']*scale
print("circle",x,y,r_sat)
r_satX = x + math.sin(t*2*math.pi/dict['Object']['Satellites']['Period'])*r_sat
r_satY = y + math.cos(t*2*math.pi/dict['Object']['Satellites']['Period'])*r_sat
print("fillcircle",r_satX,r_satY,dict['Object']['Satellites']['Radius']*scale)
print("text ",['Object']['Satellites'],r_satX+dict['Object']['Satellites']['Radius']*scale,r_satY)
t += 0.02