首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击按钮时程序不会暂停

单击按钮时程序不会暂停
EN

Stack Overflow用户
提问于 2018-09-08 03:23:53
回答 1查看 48关注 0票数 1

我想通过能够停止我的模拟。我准确地复制并粘贴了GlowScript文档的代码。但当按下暂停按钮时,它根本不会发生任何事情。唯一的问题是,它会将按钮中写入的内容更改为文本。我也有一个重置按钮,可以很好地工作。Pause/Run在代码的开头。我还提供了其他代码细节,因为我认为它必须对这些部分进行smth。因为我刚从Glowscript文档中复制了它(运行/暂停)

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

发布于 2018-11-16 02:08:09

你的Run函数改变了全局可验证的"running",但是你的动画循环并不关注这个变量。你需要这样的东西:

while True: rate(帧率)如果正在运行: g_force = .....

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52228530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档