Introduction to programming - class 5

16
Introduction to Programming Class 5 Paul Brebner

Transcript of Introduction to programming - class 5

Introduction to ProgrammingClass 5

Paul Brebner

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

Today Chapter 5 textbook – mouse

• Page 51ff// declare global variablesint anIntToRuleTheWorld = 1; // can be used anywhere

void setup() {// do stuff once onlyframeRate(60); // default frameRateprintln(anIntToRuleTheWorld++);

}void draw() {

// repeat foreverPrintln(“frame number “ + frameCount); // frameCount is global variable, incremented each time

draw runsprintln(anIntToRuleTheWorld++);

}

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

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”);

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”);

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”);

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”);

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”);

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”);

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”);

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

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

Exercises

• Examples 5-1 etc

– Don’t worry about easing and map examples

– Leave Example 5-16 and 5-17 (boundaries) until next time