Beruflich Dokumente
Kultur Dokumente
Defining a function
2
Recap
Calling function & passing parameters
100 150
3
Global variables vs Local variables
4
Preparation
5
Hands-on Exercise: Error Fixing
6
Exercise Part (a)
Write a program to
Draw a toy image if the mouse is clicked
Clear the window if letter R is pressed (case insensitive)
In your program, write the following function to draw the toy
image
7
Hints
w
(?,?)
h
(x, y)
8
Exercise Part (b)
Modify the drawToy function you have done in part(a) to
allow selection of the toy image
10
Exercise Part (c)
Modify the program you have done in part(b) to draw the
toy images in the following order
toy01 toy02 toy03 toy04 toy05 toy01 toy02 toy03
11
Hints
12
Exercise Part (d)
Modify the program you have done in part(c) to draw a
toy image every second automatically at a random
position
After a After a
second second
13
Hints
14
Lab Ex. Submission
"lab5_12345678.zip"
lab5_12345678 lab5_12345678
15
More about User Controls
Supplementary
MOUSE CONTROLS
Recap: mouseX, mouseY, and mousePressed()
mouseButton
17
Recap:
mouseX & mouseY
mouseX and mouseY are
system variables storing the
mouse cursor's horizontal and
vertical position
Example usage:
displaying an image at
(mouseX, mouseY) instead of
at fixed position can make
the image follow the mouse
See robotFollowMouse in
userControl folder
18
Recap: mousePressed()
19
Recap: mousePressed()
Example: robotAtMousePress
this program displays only the
background when the program
starts, and displays a robot at the
mouses position when you click
the mouse
20
Clearing the robots with mouse press
Lets clear the robots with RIGHT click by checking another
system variable mouseButton
System Variable Information provided
mouseButton Which button is pressed? LEFT, RIGHT, or CENTER?
21
Lets try mouseButton
Modify the mousePressed() function in the source program
robotAtMousePress so as to clear the robots with RIGHT click
22
KEYBOARD CONTROLS
Recap: keyPressed()
23
Recap: keyPressed()
24
Recap: keyPressed()
Example: robotClearByKey
clear the robots by ANY
key press
25
Which key is pressed?
26
Lets try keyCode
Open the source program robotMove
This program moves the robot according to the direction (UP /
DOWN / LEFT / RIGHT) stored in the direction variable
27
Lets try keyCode
28
How does the program work?
29
How does the program work?
30
Lets try key
Lets use the i and d keys to increase and decrease the speed
of the robot
Where to add this part
in the program?
31
How does the program work?
32
How does the program work?
33
How does the program work?
35
More about Functions
Supplementary
Exercise
37
Hints
38
Hints
39
References