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
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:
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()
orstart_animation()
- it happens as it goes. An example you can run on python command line:That line creates a sphere, and a window, and shows the sphere in the window already!!!
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.