SlideShare a Scribd company logo
1 of 94
Learn Creative Coding 
Begin Programming with the 
Processing Language 
W. Michelle Harris 
Rochester Institute of Technology 
http://people.rit.edu/wmhics 
Long Beach Indie Digital Edutainment Conference 8.30.14
http://processing.org
How do you communicate a 
design? 
Imagine you opened your favorite drawing 
app, created a new document, and drew a 
straight line and a circle. 
How would you type out complete 
instructions for re-creating your drawing (no 
illustrations allowed)? 
Take a few minutes to ponder this…
Do they need to know: 
1. Drawing app choice 
2. Dimensions of canvas 
3. Canvas color 
4. Line drawing tool 
5. Line stroke color, 
thickness 
6. Line drag start and 
stop coordinates 
7. Circle drawing tool 
8. Circle stroke color, 
thickness 
9. Circle fill color and 
pattern 
10. Circle bounding box 
drag start and stop 
coordinates 
-or- Circle center 
coordinate and radius 
11. ?
Line + circle in drawing app, 
then in Processing code
Prepare the Canvas 
1. Drawing app choice: new Processing sketch! 
2. Dimensions of canvas 
3. Canvas color
Processing 
environment 
A program written in 
Processing is called a 
sketch. 
Type your sketch in this 
space. Text is colored 
automatically according to 
its purpose. 
Status feedback area 
Console area for text output and too-detailed 
error messages. 
Current cursor line number in this sketch.
A warning before we code: 
Processing is as picky as your 8th grade 
Language Arts teacher 
Order, spelling, capitalization, and punctuation 
matter. Mistake => won’t go at all, or goes wonky. 
point(30, 75); 
// point() parameters: x, y 
 Function name, then parentheses with any 
parameters inside separated by commas. End with a 
semicolon. 
 Each instruction can start and be interspersed with 
spaces, tabs, and even line breaks for readability. 
 Entire lines can be blank for readability, too.
Prepare the Canvas 
/* When you make this new canvas, size it 300 
pixels wide, and 250 pixels tall. */ 
size(300, 250); 
/* Set the canvas background color to white. */ 
background(255); 
size(widthPix, heightPix); // in pixels 
background(grayValue); // 0.0 – 255.0 
// Alternatively, background(red, green, blue);
Prepare settings for the Line 
4. Line drawing tool? There is only one choice ;) 
5. Line stroke color, thickness
Prepare settings for the Line 
/* Set the drawing stroke color to blue */ 
stroke(128, 119, 251); 
/* Set the drawing stroke thickness to 5 pixels */ 
strokeWeight(5); 
stroke(redVal, blueVal, greenVal); //0.0 – 255 
strokeWeight(widthPix);
Draw the Line 
6. Line drag start and stop coordinates
Draw the Line 
/* Start line at coordinates x:135, y:213, and end 
the line at coordinates x:287, y:66. */ 
line(135, 213, 287, 66); 
line(x1, y1, x2, y2); 
Save and Run your sketch.
Prepare settings for the Circle 
7. Circle drawing tool: Choose drawing mode – 
corner-to-corner? Start coordinates & width 
& height (default mode in Processing)? 
8. Circle stroke color, thickness 
9. Circle fill color and pattern
Prepare settings for the Circle 
/* Set the drawing fill color to black */ 
fill(0); 
/* We don’t want any drawing stroke */ 
noStroke(); 
fill(grayValue); //0–255 
fill(red, blue, green); 
noStroke();
Draw the Circle 
10.From circle center drag out width and height
Draw the Circle 
/* Center ellipse at coordinates x:110, y:110, and 
make width 100, height 100. */ 
ellipse(110, 110, 100, 100); 
ellipse(x, y, width, height); 
Save and Run your sketch.
One question you might have 
Sometimes, we gave Processing a single 
grayscale color value, and sometimes we gave 
it three RGB color values. Can both be right?
stroke(): 6 ways 
to color 
stroke(gray); 
stroke(red, green, blue); 
stroke(#RRGGBB); 
stroke(gray, alpha); 
stroke(red, green, blue, alpha); 
stroke(#RRGGBB, alpha); 
Processing knows which version of the function 
you want by the number and type of parameters 
within its parentheses. 
Many of the built-in functions work this way.
Start exploring 
(the Processing Reference too) 
Some primitive 2D shapes: 
line(), triangle(), quad(), rect(), ellipse(), arc() 
Setting shape attributes: 
fill(), noFill() 
stroke(), noStroke() 
strokeWeight(), strokeCap() 
ellipseMode(), rectMode()
Section goals 
1. If I design a composition with simple shapes, 
I can remake it in Processing. 
2. I know how to use different kinds of color 
parameters to make those shapes. 
3. I know how to use the Processing Reference 
to find out details about functions.
Helpful hints 
Pay attention when auto-coloring does NOT 
do what you expected. 
Get familiar with what kinds of errors cause 
which messages and where (make mistakes 
on purpose to check). 
Processing will stop compiling your code at 
the first syntax error it encounters. Watch the 
cursor for where it thinks that error occurred.
Next: Five Things 
1. /* Comments */ and //Comments 
2. Math expressions 
3. Code blocks for animation 
4. Tracking the mouse
1. Which would you rather 
troubleshoot? 
size(300, 250); 
background(255); 
stroke(128, 119, 251); 
strokeWeight(5); 
line(135, 213, 287, 66); 
fill(0); 
noStroke(); 
ellipseMode(CORNERS); 
ellipse(59, 60, 159, 160); 
/* Canvas prep */ 
size(300, 250); 
background(255); // white bg 
/* Line prep */ 
stroke(128, 119, 251); // blue line 
strokeWeight(5); 
/* Make the line */ 
line(135, 213, 287, 66); 
/* Circle prep */ 
fill(0); // black fill 
noStroke(); 
/* Make the circle */ 
ellipse(110, 110, 100, 100);
1. Comments, 2 ways 
//Single line comment 
/* Multiline comments 
can take up all the 
space 
you need as long as 
they end with… */ 
/* Canvas prep */ 
size(300, 250); 
background(255); // white 
/* Line prep */ 
stroke(128, 119, 251); // blue 
strokeWeight(5); 
// Make the line 
line(135, 213, 287, 66);
2. How many little equations 
would you need to figure to 
position all the shapes? 
An alternative: substitute 
a math expression for a 
person-calculated value.
2. Math expressions as function 
parameters. Code before... 
+ plus 
size(400, 300); 
- minus 
*multiplied by 
/ divided by 
%modulo remainder 
// head 
ellipse(200, 150, 100, 100); 
//eyes 
ellipse(180, 150, 20, 30); 
ellipse(220, 150, 20, 30);
2. Math expressions as function 
parameters. Code lazier. 
+ plus 
size(400, 300); 
- minus 
*multiplied by 
/ divided by 
%modulo remainder 
// head 
ellipse(200, 150, 100, 100); 
//eyes 
ellipse(200 - 20, 150, 20, 30); 
ellipse(200 + 20, 150, 20, 30);
2. Math expressions as function 
parameters. Code laziest. 
+ plus 
- minus 
*multiplied by 
/ divided by 
%modulo remainder 
Helpful built-in 
property variables: 
width //canvas width 
height //canvas height 
size(400, 300); 
// head – centered on canvas 
ellipse(width/2, height/2, 
100, 100); 
//eyes 
ellipse((width/2 – 20), height/2, 
20, 30); 
ellipse((width/2 + 20), height/2, 
20, 30);
3. An analogy: running 
http://www.blackgirlsrun.com/partner-up
3. To animate runners 
One-time Setup steps: 
1. Size the canvas 
2. Set the desired frame update rate 
Perpetually loop these Draw steps: 
1. Color the canvas (to erase the prior frame) 
2. Draw scenery elements 
3. Draw next sprite in cycle of runner #1 
4. Draw next sprite in cycle of runner #2 
5. Render pixels from 1-4.
3. Blocks: grouping/herding code 
A block of code is any code enclosed in { }. One useful feature 
of blocks: they can be nested within each other. 
block1 { // a block of code opens 
statement; 
block2 { // a nested block of code opens 
statement; 
statement; 
} // end of block2 
statement; 
} // end of block1 
block3 { // a block of code opens 
statement; 
} // end of block3
3. To animate in Processing 
void setup() { // Initialization code goes here 
size(400, 3oo); 
frameRate(24); // Frames per second. Default max is 60. 
} 
void draw() { // Code to run forever - animate - goes here 
background(255); 
drawScenery(); // this function doesn’t exist 
drawNextRunner1(); // this function doesn’t exist 
drawNextRunner2(); // this function doesn’t exist 
/* At the end of draw(), Processing renders 
the new pixels produced in this block. */ 
}
3. Try this in Processing 
// Pointy sketch 
size(400, 300); 
strokeWeight(5); 
stroke(0); 
background(255); 
point(200, 150); 
// Pointy sketch 
void setup() { 
size(400, 300); 
strokeWeight(5); 
stroke(0); 
} 
void draw() { // animate! 
background(255); 
point(200, 150); 
}
4. Now edit it 
} 
void draw() { // animate! 
background(255); 
point(200, 150); 
} 
} 
void draw() { // animate! 
background(255); 
//point(200, 150); 
point(mouseX, mouseY); 
} 
mouseX and mouseY are built-in property variables of the sketch like 
height and width (in the same way that point() is a built-in function).
4. What’s the difference? 
void setup() { 
size(400, 3oo); 
strokeWeight(5); 
stroke(0); 
} 
void draw() { // animate! 
background(255); 
point(mouseX, 
mouseY); 
} 
void setup() { 
size(400, 3oo); 
strokeWeight(5); 
stroke(0); 
background(255); 
} 
void draw() { // animate! 
// background(255); 
point(mouseX, mouseY); 
}
4. Edit for variation with 
pmouseX and pmouseY 
void draw() { // animate! 
// background(255); 
point(mouseX, mouseY); 
} 
void draw() { // animate! 
// background(255); 
// point(mouseX, 
mouseY); 
line(pmouseX, pmouseY, 
mouseX, mouseY); 
} 
pmouseX and pmouseY are built-in property variables for the position 
of the mouse in the frame draw()n just prior to the current one.
3 Things you’re doing now 
1. draw() {} code blocks for animation 
2. Math expressions with built-in property 
variables 
3. Mouse property variables for interaction
Data types 
In Processing, data are 
categorized by type. 
Three useful data types: 
int whole numbers (-230548, 0, 45) 
float decimal numbers (-4.3, 30984.349) 
String sequence of characters ("Hi, Ebony!")
Making our own Variables 
What if I want to use that hard-calculated 
value again? 
The answer: Let's create our own variable 
and store it there.
Variables 
Container for storing some piece of data. A 
variable is a way for the program to remember. 
Each variable has a name 
 We make up almost any name we want within the 
rules (but it should make sense!) 
 We can use the name to refer to that piece of data 
The value might change during the execution 
of the program. Hence, “variable.” 
 The variable actually points to a specific physical 
memory location in RAM.
Some redundancies here... 
void setup() { 
size(400, 300); 
frameRate(5); 
colorMode(HSB); 
} 
void draw() { 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, mouseX, mouseY, 
dist(pmouseX, pmouseY, mouseX, mouseY), 
dist(pmouseX, pmouseY, mouseX, mouseY)); 
}
float mdistance; // Declare the variable type and name 
void setup() { 
size(400, 300); 
frameRate(5); 
colorMode(HSB); 
} 
void draw() { 
/* Assign mdistance to store the distance between 
the latest and just-previous mouse positions. 
*/ 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, mouseX, mouseY, mdistance, mdistance); 
}
float mdistance; // Declare the variable type and name 
void setup() { 
size(400, Before 300); 
we use a variable, we need to declare 
frameRate(5); 
colorMode(it. 
HSB); 
} 
To declare it, use the keyword for the type of 
void draw() data { 
it will hold: 
/* Assign mdistance to store the distance between 
String instructions; 
int shapeNumber; 
the latest and just-previous mouse positions. 
*/ 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
At this point, the variable exists, but doesn’t 
hold a value yet. We’ve simply informed the 
computer that we intend to use a variable by 
that name and of that data type. 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, mouseX, mouseY, mdistance, mdistance); 
}
float mdistance; // Declare the variable type and name 
void setup() { 
size(400, 300); 
frameRate(5); 
Why is this variable 
colorMode(HSB); 
declared as type float? 
} 
Why not type int? 
void draw() { 
/* Assign mdistance to store the distance between 
the latest and just-previous mouse positions. 
*/ 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, 0, height, mdistance, mdistance); 
}
void draw() { 
/* Assign mdistance to store the distance between 
the latest and just-previous mouse positions. 
*/ 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, 0, height, mdistance, mdistance); 
triangle(width, height, width, 0, mdistance, mdistance); 
} 
Give a variable a value with an assignment 
statement – it uses an equal sign. 
shapeNumber = frameCount - 1; 
The action goes right to left: the value 
(frameCount – 1) replaces any current value in 
the variable shapeNumber.
float mdistance; // Declare the variable type and name 
void setup() Once { 
the variable is declared and a value 
size(400, assigned, 300); 
we can use the variable anywhere 
frameRate(5); 
colorMode(we would HSB); 
have used the value: 
} 
point(shapeNumber, height/2); 
void draw() { 
/* Assign mdistance to store the distance between 
the latest and just-previous mouse positions. 
*/ 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, mouseX, mouseY, mdistance, mdistance); 
}
float mdistance; // Declare the variable type and name 
void setup() { 
size(400, 300); 
frameRate(5); 
colorMode(HSB); 
} 
void draw() { 
/* Assign mdistance to store the distance between 
the latest and just-previous mouse positions. 
*/ 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, mouseX, mouseY, mdistance, mdistance); 
}
Rules for variable names 
Must start with a letter 
 May NOT contain spaces (only one “word”) 
May contain numbers and _underscores_ 
camelCase 
 Start with lower case letter 
 Opt: next words start with upper case (easier to read) 
 Tip: Longer is often better for future 
comprehension 
 selectedCar instead of sc or selCar or selectCar
You can declare and assign an 
initial value in one statement 
// Circles along the diagonal 
int shapeNumber; 
int randomSize; 
void setup() { 
size(200, 200); 
ellipseMode(CENTER); 
shapeNumber = 1; 
} 
void draw() { 
randomSize = int (random(5, 50)); 
ellipse(shapeNumber, 
shapeNumber, randomSize, 
randomSize); 
shapeNumber = shapeNumber + 1; 
} 
// Circles along the diagonal 
int shapeNumber = 1; 
int randomSize; 
void setup() { 
size(200, 200); 
ellipseMode(CENTER); 
} 
void draw() { 
…
Statement 
review 
String instructions; 
void setup() { 
size(200, 500); 
instructions = "Swoosh your mouse"; 
} 
void draw() { // points follow mouse 
point(mouseX, mouseY); 
} 
/* On click, instructions print in console */ 
void mousePressed() { 
println(instructions); 
} 
1. Variable declaration 
2. Code block 
3. Function call 
4. Assignment statement 
5. Comments
and now 6... 
If-else [conditional] blocks 
Instead of always following one path, let the 
computer make a choice based on the 
current outcome of some condition.
Decisions in computerese 
Carbon-based life 
If it is raining hard 
enough to bother me 
outside, maybe I'll put 
on a hat or carry an 
umbrella – depends on 
my mood. 
Silicon-based machines 
rainIntensity = calcIntensity(); 
if (rainIntensity > 3) { 
wearRainCover(); 
} 
wear 
RainCover
Decisions in computerese 
Carbon-based life 
If it is raining hard 
enough to bother me 
outside, maybe I'll put 
on a hat or carry an 
umbrella – depends on 
my mood. 
Otherwise, I'm 
wearing my new 
shades. 
Silicon-based machines 
rainIntensity = calcIntensity(); 
if (rainIntensity > 3) { 
wearRainCover(); 
} else { 
wearSunCover(); 
}
If-else block 
if (rainIntensity > 3) { // do this if true 
wearRainCover(); 
} else { // otherwise do this 
wearSunCover(); 
} 
Expression that can 
evaluate to true or false 
rain sun
If-else block 
if (Boolean expression) { // do this if true 
statement; 
statement; 
} else { // otherwise do this (opt) 
statement; 
statement; 
statement; 
} 
Expression that can 
evaluate to true or false
Start with creeping circle... 
int x = -20; 
void setup() { 
ellipseMode(CENTER); 
} 
void draw() { 
ellipse(x, height/2, 20, 20); 
x = x + 1; 
} 
After the circle 
gets beyond the 
right edge, we will 
never see it 
again. 
Let's change that.
Proceed with cycling circle 
… } 
void draw() { 
ellipse(x, height/2, 20, 20); 
x = x + 1; 
// If circle gets too far… 
if (x > (width + 20)) { //cycle! 
x = -20; 
} 
} 
… } 
void draw() { 
ellipse(x, height/2, 20, 20); 
x = x + 1; 
/* When circle goes off 
right edge, start it back 
at left edge again. 
*/ 
}
Boolean expressions with 
comparison operators 
true 
45 == 45 
10 < 100 
10 > -3 
99 <= 100 
99 >= 98 
100 != 88 
Do block 
false 
45 == 30 
10 < -3 
10 > 88 
99 <= 30 
99 >= 100 
100 != 100 
Skip block 
Reserved word representing a 
logical value. 
Equal? 
Less? 
Greater? 
Less or equal? 
Greater or equal? 
Not equal?
Points, black 
circles, and red 
rects depending 
on conditional 
expressions. 
void setup() { 
ellipseMode(CENTER); 
rectMode(CENTER); 
} 
void draw() { 
if (mouseX < 10) { // far left 
ellipse (width/2, mouseY, 20, 20); 
} 
if (mouseX < width/2) { // left side 
stroke(0); // black stroke 
} else { // right side 
stroke(255,0,0); //red stroke 
} 
point(mouseX, mouseY); 
if (mouseX >= width - 10) { // far right 
rect (width/2, mouseY, 20, 20); 
} 
}
Boolean variables 
boolean isFirstClick = true; 
void setup() { 
ellipseMode(CENTER); 
} 
void draw() { 
if (isFirstClick) { 
ellipse(mouseX, mouseY, 20, 20); 
} else { 
point(mouseX, mouseY); 
} 
} 
void mousePressed() { 
isFirstClick = false; 
strokeWeight(4); 
stroke(150, 0, 150); //purple 
}
More 
complex 
expressions 
Logical operators: 
a || b || c // or 
if any is true 
a && b && c // and 
only if all true 
!a // not 
if not true 
booleanmyTest; 
myTest = (mouseY < 0) || 
(mouseY > height); 
// mousePressed and keyPressed 
// are built-in boolean variables. 
myTest = mousePressed && 
keyPressed; 
// key (built-in char variable) returns 
// most recent keyboard key value 
myTest = !((key == 'q') || (key == 'Q'));
More 
complex 
expressions 
Assume height=200 
mouseY myTest 
-10 true 
0 false 
50 false 
200 false 
201 true 
boolean myTest; 
myTest = (mouseY < 0) || 
(mouseY > height); 
// Would otherTest ever be true? 
boolean otherTest; 
otherTest = (mouseY < 0) && 
(mouseY > height);
More 
complex 
expressions 
boolean myTest; 
// mousePressed and keyPressed 
// are built-in boolean variables. 
myTest = mousePressed && 
keyPressed; 
// What's the difference? 
boolean otherTest; 
otherTest = mousePressed || 
keyPressed; 
keyPressed mousePressed myTest 
true true true 
true false false 
false true false 
false false false
More complex if-else block use 
boolean isFirstClick = true; 
void setup() { 
ellipseMode(CENTER); 
} 
void draw() { 
if (isFirstClick) { 
ellipse(mouseX, mouseY, 20, 20); 
} else if (mousePressed) { 
line(pmouseX, pmouseY, 
mouseX, mouseY); 
} else { 
point(mouseX, mouseY); 
} 
} 
circle 
line point
More complex if-else block use 
boolean isFirstClick = true; 
void setup() { 
ellipseMode(CENTER); 
} 
void draw() { 
if (isFirstClick) { 
ellipse(mouseX, mouseY, 20, 20); 
} else if (mousePressed) { 
line(pmouseX, pmouseY, 
mouseX, mouseY); 
} else { 
point(mouseX, mouseY); 
} 
} 
void mousePressed() { 
isFirstClick = false; 
strokeWeight(4); 
stroke(150, 0, 150); //purple 
} 
void keyPressed() { 
if (key =='c' || key == 'C') { 
background(200); //clear bg 
} 
if (key =='q' || key == 'Q') { 
exit(); //quit 
} 
}
Two things 
1. Global vs Local variables 
2. Iteration with while loops
1. Global (what we've been 
doing) versus Local variables 
variable scope
1. Global variables (used so far) 
Declare outside/beyond any block 
Known throughout all 
Persists from declaration throughout run of app 
float mdistance; 
void setup() { 
size(400, 300); 
frameRate(5); 
colorMode(HSB); 
mdistance = 10; 
} 
void draw() { 
mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, 0, height, 
mdistance, mdistance); 
triangle(width, height, width, 0, 
mdistance, mdistance); 
}
1. Local variables 
Declare inside a block 
Exists only inside the block 
Persists from declaration to end of current run of 
the block 
// no mdistance exists 
void setup() { 
size(400, 300); 
frameRate(5); 
colorMode(HSB); 
// no mdistance exists 
} 
void draw() { 
int mdistance = dist(pmouseX, pmouseY, 
mouseX, mouseY); 
fill(frameCount % 256, 255, 255); 
triangle(0, 0, 0, height, 
mdistance, mdistance); 
triangle(width, height, width, 0, 
mdistance, mdistance); 
} // no mdistance exists
1. What's the difference? 
// Global "count" 
int count = 0; 
void setup() { 
size(200,200); 
} 
void draw() { 
count = count + 1; 
background(count); 
println(count); 
} 
// Local "count" 
void setup() { 
size(200,200); 
} 
void draw() { 
int count = 0; 
count = count + 1; 
background(count); 
println(count); 
}
1. What's the difference? 
// Global "count" 
int count = 0; 
void setup() { 
size(200,200); 
} 
void draw() { 
count = count + 1; 
background(count); 
println(count); 
} 
// Local "count" 
void setup() { 
size(200,200); 
} 
void draw() { 
int count = 0; 
count = count + 1; 
background(count); 
println(count); 
}
1. Is count local or global? Would 
this declaration work? 
void setup() { 
size(200,200); 
int count = 0; 
} 
void draw() { 
count = count + 1; 
background(count); 
println(count); 
}
1. When should you use local vs. 
global? 
void mousePressed() { 
int count = frameCount % 255; 
background(count, 255, 255); 
} 
void draw() { 
int count = mouseX + 40; 
fill(count); 
triangle(0, 0, width, height, 
0, height); 
} 
Global if… 
Need to use in multiple 
blocks. 
Need to persist beyond 
a single run of a block. 
Local if… 
Alright/need to re-birth 
each run of a block (like 
both cases in this 
example)
2. Iteration with while loops 
Doing it and doing it and doing it well.
2. Conditional to Iteration 
locationX = 0; 
if (locationX < width) { 
point(locationX, 40); 
// move point 
locationX = locationX + 1; 
} 
// do more stuff 
Draw & 
move pt
2. Conditional to Iteration 
locationX = 0; 
while (locationX < width) { 
point(locationX, 40); 
// move point 
locationX = locationX + 1; 
} // exit loop only if condition false 
// do more stuff 
Draw & 
move pt
2. Conditional to Iteration 
Three keys to this loop: 
1. Prepare variable 
before 
2. Test condition with 
variable 
3. Change variable 
during repetition 
locationX = 0; 
while (locationX < width) { 
point(locationX, 40); 
// move point 
locationX = locationX + 1; 
} // exit loop if condition false 
// do more stuff 
If you don't ensure the while condition will at some point become true, the 
program won't ever escape the loop (until you kill it :O).
2. Iterating legs 
//Prep to draw legs - vertical lines 100px long 
strokeWeight(4); 
// First leg just beyond left edge of body (100). 
// Draw legs spaced every 15px until 
// beyond right edge of body, which is at 300. 
line(110, 100, 110, 200); 
//place next leg, 2 
line(125, 100, 125, 200); 
//place next leg, 3 
line(140, 100, 140, 200); 
//place next leg, 13 
line(290, 100, 290, 200); 
size(400, 300); 
stroke(0); 
strokeWeight(1); 
fill(0); 
//draw body as rectangle 
rectMode(CORNER); 
rect(100, 130, 200, 40); 
//draw head as ellipse 
ellipseMode(CENTER); 
ellipse(80, 150, 50, 30); 
What changes 
and what stays 
the same? Is 
there a pattern 
to the change? 
Can we use a 
variable to 
simplify?
2. Iterating legs 
//Prep to draw legs - vertical lines 100px long 
strokeWeight(4); 
// First leg just beyond left edge of body (100). 
// Draw legs spaced every 15px until 
// beyond right edge of body, which is at 300. 
legX = 110; 
line(legX, 100, legX, 200); 
//place next leg, 2 
legX = legX + 15; 
line(legX, 100, legX, 200); 
//place next leg, 13 
legX = legX + 15; 
line(legX, 100, legX, 200); 
int legX; //x position of leg 
size(400, 300); 
stroke(0); 
strokeWeight(1); 
fill(0); 
//draw body as rectangle 
rectMode(CORNER); 
rect(100, 130, 200, 40); 
//draw head as ellipse 
ellipseMode(CENTER); 
ellipse(80, 150, 50, 30);
2. Iterating legs 
//Prep to draw legs - vertical lines 100px long 
strokeWeight(4); 
// First leg just beyond left edge of body (100). 
// Draw legs spaced every 15px until 
// beyond right edge of body, which is at 300. 
legX = 110; 
line(legX, 100, legX, 200); 
//place next leg, 2 
legX = legX + 15; 
line(legX, 100, legX, 200); 
//place next leg, 13 
legX = legX + 15; 
line(legX, 100, legX, 200); 
int legX; //x position of leg 
size(400, 300); 
stroke(0); 
strokeWeight(1); 
fill(0); 
Prepared variable 
//draw body as rectangle 
rectMode(CORNER); 
rect(100, 130, 200, 40); 
Repetitive code that 
changes variable. 
//draw head as ellipse 
ellipseMode(CENTER); 
ellipse(80, 150, 50, 30);
2. Iterating legs 
// Prepare to draw legs 
strokeWeight(4); 
// First leg just beyond left edge of body (100). 
legX = 110; 
} // end setup 
// Draw legs spaced every 15px until 
// beyond right edge of body, which is at 300. 
void draw() { 
if ( ??? ) { // within body? 
line(legX, 100, legX, 200); 
//place next leg 
legX = legX + 15; 
} 
} 
int legX; //x position of leg 
void setup() { 
size(400, 300); 
stroke(0); 
strokeWeight(1); 
fill(0); 
Prepared variable 
//draw body as rectangle 
rectMode(CORNER); 
rect(100, 130, 200, 40); 
//draw head as ellipse 
ellipseMode(CENTER); 
ellipse(80, 150, 50, 30); 
Test 
condition? 
Repetitive code that 
changes variable.
2. Iterating legs 
// Prepare to draw legs 
strokeWeight(4); 
// First leg just beyond left edge of body (100). 
legX = 110; 
} // end setup 
// Draw legs spaced every 15px until 
// beyond right edge of body, which is at 300. 
void draw() { 
if (legX < 300) { // within body? 
line(legX, 100, legX, 200); 
//place next leg 
legX = legX + 15; 
} 
} 
int legX; //x position of leg 
void setup() { 
size(400, 300); 
stroke(0); 
strokeWeight(1); 
fill(0); 
//draw body as rectangle 
rectMode(CORNER); 
rect(100, 130, 200, 40); 
//draw head as ellipse 
ellipseMode(CENTER); 
ellipse(80, 150, 50, 30); 
Test 
condition
2. Iterating legs 
// Prepare to draw legs 
strokeWeight(4); 
// First leg just beyond left edge of body (100). 
legX = 110; 
// Draw legs spaced every 15px until 
// beyond right edge of body, which is at 300. 
while (legX < 300) { // within body? 
line(legX, 100, legX, 200); 
//place next leg 
legX = legX + 15; 
} 
noLoop(); //Don't need to do looping draw{} 
} // end setup 
void setup() { 
int legX; //x position of leg 
size(400, 300); 
stroke(0); 
strokeWeight(1); 
fill(0); 
//draw body as rectangle 
rectMode(CORNER); 
rect(100, 130, 200, 40); 
//draw head as ellipse 
ellipseMode(CENTER); 
ellipse(80, 150, 50, 30);
What goes in the blanks? 
size(200, 200); 
int y = 0; 
while ( _____ ) { 
line ( ___, ___, ____, ___ ); 
y = ___________ ; 
}
What goes in the blanks? 
size(200, 200); 
background(255); 
float w = ___ ; 
while ( ______ ) { 
fill( ___ ); 
ellipse( ___, ___, ___, ___ ); 
___ = ________ 20; 
}
Functions
Why herd code into functions? 
 Modularity 
Smaller chunks are easier 
to read/manage/update 
 Reusability 
You can write a chunk 
once, and use from 
multiple places
How could we 
break up draw? 
float xSpeed; 
float x, y; 
void setup() { 
size(200, 200); 
ellipseMode(CENTER); 
rectMode(CORNER); 
xSpeed = 1; // behavior if -1? 
x = 10; // start inside left edge 
y = height/2; // stay at middle 
} 
void draw() { 
//fill canvas with translucent rect 
fill(255, 60); 
rect(-1, -1, width+1, height+1); 
// draw the ball 
fill(255, 0, 0); 
ellipse(x, y, 20, 20); 
//at edge? Bounce the ball 
if ((x > width - 10) || (x < 10)) { 
xSpeed = xSpeed * -1; //reverse 
} 
// move the ball 
x = x + xSpeed; 
}
How could we 
break up draw? 
float xSpeed; 
float x, y; 
void setup() { 
size(200, 200); 
ellipseMode(CENTER); 
rectMode(CORNER); 
xSpeed = 1; // behavior if -1? 
x = 10; // start inside left edge 
y = height/2; // stay at middle 
} 
void draw() { 
//fill canvas with translucent rect 
fill(255, 60); 
rect(-1, -1, width+1, height+1); 
// draw the ball 
fill(255, 0, 0); 
ellipse(x, y, 20, 20); 
//at edge? Bounce the ball 
if ((x > width - 10) || (x < 10)) { 
xSpeed = xSpeed * -1; //reverse 
} 
// move the ball 
x = x + xSpeed; 
}
How could we 
break up draw? 
float xSpeed; 
float x, y; 
void setup() { 
size(200, 200); 
ellipseMode(CENTER); 
rectMode(CORNER); 
xSpeed = 1; // behavior if -1? 
x = 10; // start inside left edge 
y = height/2; // stay at middle 
} 
void draw() { 
//fill canvas with translucent rect 
fill(screenCanvas(); 
255, 60); 
rect(-1, -1, width+1, height+1); 
// draw the ball 
fill(drawBall(); 
255, 0, 0); 
ellipse(x, y, 20, 20); 
//at edge? Bounce the ball 
if bounceBall(); 
((x > width - 10) || (x < 10)) { 
xSpeed = xSpeed * -1; //reverse 
} 
// move the ball 
x = x + xSpeed; 
} 
moveBall();
How could we 
break up draw? 
// draw the ball 
void drawBall() { 
fill(255, 0, 0); 
ellipse(x, y, 20, 20); 
} 
//at edge of canvas? Bounce the ball 
void bounceBall() { 
if ((x > width - 10) || (x < 10)) { 
xSpeed = xSpeed * -1; //reverse 
} 
} 
// move the ball by xSpeed 
void moveBall() { 
x = x + xSpeed; 
} 
… 
void draw() { 
screenCanvas(); 
drawBall(); 
bounceBall(); 
moveBall(); 
} 
//fill canvas with translucent rect 
void screenCanvas() { 
fill(255, 60); 
rect(-1, -1, width+1, height+1); 
}
Anatomy of function calls 
(review) and blocks (new) 
void mousePressed() { 
functionName(arg1, arg2, arg3); 
} 
void functionName(int param1, int param2, boolean param3) { 
// Statements doing something like… 
if (param3) { 
point(param1, param2); 
} 
} 
Arguments can 
be passed to the 
function to be 
used as 
parameter 
variables.
Drawing faces flexibly 
void setup() { size(500, 500); } 
void draw() { 
drawFace(mouseX, mouseY, 100); 
} 
void drawFace(int faceX, int faceY, int radius) { 
ellipse(faceX, faceY, radius, radius); 
ellipse((faceX - radius/5), faceY, radius/5, radius/3); 
ellipse((faceX + radius/5), faceY, radius/5, radius/3); 
}

More Related Content

What's hot

Auto cad 2007-tutorial
Auto cad 2007-tutorialAuto cad 2007-tutorial
Auto cad 2007-tutorial
Syed Javeed
 

What's hot (15)

Autocad 131102050945-phpapp02
Autocad 131102050945-phpapp02Autocad 131102050945-phpapp02
Autocad 131102050945-phpapp02
 
Auto cad 2007-tutorial
Auto cad 2007-tutorialAuto cad 2007-tutorial
Auto cad 2007-tutorial
 
Unit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithmsUnit-2 raster scan graphics,line,circle and polygon algorithms
Unit-2 raster scan graphics,line,circle and polygon algorithms
 
AutoCad Basic tutorial
AutoCad Basic tutorialAutoCad Basic tutorial
AutoCad Basic tutorial
 
TUTORIAL AUTO CAD 3D
TUTORIAL AUTO CAD 3DTUTORIAL AUTO CAD 3D
TUTORIAL AUTO CAD 3D
 
Intro to HTML5 Canvas
Intro to HTML5 CanvasIntro to HTML5 Canvas
Intro to HTML5 Canvas
 
Autocad
AutocadAutocad
Autocad
 
Auto cad commands.
Auto cad commands.Auto cad commands.
Auto cad commands.
 
Autocad
AutocadAutocad
Autocad
 
Eg5 n
Eg5 nEg5 n
Eg5 n
 
Computer graphics
Computer graphics Computer graphics
Computer graphics
 
Auto CAD Notes
Auto CAD NotesAuto CAD Notes
Auto CAD Notes
 
Intro to AutoCAD
Intro to AutoCADIntro to AutoCAD
Intro to AutoCAD
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
 
Auto cad manual
Auto cad manualAuto cad manual
Auto cad manual
 

Similar to Learn Creative Coding: Begin Programming with the Processing Language

computer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdfcomputer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdf
SyedSajjadShah3
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfI need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdf
Conint29
 
14 class design (1)
14 class design (1)14 class design (1)
14 class design (1)
Satyam Gupta
 
ARTDM 170, Week13: Processing
ARTDM 170, Week13: ProcessingARTDM 170, Week13: Processing
ARTDM 170, Week13: Processing
Gilbert Guerrero
 
Im looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfIm looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdf
contact41
 
Displaying information within a window.68
Displaying information within a window.68Displaying information within a window.68
Displaying information within a window.68
myrajendra
 

Similar to Learn Creative Coding: Begin Programming with the Processing Language (20)

Introduction to Coding
Introduction to CodingIntroduction to Coding
Introduction to Coding
 
computer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdfcomputer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdf
 
Powerpointpresentation.c
Powerpointpresentation.cPowerpointpresentation.c
Powerpointpresentation.c
 
I need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdfI need help with this assignment Ive gotten abit stuck with the cod.pdf
I need help with this assignment Ive gotten abit stuck with the cod.pdf
 
Introduction to Processing
Introduction to ProcessingIntroduction to Processing
Introduction to Processing
 
Interactive Mouse (Report On Processing)
Interactive Mouse (Report On Processing)Interactive Mouse (Report On Processing)
Interactive Mouse (Report On Processing)
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
Computer Graphics Concepts
Computer Graphics ConceptsComputer Graphics Concepts
Computer Graphics Concepts
 
14 class design (1)
14 class design (1)14 class design (1)
14 class design (1)
 
14 class design
14 class design14 class design
14 class design
 
ARTDM 170, Week13: Processing
ARTDM 170, Week13: ProcessingARTDM 170, Week13: Processing
ARTDM 170, Week13: Processing
 
Im looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdfIm looking for coding help I dont really need this to be explained.pdf
Im looking for coding help I dont really need this to be explained.pdf
 
Displaying information within a window.68
Displaying information within a window.68Displaying information within a window.68
Displaying information within a window.68
 
P5js syracuse dev meetup 20181218
P5js syracuse dev meetup 20181218P5js syracuse dev meetup 20181218
P5js syracuse dev meetup 20181218
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3
 
Building a Visualization Language
Building a Visualization LanguageBuilding a Visualization Language
Building a Visualization Language
 
canvas_1.pptx
canvas_1.pptxcanvas_1.pptx
canvas_1.pptx
 
my ppt for autocad &autocad electrical
my ppt for autocad &autocad electricalmy ppt for autocad &autocad electrical
my ppt for autocad &autocad electrical
 
Autocad electrical
Autocad electricalAutocad electrical
Autocad electrical
 
Introduction to Generative Art with Processing
Introduction to Generative Art with ProcessingIntroduction to Generative Art with Processing
Introduction to Generative Art with Processing
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Learn Creative Coding: Begin Programming with the Processing Language

  • 1. Learn Creative Coding Begin Programming with the Processing Language W. Michelle Harris Rochester Institute of Technology http://people.rit.edu/wmhics Long Beach Indie Digital Edutainment Conference 8.30.14
  • 3. How do you communicate a design? Imagine you opened your favorite drawing app, created a new document, and drew a straight line and a circle. How would you type out complete instructions for re-creating your drawing (no illustrations allowed)? Take a few minutes to ponder this…
  • 4. Do they need to know: 1. Drawing app choice 2. Dimensions of canvas 3. Canvas color 4. Line drawing tool 5. Line stroke color, thickness 6. Line drag start and stop coordinates 7. Circle drawing tool 8. Circle stroke color, thickness 9. Circle fill color and pattern 10. Circle bounding box drag start and stop coordinates -or- Circle center coordinate and radius 11. ?
  • 5. Line + circle in drawing app, then in Processing code
  • 6. Prepare the Canvas 1. Drawing app choice: new Processing sketch! 2. Dimensions of canvas 3. Canvas color
  • 7. Processing environment A program written in Processing is called a sketch. Type your sketch in this space. Text is colored automatically according to its purpose. Status feedback area Console area for text output and too-detailed error messages. Current cursor line number in this sketch.
  • 8. A warning before we code: Processing is as picky as your 8th grade Language Arts teacher Order, spelling, capitalization, and punctuation matter. Mistake => won’t go at all, or goes wonky. point(30, 75); // point() parameters: x, y  Function name, then parentheses with any parameters inside separated by commas. End with a semicolon.  Each instruction can start and be interspersed with spaces, tabs, and even line breaks for readability.  Entire lines can be blank for readability, too.
  • 9. Prepare the Canvas /* When you make this new canvas, size it 300 pixels wide, and 250 pixels tall. */ size(300, 250); /* Set the canvas background color to white. */ background(255); size(widthPix, heightPix); // in pixels background(grayValue); // 0.0 – 255.0 // Alternatively, background(red, green, blue);
  • 10. Prepare settings for the Line 4. Line drawing tool? There is only one choice ;) 5. Line stroke color, thickness
  • 11. Prepare settings for the Line /* Set the drawing stroke color to blue */ stroke(128, 119, 251); /* Set the drawing stroke thickness to 5 pixels */ strokeWeight(5); stroke(redVal, blueVal, greenVal); //0.0 – 255 strokeWeight(widthPix);
  • 12. Draw the Line 6. Line drag start and stop coordinates
  • 13. Draw the Line /* Start line at coordinates x:135, y:213, and end the line at coordinates x:287, y:66. */ line(135, 213, 287, 66); line(x1, y1, x2, y2); Save and Run your sketch.
  • 14. Prepare settings for the Circle 7. Circle drawing tool: Choose drawing mode – corner-to-corner? Start coordinates & width & height (default mode in Processing)? 8. Circle stroke color, thickness 9. Circle fill color and pattern
  • 15. Prepare settings for the Circle /* Set the drawing fill color to black */ fill(0); /* We don’t want any drawing stroke */ noStroke(); fill(grayValue); //0–255 fill(red, blue, green); noStroke();
  • 16. Draw the Circle 10.From circle center drag out width and height
  • 17. Draw the Circle /* Center ellipse at coordinates x:110, y:110, and make width 100, height 100. */ ellipse(110, 110, 100, 100); ellipse(x, y, width, height); Save and Run your sketch.
  • 18. One question you might have Sometimes, we gave Processing a single grayscale color value, and sometimes we gave it three RGB color values. Can both be right?
  • 19. stroke(): 6 ways to color stroke(gray); stroke(red, green, blue); stroke(#RRGGBB); stroke(gray, alpha); stroke(red, green, blue, alpha); stroke(#RRGGBB, alpha); Processing knows which version of the function you want by the number and type of parameters within its parentheses. Many of the built-in functions work this way.
  • 20. Start exploring (the Processing Reference too) Some primitive 2D shapes: line(), triangle(), quad(), rect(), ellipse(), arc() Setting shape attributes: fill(), noFill() stroke(), noStroke() strokeWeight(), strokeCap() ellipseMode(), rectMode()
  • 21. Section goals 1. If I design a composition with simple shapes, I can remake it in Processing. 2. I know how to use different kinds of color parameters to make those shapes. 3. I know how to use the Processing Reference to find out details about functions.
  • 22. Helpful hints Pay attention when auto-coloring does NOT do what you expected. Get familiar with what kinds of errors cause which messages and where (make mistakes on purpose to check). Processing will stop compiling your code at the first syntax error it encounters. Watch the cursor for where it thinks that error occurred.
  • 23. Next: Five Things 1. /* Comments */ and //Comments 2. Math expressions 3. Code blocks for animation 4. Tracking the mouse
  • 24. 1. Which would you rather troubleshoot? size(300, 250); background(255); stroke(128, 119, 251); strokeWeight(5); line(135, 213, 287, 66); fill(0); noStroke(); ellipseMode(CORNERS); ellipse(59, 60, 159, 160); /* Canvas prep */ size(300, 250); background(255); // white bg /* Line prep */ stroke(128, 119, 251); // blue line strokeWeight(5); /* Make the line */ line(135, 213, 287, 66); /* Circle prep */ fill(0); // black fill noStroke(); /* Make the circle */ ellipse(110, 110, 100, 100);
  • 25. 1. Comments, 2 ways //Single line comment /* Multiline comments can take up all the space you need as long as they end with… */ /* Canvas prep */ size(300, 250); background(255); // white /* Line prep */ stroke(128, 119, 251); // blue strokeWeight(5); // Make the line line(135, 213, 287, 66);
  • 26. 2. How many little equations would you need to figure to position all the shapes? An alternative: substitute a math expression for a person-calculated value.
  • 27. 2. Math expressions as function parameters. Code before... + plus size(400, 300); - minus *multiplied by / divided by %modulo remainder // head ellipse(200, 150, 100, 100); //eyes ellipse(180, 150, 20, 30); ellipse(220, 150, 20, 30);
  • 28. 2. Math expressions as function parameters. Code lazier. + plus size(400, 300); - minus *multiplied by / divided by %modulo remainder // head ellipse(200, 150, 100, 100); //eyes ellipse(200 - 20, 150, 20, 30); ellipse(200 + 20, 150, 20, 30);
  • 29. 2. Math expressions as function parameters. Code laziest. + plus - minus *multiplied by / divided by %modulo remainder Helpful built-in property variables: width //canvas width height //canvas height size(400, 300); // head – centered on canvas ellipse(width/2, height/2, 100, 100); //eyes ellipse((width/2 – 20), height/2, 20, 30); ellipse((width/2 + 20), height/2, 20, 30);
  • 30. 3. An analogy: running http://www.blackgirlsrun.com/partner-up
  • 31. 3. To animate runners One-time Setup steps: 1. Size the canvas 2. Set the desired frame update rate Perpetually loop these Draw steps: 1. Color the canvas (to erase the prior frame) 2. Draw scenery elements 3. Draw next sprite in cycle of runner #1 4. Draw next sprite in cycle of runner #2 5. Render pixels from 1-4.
  • 32. 3. Blocks: grouping/herding code A block of code is any code enclosed in { }. One useful feature of blocks: they can be nested within each other. block1 { // a block of code opens statement; block2 { // a nested block of code opens statement; statement; } // end of block2 statement; } // end of block1 block3 { // a block of code opens statement; } // end of block3
  • 33. 3. To animate in Processing void setup() { // Initialization code goes here size(400, 3oo); frameRate(24); // Frames per second. Default max is 60. } void draw() { // Code to run forever - animate - goes here background(255); drawScenery(); // this function doesn’t exist drawNextRunner1(); // this function doesn’t exist drawNextRunner2(); // this function doesn’t exist /* At the end of draw(), Processing renders the new pixels produced in this block. */ }
  • 34. 3. Try this in Processing // Pointy sketch size(400, 300); strokeWeight(5); stroke(0); background(255); point(200, 150); // Pointy sketch void setup() { size(400, 300); strokeWeight(5); stroke(0); } void draw() { // animate! background(255); point(200, 150); }
  • 35. 4. Now edit it } void draw() { // animate! background(255); point(200, 150); } } void draw() { // animate! background(255); //point(200, 150); point(mouseX, mouseY); } mouseX and mouseY are built-in property variables of the sketch like height and width (in the same way that point() is a built-in function).
  • 36. 4. What’s the difference? void setup() { size(400, 3oo); strokeWeight(5); stroke(0); } void draw() { // animate! background(255); point(mouseX, mouseY); } void setup() { size(400, 3oo); strokeWeight(5); stroke(0); background(255); } void draw() { // animate! // background(255); point(mouseX, mouseY); }
  • 37. 4. Edit for variation with pmouseX and pmouseY void draw() { // animate! // background(255); point(mouseX, mouseY); } void draw() { // animate! // background(255); // point(mouseX, mouseY); line(pmouseX, pmouseY, mouseX, mouseY); } pmouseX and pmouseY are built-in property variables for the position of the mouse in the frame draw()n just prior to the current one.
  • 38. 3 Things you’re doing now 1. draw() {} code blocks for animation 2. Math expressions with built-in property variables 3. Mouse property variables for interaction
  • 39. Data types In Processing, data are categorized by type. Three useful data types: int whole numbers (-230548, 0, 45) float decimal numbers (-4.3, 30984.349) String sequence of characters ("Hi, Ebony!")
  • 40. Making our own Variables What if I want to use that hard-calculated value again? The answer: Let's create our own variable and store it there.
  • 41. Variables Container for storing some piece of data. A variable is a way for the program to remember. Each variable has a name  We make up almost any name we want within the rules (but it should make sense!)  We can use the name to refer to that piece of data The value might change during the execution of the program. Hence, “variable.”  The variable actually points to a specific physical memory location in RAM.
  • 42. Some redundancies here... void setup() { size(400, 300); frameRate(5); colorMode(HSB); } void draw() { fill(frameCount % 256, 255, 255); triangle(0, 0, mouseX, mouseY, dist(pmouseX, pmouseY, mouseX, mouseY), dist(pmouseX, pmouseY, mouseX, mouseY)); }
  • 43. float mdistance; // Declare the variable type and name void setup() { size(400, 300); frameRate(5); colorMode(HSB); } void draw() { /* Assign mdistance to store the distance between the latest and just-previous mouse positions. */ mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, mouseX, mouseY, mdistance, mdistance); }
  • 44. float mdistance; // Declare the variable type and name void setup() { size(400, Before 300); we use a variable, we need to declare frameRate(5); colorMode(it. HSB); } To declare it, use the keyword for the type of void draw() data { it will hold: /* Assign mdistance to store the distance between String instructions; int shapeNumber; the latest and just-previous mouse positions. */ mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); At this point, the variable exists, but doesn’t hold a value yet. We’ve simply informed the computer that we intend to use a variable by that name and of that data type. fill(frameCount % 256, 255, 255); triangle(0, 0, mouseX, mouseY, mdistance, mdistance); }
  • 45. float mdistance; // Declare the variable type and name void setup() { size(400, 300); frameRate(5); Why is this variable colorMode(HSB); declared as type float? } Why not type int? void draw() { /* Assign mdistance to store the distance between the latest and just-previous mouse positions. */ mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, 0, height, mdistance, mdistance); }
  • 46. void draw() { /* Assign mdistance to store the distance between the latest and just-previous mouse positions. */ mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, 0, height, mdistance, mdistance); triangle(width, height, width, 0, mdistance, mdistance); } Give a variable a value with an assignment statement – it uses an equal sign. shapeNumber = frameCount - 1; The action goes right to left: the value (frameCount – 1) replaces any current value in the variable shapeNumber.
  • 47. float mdistance; // Declare the variable type and name void setup() Once { the variable is declared and a value size(400, assigned, 300); we can use the variable anywhere frameRate(5); colorMode(we would HSB); have used the value: } point(shapeNumber, height/2); void draw() { /* Assign mdistance to store the distance between the latest and just-previous mouse positions. */ mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, mouseX, mouseY, mdistance, mdistance); }
  • 48. float mdistance; // Declare the variable type and name void setup() { size(400, 300); frameRate(5); colorMode(HSB); } void draw() { /* Assign mdistance to store the distance between the latest and just-previous mouse positions. */ mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, mouseX, mouseY, mdistance, mdistance); }
  • 49. Rules for variable names Must start with a letter  May NOT contain spaces (only one “word”) May contain numbers and _underscores_ camelCase  Start with lower case letter  Opt: next words start with upper case (easier to read)  Tip: Longer is often better for future comprehension  selectedCar instead of sc or selCar or selectCar
  • 50. You can declare and assign an initial value in one statement // Circles along the diagonal int shapeNumber; int randomSize; void setup() { size(200, 200); ellipseMode(CENTER); shapeNumber = 1; } void draw() { randomSize = int (random(5, 50)); ellipse(shapeNumber, shapeNumber, randomSize, randomSize); shapeNumber = shapeNumber + 1; } // Circles along the diagonal int shapeNumber = 1; int randomSize; void setup() { size(200, 200); ellipseMode(CENTER); } void draw() { …
  • 51. Statement review String instructions; void setup() { size(200, 500); instructions = "Swoosh your mouse"; } void draw() { // points follow mouse point(mouseX, mouseY); } /* On click, instructions print in console */ void mousePressed() { println(instructions); } 1. Variable declaration 2. Code block 3. Function call 4. Assignment statement 5. Comments
  • 52. and now 6... If-else [conditional] blocks Instead of always following one path, let the computer make a choice based on the current outcome of some condition.
  • 53. Decisions in computerese Carbon-based life If it is raining hard enough to bother me outside, maybe I'll put on a hat or carry an umbrella – depends on my mood. Silicon-based machines rainIntensity = calcIntensity(); if (rainIntensity > 3) { wearRainCover(); } wear RainCover
  • 54. Decisions in computerese Carbon-based life If it is raining hard enough to bother me outside, maybe I'll put on a hat or carry an umbrella – depends on my mood. Otherwise, I'm wearing my new shades. Silicon-based machines rainIntensity = calcIntensity(); if (rainIntensity > 3) { wearRainCover(); } else { wearSunCover(); }
  • 55. If-else block if (rainIntensity > 3) { // do this if true wearRainCover(); } else { // otherwise do this wearSunCover(); } Expression that can evaluate to true or false rain sun
  • 56. If-else block if (Boolean expression) { // do this if true statement; statement; } else { // otherwise do this (opt) statement; statement; statement; } Expression that can evaluate to true or false
  • 57. Start with creeping circle... int x = -20; void setup() { ellipseMode(CENTER); } void draw() { ellipse(x, height/2, 20, 20); x = x + 1; } After the circle gets beyond the right edge, we will never see it again. Let's change that.
  • 58. Proceed with cycling circle … } void draw() { ellipse(x, height/2, 20, 20); x = x + 1; // If circle gets too far… if (x > (width + 20)) { //cycle! x = -20; } } … } void draw() { ellipse(x, height/2, 20, 20); x = x + 1; /* When circle goes off right edge, start it back at left edge again. */ }
  • 59. Boolean expressions with comparison operators true 45 == 45 10 < 100 10 > -3 99 <= 100 99 >= 98 100 != 88 Do block false 45 == 30 10 < -3 10 > 88 99 <= 30 99 >= 100 100 != 100 Skip block Reserved word representing a logical value. Equal? Less? Greater? Less or equal? Greater or equal? Not equal?
  • 60. Points, black circles, and red rects depending on conditional expressions. void setup() { ellipseMode(CENTER); rectMode(CENTER); } void draw() { if (mouseX < 10) { // far left ellipse (width/2, mouseY, 20, 20); } if (mouseX < width/2) { // left side stroke(0); // black stroke } else { // right side stroke(255,0,0); //red stroke } point(mouseX, mouseY); if (mouseX >= width - 10) { // far right rect (width/2, mouseY, 20, 20); } }
  • 61. Boolean variables boolean isFirstClick = true; void setup() { ellipseMode(CENTER); } void draw() { if (isFirstClick) { ellipse(mouseX, mouseY, 20, 20); } else { point(mouseX, mouseY); } } void mousePressed() { isFirstClick = false; strokeWeight(4); stroke(150, 0, 150); //purple }
  • 62. More complex expressions Logical operators: a || b || c // or if any is true a && b && c // and only if all true !a // not if not true booleanmyTest; myTest = (mouseY < 0) || (mouseY > height); // mousePressed and keyPressed // are built-in boolean variables. myTest = mousePressed && keyPressed; // key (built-in char variable) returns // most recent keyboard key value myTest = !((key == 'q') || (key == 'Q'));
  • 63. More complex expressions Assume height=200 mouseY myTest -10 true 0 false 50 false 200 false 201 true boolean myTest; myTest = (mouseY < 0) || (mouseY > height); // Would otherTest ever be true? boolean otherTest; otherTest = (mouseY < 0) && (mouseY > height);
  • 64. More complex expressions boolean myTest; // mousePressed and keyPressed // are built-in boolean variables. myTest = mousePressed && keyPressed; // What's the difference? boolean otherTest; otherTest = mousePressed || keyPressed; keyPressed mousePressed myTest true true true true false false false true false false false false
  • 65. More complex if-else block use boolean isFirstClick = true; void setup() { ellipseMode(CENTER); } void draw() { if (isFirstClick) { ellipse(mouseX, mouseY, 20, 20); } else if (mousePressed) { line(pmouseX, pmouseY, mouseX, mouseY); } else { point(mouseX, mouseY); } } circle line point
  • 66. More complex if-else block use boolean isFirstClick = true; void setup() { ellipseMode(CENTER); } void draw() { if (isFirstClick) { ellipse(mouseX, mouseY, 20, 20); } else if (mousePressed) { line(pmouseX, pmouseY, mouseX, mouseY); } else { point(mouseX, mouseY); } } void mousePressed() { isFirstClick = false; strokeWeight(4); stroke(150, 0, 150); //purple } void keyPressed() { if (key =='c' || key == 'C') { background(200); //clear bg } if (key =='q' || key == 'Q') { exit(); //quit } }
  • 67. Two things 1. Global vs Local variables 2. Iteration with while loops
  • 68. 1. Global (what we've been doing) versus Local variables variable scope
  • 69. 1. Global variables (used so far) Declare outside/beyond any block Known throughout all Persists from declaration throughout run of app float mdistance; void setup() { size(400, 300); frameRate(5); colorMode(HSB); mdistance = 10; } void draw() { mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, 0, height, mdistance, mdistance); triangle(width, height, width, 0, mdistance, mdistance); }
  • 70. 1. Local variables Declare inside a block Exists only inside the block Persists from declaration to end of current run of the block // no mdistance exists void setup() { size(400, 300); frameRate(5); colorMode(HSB); // no mdistance exists } void draw() { int mdistance = dist(pmouseX, pmouseY, mouseX, mouseY); fill(frameCount % 256, 255, 255); triangle(0, 0, 0, height, mdistance, mdistance); triangle(width, height, width, 0, mdistance, mdistance); } // no mdistance exists
  • 71. 1. What's the difference? // Global "count" int count = 0; void setup() { size(200,200); } void draw() { count = count + 1; background(count); println(count); } // Local "count" void setup() { size(200,200); } void draw() { int count = 0; count = count + 1; background(count); println(count); }
  • 72. 1. What's the difference? // Global "count" int count = 0; void setup() { size(200,200); } void draw() { count = count + 1; background(count); println(count); } // Local "count" void setup() { size(200,200); } void draw() { int count = 0; count = count + 1; background(count); println(count); }
  • 73. 1. Is count local or global? Would this declaration work? void setup() { size(200,200); int count = 0; } void draw() { count = count + 1; background(count); println(count); }
  • 74. 1. When should you use local vs. global? void mousePressed() { int count = frameCount % 255; background(count, 255, 255); } void draw() { int count = mouseX + 40; fill(count); triangle(0, 0, width, height, 0, height); } Global if… Need to use in multiple blocks. Need to persist beyond a single run of a block. Local if… Alright/need to re-birth each run of a block (like both cases in this example)
  • 75. 2. Iteration with while loops Doing it and doing it and doing it well.
  • 76. 2. Conditional to Iteration locationX = 0; if (locationX < width) { point(locationX, 40); // move point locationX = locationX + 1; } // do more stuff Draw & move pt
  • 77. 2. Conditional to Iteration locationX = 0; while (locationX < width) { point(locationX, 40); // move point locationX = locationX + 1; } // exit loop only if condition false // do more stuff Draw & move pt
  • 78. 2. Conditional to Iteration Three keys to this loop: 1. Prepare variable before 2. Test condition with variable 3. Change variable during repetition locationX = 0; while (locationX < width) { point(locationX, 40); // move point locationX = locationX + 1; } // exit loop if condition false // do more stuff If you don't ensure the while condition will at some point become true, the program won't ever escape the loop (until you kill it :O).
  • 79. 2. Iterating legs //Prep to draw legs - vertical lines 100px long strokeWeight(4); // First leg just beyond left edge of body (100). // Draw legs spaced every 15px until // beyond right edge of body, which is at 300. line(110, 100, 110, 200); //place next leg, 2 line(125, 100, 125, 200); //place next leg, 3 line(140, 100, 140, 200); //place next leg, 13 line(290, 100, 290, 200); size(400, 300); stroke(0); strokeWeight(1); fill(0); //draw body as rectangle rectMode(CORNER); rect(100, 130, 200, 40); //draw head as ellipse ellipseMode(CENTER); ellipse(80, 150, 50, 30); What changes and what stays the same? Is there a pattern to the change? Can we use a variable to simplify?
  • 80. 2. Iterating legs //Prep to draw legs - vertical lines 100px long strokeWeight(4); // First leg just beyond left edge of body (100). // Draw legs spaced every 15px until // beyond right edge of body, which is at 300. legX = 110; line(legX, 100, legX, 200); //place next leg, 2 legX = legX + 15; line(legX, 100, legX, 200); //place next leg, 13 legX = legX + 15; line(legX, 100, legX, 200); int legX; //x position of leg size(400, 300); stroke(0); strokeWeight(1); fill(0); //draw body as rectangle rectMode(CORNER); rect(100, 130, 200, 40); //draw head as ellipse ellipseMode(CENTER); ellipse(80, 150, 50, 30);
  • 81. 2. Iterating legs //Prep to draw legs - vertical lines 100px long strokeWeight(4); // First leg just beyond left edge of body (100). // Draw legs spaced every 15px until // beyond right edge of body, which is at 300. legX = 110; line(legX, 100, legX, 200); //place next leg, 2 legX = legX + 15; line(legX, 100, legX, 200); //place next leg, 13 legX = legX + 15; line(legX, 100, legX, 200); int legX; //x position of leg size(400, 300); stroke(0); strokeWeight(1); fill(0); Prepared variable //draw body as rectangle rectMode(CORNER); rect(100, 130, 200, 40); Repetitive code that changes variable. //draw head as ellipse ellipseMode(CENTER); ellipse(80, 150, 50, 30);
  • 82. 2. Iterating legs // Prepare to draw legs strokeWeight(4); // First leg just beyond left edge of body (100). legX = 110; } // end setup // Draw legs spaced every 15px until // beyond right edge of body, which is at 300. void draw() { if ( ??? ) { // within body? line(legX, 100, legX, 200); //place next leg legX = legX + 15; } } int legX; //x position of leg void setup() { size(400, 300); stroke(0); strokeWeight(1); fill(0); Prepared variable //draw body as rectangle rectMode(CORNER); rect(100, 130, 200, 40); //draw head as ellipse ellipseMode(CENTER); ellipse(80, 150, 50, 30); Test condition? Repetitive code that changes variable.
  • 83. 2. Iterating legs // Prepare to draw legs strokeWeight(4); // First leg just beyond left edge of body (100). legX = 110; } // end setup // Draw legs spaced every 15px until // beyond right edge of body, which is at 300. void draw() { if (legX < 300) { // within body? line(legX, 100, legX, 200); //place next leg legX = legX + 15; } } int legX; //x position of leg void setup() { size(400, 300); stroke(0); strokeWeight(1); fill(0); //draw body as rectangle rectMode(CORNER); rect(100, 130, 200, 40); //draw head as ellipse ellipseMode(CENTER); ellipse(80, 150, 50, 30); Test condition
  • 84. 2. Iterating legs // Prepare to draw legs strokeWeight(4); // First leg just beyond left edge of body (100). legX = 110; // Draw legs spaced every 15px until // beyond right edge of body, which is at 300. while (legX < 300) { // within body? line(legX, 100, legX, 200); //place next leg legX = legX + 15; } noLoop(); //Don't need to do looping draw{} } // end setup void setup() { int legX; //x position of leg size(400, 300); stroke(0); strokeWeight(1); fill(0); //draw body as rectangle rectMode(CORNER); rect(100, 130, 200, 40); //draw head as ellipse ellipseMode(CENTER); ellipse(80, 150, 50, 30);
  • 85. What goes in the blanks? size(200, 200); int y = 0; while ( _____ ) { line ( ___, ___, ____, ___ ); y = ___________ ; }
  • 86. What goes in the blanks? size(200, 200); background(255); float w = ___ ; while ( ______ ) { fill( ___ ); ellipse( ___, ___, ___, ___ ); ___ = ________ 20; }
  • 88. Why herd code into functions?  Modularity Smaller chunks are easier to read/manage/update  Reusability You can write a chunk once, and use from multiple places
  • 89. How could we break up draw? float xSpeed; float x, y; void setup() { size(200, 200); ellipseMode(CENTER); rectMode(CORNER); xSpeed = 1; // behavior if -1? x = 10; // start inside left edge y = height/2; // stay at middle } void draw() { //fill canvas with translucent rect fill(255, 60); rect(-1, -1, width+1, height+1); // draw the ball fill(255, 0, 0); ellipse(x, y, 20, 20); //at edge? Bounce the ball if ((x > width - 10) || (x < 10)) { xSpeed = xSpeed * -1; //reverse } // move the ball x = x + xSpeed; }
  • 90. How could we break up draw? float xSpeed; float x, y; void setup() { size(200, 200); ellipseMode(CENTER); rectMode(CORNER); xSpeed = 1; // behavior if -1? x = 10; // start inside left edge y = height/2; // stay at middle } void draw() { //fill canvas with translucent rect fill(255, 60); rect(-1, -1, width+1, height+1); // draw the ball fill(255, 0, 0); ellipse(x, y, 20, 20); //at edge? Bounce the ball if ((x > width - 10) || (x < 10)) { xSpeed = xSpeed * -1; //reverse } // move the ball x = x + xSpeed; }
  • 91. How could we break up draw? float xSpeed; float x, y; void setup() { size(200, 200); ellipseMode(CENTER); rectMode(CORNER); xSpeed = 1; // behavior if -1? x = 10; // start inside left edge y = height/2; // stay at middle } void draw() { //fill canvas with translucent rect fill(screenCanvas(); 255, 60); rect(-1, -1, width+1, height+1); // draw the ball fill(drawBall(); 255, 0, 0); ellipse(x, y, 20, 20); //at edge? Bounce the ball if bounceBall(); ((x > width - 10) || (x < 10)) { xSpeed = xSpeed * -1; //reverse } // move the ball x = x + xSpeed; } moveBall();
  • 92. How could we break up draw? // draw the ball void drawBall() { fill(255, 0, 0); ellipse(x, y, 20, 20); } //at edge of canvas? Bounce the ball void bounceBall() { if ((x > width - 10) || (x < 10)) { xSpeed = xSpeed * -1; //reverse } } // move the ball by xSpeed void moveBall() { x = x + xSpeed; } … void draw() { screenCanvas(); drawBall(); bounceBall(); moveBall(); } //fill canvas with translucent rect void screenCanvas() { fill(255, 60); rect(-1, -1, width+1, height+1); }
  • 93. Anatomy of function calls (review) and blocks (new) void mousePressed() { functionName(arg1, arg2, arg3); } void functionName(int param1, int param2, boolean param3) { // Statements doing something like… if (param3) { point(param1, param2); } } Arguments can be passed to the function to be used as parameter variables.
  • 94. Drawing faces flexibly void setup() { size(500, 500); } void draw() { drawFace(mouseX, mouseY, 100); } void drawFace(int faceX, int faceY, int radius) { ellipse(faceX, faceY, radius, radius); ellipse((faceX - radius/5), faceY, radius/5, radius/3); ellipse((faceX + radius/5), faceY, radius/5, radius/3); }

Editor's Notes

  1. The Else part of if-else is optional.
  2. Note the distinct difference between = (assignment) and == (comparison)