实例已创建但未显示在动画中

时间:2018-11-27 17:08:56

标签: python-3.x class animation methods

我正在尝试创建一个探针的实例,该实例从行星的位置发射,这是因为该行星与x轴的角度与被发送到该行星的行星的角度之差为44度。 当我在animations方法中使用I语句创建Probe的实例时,探针是在后台创建的,并且受代码其他部分的影响,但会显示在动画中。

import numpy , math, scipy as sp
import matplotlib.pyplot as plt
from matplotlib import animation

G = 6.67428e-11
AU = (149.6e6 * 1000)
timestep = 24 * 3600
class Planet(object):
    def __init__(self):
    self.name = None
    self.mass = 0.0
    self.x = 0.0
    self.y = 0.0
    self.vx = 0.0
    self.vy = 0.0
    self.v = 0.0
    self.size = 0.0
    self.color = None
def comp_force(self,others):
    self.tfx = self.tfy = 0.0
    for other in others:
        dx = -self.x + other.x
        dy = -self.y + other.y
        r = math.sqrt(dx**2 + dy**2)
        angle = math.atan2(dy, dx)
        f = G*self.mass*other.mass/(r**2)
        fx = f*math.cos(angle)
        fy = f*math.sin(angle)
        self.tfx += fx
        self.tfy += fy
def change(self):
    self.vx += self.tfx*timestep/self.mass
    self.vy += self.tfy*timestep/self.mass
    self.v = math.sqrt(self.vx**2 + self.vy**2)
    self.x += self.vx*timestep
    self.y += self.vy*timestep
    return self.x, self.y

def satellite(self, n):
    i = 0
    Sat = []
    while i < n:
       i += 1
       sat = Planet()
       sat.name = "Satellite"
       sat.mass = 7.34767309 * 10**22
       sat.color = "gray"
       sat.x = -.00257*AU + self.x
       sat.size = 2
       sat.vy = -28.999*1000
       sat.vx =  0
       Sat.append(sat)
    return Sat

def launch(bodies):
    Satellites = bodies[1].satellite(2)
    Probe = Satellites[1]
    Probe.mass = 500
    Probe.name = "Probe"
    Probe.x = bodies[1].x
    Probe.y = bodies[1].y - .01*AU
    Probe.vx = -32.711*1000
    Probe.size = 3
    bodies.append(Probe)

def ani_traj(i, bodies, lines):
     if -math.degrees(math.atan2(bodies[2].y,bodies[2].x))+math.degrees(math.atan2(bodies[1].y, bodies[1].x)) == 44.41030336898693:
        launch(bodies)
        print("LAUNCH")
        lobj = ax.plot([], [], lw=2, color=bodies[len(bodies)-1].color)[0]
        lines.append(lobj)

    for ind, body in enumerate(bodies):
        body.comp_force(numpy.delete(bodies, ind))
        print(body.name)
    for body in bodies:
        body.change()
    for i in range(len(bodies)):
        lines[i].set_data(bodies[i].x / AU, bodies[i].y / AU)
        print("\n", [bodies[len(bodies)-1].x/AU,bodies[len(bodies)-1].y/AU])
        print(len(lines))
        return lines
#Sun
Sun = Planet()
Sun.name = "Sun"
Sun.mass = 1.98892 * 10 ** 30
Sun.size = 50
Sun.color = "Orange"

#Earth
Earth = Planet()
Earth.name = "Earth"
Earth.mass = 5.9742 * 10 ** 24
Earth.x = -1 * AU
Earth.vy = -29.783 * 1000
Earth.size = 5
Earth.color = "Blue"

#Moon
#Satellites = Earth.satellite(1)
#Moon = Satellites[0]

#Mars
Mars = Planet()
Mars.name = "Mars"
Mars.mass = 6.39 * 10 ** 23
Mars.x = -1.524 * AU
Mars.vy = -24 * 1000
Mars.size = 5
Mars.color = "Red"

obj = [Sun,Earth,Mars]
traj = plt.figure("trajectory")
ax = plt.axes(xlim = (-2,2), ylim = (-2,2))
line, = ax.plot([],[],'o')
lines = []

#print(len(lines))
for index in range(len(obj)):
    lobj = ax.plot([],[],lw = 2,color = obj[index].color)[0]
    #print(lobj)
    lines.append(lobj)

#print(len(obj))
for l in range(len(obj)):
    lines[l], = ax.plot(obj[l].x/AU, obj[l].y/AU, marker = 'o', color =     obj[l].color, ms = obj[l].size)

trajectory = animation.FuncAnimation(traj, ani_traj, numpy.arange(0,500), fargs=[obj, lines], interval=20, blit=True)
plt.grid()
plt.show()

很抱歉,如果代码看起来很凌乱,我在过去的3天里一直试图找到一种解决问题的方法,并且很多print语句都是因为这个原因(大多数不是必需的)。

0 个答案:

没有答案