Lecture 07
While{} blocK:
int a = 0;
int b = 1;
int sum = a+b;
while (sum < 10000) { // some boolean test!
println(sum);
a = b;
b = sum;
sum = a+b;
}
Trigger an event periodically:
int poopPeriod = 3000;
int lastPoopTime = 0;
void setup(){
}
void draw(){
if ((millis() - lastPoopTime) >= poopPeriod) {
poop();
}
}
void poop(){
// only printed output for now; visuals are up to you
println("At " + millis() + ", I pooped");
lastPoopTime = millis();
}
Function that returns a value:
void setup(){
int mySum = addThreeInts (3,8,11);
float f = cos(4.0);
println("My Sum = " + mySum);
}
int addThreeInts (int a, int b, int c){
int sum = a + b + c;
return sum;
}
Interpolation (Blurred Integrator / “Zeno’s Interpolation”) to the Mouse
float px = 0;
float py = 0;
void setup(){
size(400,400);
}
void draw(){
background(127);
float A = 0.95;
float B = 1.0-A;
px = A*px + B*mouseX;
py = A*py + B*mouseY;
ellipse (px,py, 30,30);
}
Damping a variable down to zero / Triggering an event sequence
float py, px;
float vy, vx;
void setup(){
size(400,400);
initiateMovementSequence();
}
void draw(){
float grayColor = map(px, 0,width, 0,255);
background(grayColor);
py += vy;
px += vx;
vy *= 0.96;
vx *= 0.96;
ellipse(px,py, 30,30);
}
void mousePressed(){
// clicking resets the action
initiateMovementSequence();
}
void initiateMovementSequence(){
px = width/2;
py = height/2;
vx = random(-5,5);
vy = random(-5,5);
}
Simple Pong
float px, py;
float vx, vy;
float diam = 80;
void setup(){
size(400,350);
px = width/2;
py = height/2;
vx = random(-10,10);
vy = random(-10,10);
}
void draw(){
background(127);
px += vx;
py += vy;
if ((px < (diam/2)) || (px > (width - diam/2))){
vx = -vx;
}
if ((py < (diam/2)) || (py > (height - diam/2))){
vy = -vy;
}
ellipse(px,py, 80,80);
}
void mousePressed(){
// clicking the mouse randomizes direction
vx = random(-10,10);
vy = random(-10,10);
}
Libraries:
Speech synthesizer…
Audio input…