如何在python中进行可视化移动或删除

时间:2015-08-28 14:22:52

标签: python python-2.7 vpython

我不知道如何进行视觉移动或删除。要做一个我就做:

from visual import *
sphere (color=color.blue,pos=(0,0,0))

但这只会产生sphere()。如何让它移动或消失?

1 个答案:

答案 0 :(得分:0)

If you are aiming for a single move, then you can just change the sphere's attributespos (or x, y and z). To delete the sphere you can just make it invisible by setting its visible attribute to False and delete it.

from visual import *

s = sphere(color=color.blue)  # 'pos=(0,0,0)' is the default
# Move the sphere to x=1, y=0, z=0
s.pos = (1, 0, 0)

# Now remove the sphere
s.visible = False
del s

Obviously when executing this code, you will see nothing since you deleted the sphere. If you want to do it when you click, try looking at VPython documentation for controls. I recommend using wxPython's GUI if you're looking for something a little more intricate.

Also, if you want to make an animation of the moving sphere, you could use a loop:

from visual import *

scene.autoscale = False              # Stop the scene from autoscaling
s = sphere(color=color.blue, x=-10)  # Create the sphere
for n in range(-100,100):
    v.rate(100)                      # Set the loop rate to 100 iterations per second
    s.x = n/10.0                     # Set the x attribute of s to move from -10 to 10

# Now remove the sphere
s.visible = False
del s