2017-05-22 6 views
0

Le projet sur lequel je travaille est supposé modéliser une impulsion descendant un ensemble de sphères reliées par des ressorts. J'essaie de diminuer la longueur du ressort lorsque le pouls descend dans la chaîne, mais quand je le lance, rien ne se passe.La boucle pour changer les longueurs de ressort ne change rien quand je l'exécute

Voici mon code:

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 

Répondre

0

Faire les masses ont opacité = 0,3 et remplacer la première déclaration de taux avec scene.waitfor (clic). Vous verrez que lorsque vous déplacez la neuvième masse vers la droite, le printemps se raccourcit, et quand la (n + 1) ème masse est déplacée, le ressort à sa droite se raccourcit, laissant un espace entre le nième printemps et le (n + 1) ème printemps. Incidemment, je ne comprends pas où il y a une seconde "while True". Cela ne sera jamais atteint.

Je prévienne que un meilleur endroit pour poser des questions VPython est dans le forum VPython à

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