简体   繁体   中英

Visual module in python assign objects

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM