# # this file shows how to handle mouse clicks in vpython # directions: clickk on the ball or the cube to change its color # pmb 10/1/02 from visual import * # set up a ball and a cube ball = sphere ( pos=( 5,0,0 ), radius=2, color=color.red ) cube = box ( size = (3,3,3), pos = (-5,0,0), color= color.red ) # a vector of colors; clicks cycle through them colorVec = [ color.red, color.yellow, color.green, color.cyan, color.blue, color.magenta ] # current color of ball and cube ballState = 0 cubeState = 0 while 1: # wait for mouse click if scene.mouse.clicked: # get the click- without this it never gets unclicked click = scene.mouse.getclick() # what got clicked? if scene.mouse.pick == ball: print "ball was clicked" #advance ball color counter ballState = ballState + 1 # if counter too high,reset if (ballState > 5 ): ballState = 0 #change ball color to next ball.color = colorVec[ballState] # same for cube; elif is really else if elif scene.mouse.pick == cube: print "cube was clicked" cubeState = cubeState+1 if ( cubeState > 5 ): cubeState = 0 cube.color = colorVec[cubeState] # nothing was clicked; make a little red ball where the mouse is else: loc = click.pos print "make a new ball at ", loc sphere(pos=loc, color=color.red, radius=0.5 )