2017-05-22 49 views
0

我正在開發的項目應該模擬沿着由彈簧連接的一組球體的脈衝。我試圖在脈衝沿着鏈條向下移動時減小彈簧長度,但是當我運行它時,沒有任何反應。當我運行它時,改變彈簧長度的循環不會改變任何東西

這裏是我的代碼:

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

使廣大羣衆有不透明度= 0.3和替換scene.waitfor第一的利率聲明( '點擊')。你會看到,當你將第n個質量向右移動時,彈簧實際上會縮短,當第(n + 1)個質量移動時,右邊的彈簧縮短,在第n個彈簧和第(n + 1)日春天。

順便說一下,我不明白哪裏有第二個「while True」。它永遠不會被達到。

我會通告一個更好的地方提出VPython問題是在VPython論壇

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

相關問題