High School level (years 9-10 in Australia, ages 14-16) introduction to programming course, based on the language Processing, includes class material, exercises, examples, and tests. Course ran for 2 terms in 2014. Feel free to use as is, borrow ideas, etc. 5th class.
2. Exercises Revision
• Textbook
– Page 42ff
– Example 4.6 to 4.13
– Then go back to fruit bowl problem
– Save your fruit bowl problem to work on later
– Monday Robot 2 Page 49
• How far did you get?
– Who managed to get fruit bowl working?
– If you did keep working on it next time if you have
time – try “surreal” version
3.
4.
5. Today Chapter 5 textbook – mouse
• Page 51ff
// declare global variables
int anIntToRuleTheWorld = 1; // can be used anywhere
void setup() {
// do stuff once only
frameRate(60); // default frameRate
println(anIntToRuleTheWorld++);
}
void draw() {
// repeat forever
Println(“frame number “ + frameCount); // frameCount is global variable, incremented each time
draw runs
println(anIntToRuleTheWorld++);
}
6. mouse
• mouseX and mouseY are global variables for
current mouse position
• background(value) clears screen
• pmouseX and pmouseY – mouseX and
mouseY in the previous frame
• dist(x1,y1,x2,y2) – length of line from (x1,y1)
to (x2,y2)
dist(mouseX,mouseY,pmouseX,pmouseY) is speed
of the mouse
7. Mouse button
• mousePressed global variable
– Boolean so can only be true or false
– So don’t need the “==“
• if (mousePressed == true)
println(“button!”)
else println(“no button”);
• if (mousePressed)
println(“button!”)
else println(“no button”);
8. Mouse button – bugs
“=“ (assignment) is legal but...
• if (mousePressed = true)
println(“button!”)
else println(“no button”);
• if (mousePressed = false)
println(“button!”)
else println(“no button”);
9. Mouse buttons
if (mousePressed)
{
If (mouseButton == LEFT)
println(“Left button”);
else if (mouseButton == CENTER)
println(“Center button”);
else if (mouseButton == RIGHT)
println(“Right button”);
}
else println(“NO button”);
10. Mouse buttons – same as
if (mousePressed)
If (mouseButton == LEFT)
println(“Left button”);
else if (mouseButton == CENTER)
println(“Center button”);
else if (mouseButton == RIGHT)
println(“Right button”);
else println(“NO button”);
11. Mouse buttons – same as
if (mousePressed)
If (mouseButton == LEFT)
println(“Left button”);
else if (mouseButton == CENTER)
println(“Center button”);
else if (mouseButton == RIGHT)
println(“Right button”);
else println(“NO button”);
12. Mouse buttons – same as
if (mousePressed && mouseButton == LEFT)
println("Left button");
else if (mousePressed && mouseButton == CENTER)
println("Center button");
else if (mousePressed && mouseButton == RIGHT)
println("Right button");
else println(“NO button”);
13. Mouse buttons – same as???
Not sure – seems to be lag
if (mouseButton == LEFT)
println("Left button");
else if (mouseButton == CENTER)
println("Center button");
else if (mouseButton == RIGHT)
println("Right button");
else println(“NO button”);
14. switch statement – for lots of values
switch (mouseButton) // a variable
{
case LEFT: println("Left Button"); break;
case CENTER: println(“Center Button”); break;
case RIGHT: println("Right Button"); break;
default: println("No button"); break;
// default matches if none of the other case values match
(it’s a “catch all”)
}
// case X is true if (mouseButton == X)
// break exits the “block” (curly brackets) to here
// don’t need the last break as exits automatically
15. Keys and text
• keyPressed – global boolean variable, true or
false if key pressed or not
• key – global char variable remembers the last key
typed (until a new key is hit)
– Char is a single character ‘a’, ‘b’, ‘c’, ‘1’, ‘!’ etc
• “a” and a are not chars
– Some char values are not printable
• Need to check if CODED as follows
• if (key == CODED)
if (keyCode == LEFT) // check if left arrow pressed etc
• textSize(n) – sets font size (in pixels)
• text(char, x, y) – draws char at (x,y) location
16. Exercises
• Examples 5-1 etc
– Don’t worry about easing and map examples
– Leave Example 5-16 and 5-17 (boundaries) until
next time