简体   繁体   English

python分配对象中的可视模块

[英]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. 我是python中的Visual模块中的新手,不是很了解如何为对象分配值。 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!... 我只是不明白,通常,当python读取此代码时,列表将在for循环后完全完成,但是在导入可视模块后,这些球体可以显示在屏幕上,并通过最后一次for循环的每次迭代更新其位置!

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? 或者我的问题还可能链接到可视模块中的“ show()”,“ print”,“ start the animation”语句的内容和位置,以及它是如何工作的? how may I use it? 我该如何使用?

kind of like if I add print state with in the for loop or after it finished. 有点像是在for循环中或完成后添加打印状态。

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. 最好使用列表的.append()方法。 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. 动画是通过这种更改实时完成的,不需要show()start_animation() -它可以start_animation()进行。 An example you can run on python command line: 您可以在python命令行上运行的示例:

>>> 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 . 那条线将x轴上的球体位置更改为-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. 因此,动画是通过更改对象的值来发生的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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