循环改变弹簧长度在我运行时不会改变任何东西



我正在从事的项目应该模拟一个脉冲,沿着一组由弹簧连接的球体向下移动。 随着脉冲沿着链条向下移动,我试图减少弹簧长度,但是当我运行它时,没有任何反应。

这是我的代码:

    from visual import *
one = sphere(pos=(-10,0,0), radius = 0.5, color = color.red)
two = sphere(pos=(-8,0,0), radius = 0.5, color = color.orange)
three = sphere(pos=(-6,0,0), radius = 0.5, color = color.yellow)
four = sphere(pos=(-4,0,0), radius = 0.5, color = color.green)
five = sphere(pos=(-2,0,0), radius = 0.5, color = color.blue)
six = sphere(pos=(0,0,0), radius = 0.5, color = color.cyan)
seven = sphere(pos=(2,0,0), radius = 0.5, color = color.magenta)
eight = sphere(pos=(4,0,0), radius = 0.5, color = color.white)
nine = sphere(pos=(6,0,0), radius = 0.5, color = color.red)
ten = sphere(pos=(8,0,0), radius = 0.5, color = color.orange)
spring1 = helix(pos = (-10, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.red)
spring2 = helix(pos = (-8, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.orange)
spring3 = helix(pos = (-6, 0, 0), length = 2, radius = 0.3,
   thickness = 0.05, color = color.yellow)
spring4 = helix(pos = (-4, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.green)
spring5 = helix(pos = (-2, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.blue)
spring6 = helix(pos = (0, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.cyan)
spring7 = helix(pos = (2, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.magenta)
spring8 = helix(pos = (4, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.white)
spring9 = helix(pos = (6, 0, 0), length = 2.0, radius = 0.3,
   thickness = 0.05, color = color.red)
masses = [one, two, three, four, five, six, seven, eight, nine, ten]
springs = [spring1, spring2, spring3, spring4, spring5, spring6, spring7,
           spring8, spring9]
while True:
    n=0
    deltax=.2
    while n < 10:
        rate(30)
        masses[n].pos.x = masses[n].pos.x + deltax
        if n < 9:
            springs[n].pos = masses[n].pos
            springs[n].axis = masses[n+1].pos-masses[n].pos
        n=n+1
    n = n-1
    while n >= 0:
        rate(30)
        masses[n].pos.x = masses[n].pos.x - deltax
        if n < 0:
            springs[n-1].pos = masses[n-1].pos - deltax
            springs[n-1].axis = masses[n].pos-masses[n-1].pos
        n = n-1

while True:
    m=0
    deltat=.2
    while m<9:
        rate(30)
        springs[m].length = springs[m].length - deltat
        springs[m].length = springs[m].length + deltat
        m=m+1
    m=m-1
    while n>=0:
        rate(30)
        springs[m].length = springs[m].length - deltat
        springs[m].length = springs[m].length + deltat
        m=m-1

使质量具有不透明度 = 0.3,并将第一速率语句替换为 scene.waitfor('click'(。你会看到,当你向右移动第 n 个质量时,弹簧确实缩短了,当第 (n+1(个质量移动时,其右侧的弹簧缩短,在第 n 个弹簧和第 (n+1( 个弹簧之间留下一个间隙。

顺便说一句,我不明白哪里有第二个"虽然是真的"。它永远不会被达到。

我会宣传一个更好的提出 VPython 问题的地方是在 VPython 论坛

https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users

相关内容

  • 没有找到相关文章

最新更新