我想通过能够停止我的模拟。我准确地复制并粘贴了GlowScript文档的代码。但当按下暂停按钮时,它根本不会发生任何事情。唯一的问题是,它会将按钮中写入的内容更改为文本。我也有一个重置按钮,可以很好地工作。Pause/Run在代码的开头。我还提供了其他代码细节,因为我认为它必须对这些部分进行smth。因为我刚从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 发布于 2018-11-16 02:08:09
你的Run函数改变了全局可验证的"running",但是你的动画循环并不关注这个变量。你需要这样的东西:
while True: rate(帧率)如果正在运行: g_force = .....
https://stackoverflow.com/questions/52228530
复制相似问题