簡體   English   中英

python分配對象中的可視模塊

[英]Visual module in python assign objects

我是python中的Visual模塊中的新手,不是很了解如何為對象分配值。

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]

我只是不明白,通常,當python讀取此代碼時,列表將在for循環后完全完成,但是在導入可視模塊后,這些球體可以顯示在屏幕上,並通過最后一次for循環的每次迭代更新其位置!

或者我的問題還可能鏈接到可視模塊中的“ show()”,“ print”,“ start the animation”語句的內容和位置,以及它是如何工作的? 我該如何使用?

有點像是在for循環中或完成后添加打印狀態。

在此先感謝

首先是第一件事。 您的代碼使用列表串聯將內容添加到列表中。 最好使用列表的.append()方法。 同樣,最后一個循環可以直接在對象上進行迭代,而無需使用索引。 這樣更優雅,更容易理解。

以下偽代碼與您的偽代碼等效,但應用了上述更正:

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

有了這些,我可以解釋視覺是如何工作的。

更改對象后,可視模塊將立即更新屏幕。 動畫是通過這種更改實時完成的,不需要show()start_animation() -它可以start_animation()進行。 您可以在python命令行上運行的示例:

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

那條線創建了一個球體和一個窗口,並且已經在窗口中顯示了該球體!!!

>>> s.x = -100

那條線將x軸上的球體位置更改為-100 更改立即在屏幕上發生。 在這條線運行之后,您會看到球形出現在窗口的左側。

因此,動畫是通過更改對象的值來發生的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM