单击按钮时程序不会暂停

时间:2018-09-07 19:23:53

标签: python simulation vpython

我希望能够停止模拟。我正好复制粘贴了GlowScript文档的代码。但是当按下暂停按钮时,它根本不会发生任何事情。唯一的是,它会更改按钮中以文本形式写入的内容。我也有一个重置按钮,可以正常工作。暂停/运行是在代码的开头。我还提供了其他代码详细信息,因为我认为它与这些部分有关。因为我只是从Glowscript文档中复制了它(运行/暂停)

from vpython import *


running = True

def Run(b):
    global running
    running = not running
    if running: b.text = "Pause"
    else: b.text = "Run"

button(text="Pause", pos=scene.title_anchor, bind=Run)

def Reset(c):
    global t, e

    t=0
    e.pos = vec(ae,0,0)
    e.velocity = vec(0,0,-25000)

button(text ="Reset", pos=scene.title_anchor, bind=Reset)


#other constants for calc. ...

framerate = 100



#sun
s = sphere(pos = vec(0,0,0), radius = s_rad1, color=color.orange, make_trail = True ) 
s.mass = 2e30   
s.velocity = vec(0,0,0)


#earth
e = sphere (pos = vec(ae, 0, 0), radius = e_rad, make_trail = True, color = color.blue)

e.mass = 5.9e24    
e.velocity = vec(0,0,-25000)#bewegt sich mit 30000 ms




dt = 50000
time = 0.1



while (True):

    rate(framerate)  


    g_force = g * s.mass * e.mass * (s.pos - e.pos).norm()  / (s.pos - e.pos).mag2

    g_forceS = -g_force

    s.velocity = s.velocity + ( g_forceS / s.mass) * dt #Richtungsänderung

    s.pos += s.velocity * dt 



    e.velocity = e.velocity + ( g_force / e.mass) * dt #Richtungsänderung


    e.pos += e.velocity * dt 

1 个答案:

答案 0 :(得分:0)

您的“运行”功能更改了全局可验证的“运行中”,但是动画循环不会查看该变量。您需要这样的东西:

为True时:     速率(帧率)     如果正在运行:         g_force = ....         .....