0

I am a newb in Visual module in python, not really understand how does it assign a value to an objects. say

from visual import *
stars=[]
galaxies=[]    
for i in range(10):
   stars+=[sphere('pos,radius,color.....')]
for j in range(20):
   galaxies+=[sphere('pos,radius,color......')]
for k in range(30):
   stars[k].pos=position[k] 
   galaxies[k].pos=G_position[k]

i just can not understand, normally, when python read this code, the list would be fully finished after the for loop, but after import visual module, those spheres can show up on screen and update their positions by each iteration of the last for loop!...

or my question may also link to what and where the "show()","print" "start the animation" statement in the visual module and how does it work? how may I use it?

kind of like if I add print state with in the for loop or after it finished.

Thanks alot in advance

BIBD
  • 15,107
  • 25
  • 85
  • 137
user211037
  • 1,725
  • 5
  • 16
  • 11

1 Answers1

1

First things first. Your code uses list concatenation to add stuff to the list. It is better to use the .append() method of lists. Also, the last loop could iterate directly on the objects instead of using an index. It is more elegant and easy to understand this way.

The pseudo-code below is equivalent to yours, but with the above corrections applied:

from visual import *
stars = []
galaxies = []    
for i in  range(10):
   stars.append(sphere(...))
for j in range(20):
   galaxies.append(sphere(...))
for star, galaxy, starpos, galaxypos in zip(stars, galaxies, 
                                            position, G_position):
   star.pos = starpos
   galaxy.pos = galaxypos

With that out of the way, I can explain how visual works.

Visual module updates the screen as soon as the object is changed. The animation is done by that alteration, in realtime, there's no need for a show() or start_animation() - it happens as it goes. An example you can run on python command line:

>>> from visual import sphere
>>> s = sphere()

That line creates a sphere, and a window, and shows the sphere in the window already!!!

>>> s.x = -100

That line changes the sphere position on x axis to -100. The change happens immediatelly on the screen. Just after this line runs, you see the sphere appear to the left of the window.

So the animation happens by changing the values of the objects.

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • @nosklo: thank you very much for the answer, its great! helps alot, but one more question,if that so, how can I pack "show" with in a function? when I call the function, it shows. – user211037 Jan 05 '10 at 18:01
  • Just do *everything* inside the function. – nosklo Jan 05 '10 at 21:48