
Lesson 9 - Events and Objects
In this lesson we look at mouse and keyboard events and how they can interact with objects.
Objectives
By the end of the lesson the student should be able to:
- Understand how to bind an event to an object.
- Produce motion or other effects in response to a keyboard or mouse event.
- Write functions to respond to the events.
- How to control the visibility of objects.
- How to use a global variable to change an object.
- Use the scene.range property to preset the view of the object.
Timings
Teaching: 40 min
Exercises: 20 min
The program
This program shows how to select an object with the mouse and control the object using the keyboard.
To fully use the example and see the effect of pressing the arrow keys, run the program from your own editor,
as the webpage here overides key presses in the example window.
scene.range=20 # make the scene 20 units across so the ball does not fill the graphics window.
center=vec(0,0,0)
aball = sphere(pos=center,color=color.cyan) # All shapes are available globally.
dv = 0.2
def get_key_event(ev):
global dv # state we want to use the global variable 'dv' from outside this function
k = keysdown() # a list of keys that are down
if 'left' in k: aball.pos.x -= dv
if 'right' in k: aball.pos.x += dv
if 'down' in k: aball.pos.y -= dv
if 'up' in k: aball.pos.y += dv
def get_mouse_event(ev):
obj_under_mouse= None
obj_under_mouse=scene.mouse.pick # This statement places the object found at the mouse coordinates
# into the variable 'obj_under_mouse'
if obj_under_mouse!=None:
if(obj_under_mouse==aball): # If the object picked is the 'aball' object then colour it red
aball.color=color.red
else:
aball.color=color.cyan
scene.bind('keydown',get_key_event)
scene.bind('mousedown',get_mouse_event)
scene.waitfor('mousedown mouseup keydown keyup ')
Things to note
A global variable is used within the function to enable the function to use their values.
The 'pick' method returns the object under the mouse when clicked if an object is there.
The scene.bind() statement asks to fire the specified event when a mouse is clicked or a key is pressed.
The last line of the program asks the scene to wait for the specified events.
Exercise 1
Copy the program above and do the following:
1. Modify the program to make the cyan sphere change to a cube when the user presses 'shift' and the letter 'c' together.
2. Make it change back to a sphere when the user presses 'shift' and the letter 'b' together.
Hint: Create the red cube and make it invisible using the attribute "visible=False". Use a global variable active_object to hold the box or ball.
Note: An object has to be visible before one can assign an attribute to it, e.g. position.
scene.range=20 # make the scene 20 units across so the ball does not fill the graphics window.Solution to Exercise 1
center=vec(0,0,0)
aball = sphere(pos=center,color=color.cyan)
abox=box(pos=center,color=color.red, size=vector(2,2,2), visible=False) # add a box and make invisible
active_object=aball # seet the active object to the ball
dv = 0.2
def get_key_event(ev):
global active_object,center,dv #use global variables above
k = keysdown() # a list of keys that are down
if 'left' in k: active_object.pos.x -= dv
if 'right' in k: active_object.pos.x += dv
if 'down' in k: active_object.pos.y -= dv
if 'up' in k: active_object.pos.y += dv
if 'shift' in k and 'C' in k : # box solution
center=aball.pos
active_object=abox
abox.visible=True
active_object.pos=center
aball.visible=False
if 'shift' in k and 'B' in k : # ball solution
center=abox.pos
active_object=aball
aball.visible=True
active_object.pos=center
abox.visible=False
def get_mouse_event(ev):
obj_under_mouse= None
obj_under_mouse=scene.mouse.pick
if obj_under_mouse!=None:
if(obj_under_mouse==active_object):
active_object.color=color.red
else:
active_object.color=color.cyan
scene.bind('keydown',get_key_event)
scene.bind('mousedown',get_mouse_event)
scene.waitfor('mousedown mouseup keydown keyup ')
Conclusion
You should now be able to:
Understand how to bind an event to an object.
Produce motion or other effects in response to a keyboard or mouse event.
Write functions to respond to the events.
How to control the visibility of objects.
How to use a global variable to change an object.
Use the scene.range property to preset the view of the object.