Variables
Purpose
Learn how to make computers remember numbers.
Learning Objectives
By the end of this activity, you should be able to:
- Explain what a variable is and why it is useful in programming
- Create and use variables to store numeric values
- Use the built-in variables mouseX and mouseY to create interactive elements
- Predict how changing variable values affects the visual output
Model 1
let circleX = 100;
let circleY = 150;
let squareX = 300;
let squareY = 200;
let shapeSize = 50;
let shapeColor = 100;
function setup() {
createCanvas(400, 300);
}
function draw() {
background(220);
//
fill(255, 0, 0);
circle(circleX, circleY, shapeSize);
//
fill(shapeColor);
square(squareX, squareY, shapeSize);
//
fill(0, 0, 255);
rect(150, 100, shapeSize, shapeSize + 50);
//
fill(shapeColor);
circle(350, 50, shapeSize);
//
stroke(shapeColor);
line(150, 250, 250, 250);
}
Part 1: (PAPER) Exploration
- How many variables are defined at the top of the code?
- What values are stored in the variables
circleXandcircleY? - What does the variable
shapeSizerepresent? - Find where
circleXandcircleYare used in the code. What function uses these variables? - What do you think would happen if we changed
circleXfrom 100 to 200? - How many times is the
shapeColorvariable used in the code? - Which functions use the
shapeColorvariable? - What would happen if we changed the value of
shapeColorfrom 100 to 255?
Part 2: (PAPER) Concept Invention
- Why do you think the programmer created variables instead of just using numbers?
- Why do you think the variable for the circle’s x-position is named
circleXrather than justx? - What is the advantage of using the
shapeColorvariable instead of repeating the number 100 three times in the code? - If you wanted to change the color of all three shapes that use
shapeColor, how many lines of code would you need to change?
Part 3: (ON YOUR COMPUTER) Practice
- Click here to open the code.
- Make your own copy of the code (File > Duplicate).
- Do at least one of these exercises:
- EASY: Change the red circle to green
- MEDIUM: Create a new variable called
outlineThicknessand use it to set thestrokeWeightfor all shapes - HARD: Add a new rectangle that uses
circleXandcircleYfor its position, but has a width and height that are half ofsquareSize
Model 2
let size = 30;
function setup() {
createCanvas(400, 300);
}
function draw() {
background(220);
//
fill(0);
textSize(20);
text(mouseX, 30, 30);
text(mouseY, 30, 60);
//
fill(255, 0, 0);
circle(mouseX, mouseY, size);
//
fill(0, 0, 255);
rect(300, 200, size, size);
}
Exploration
- Which variables in this code were not explicitly defined at the top?
- Where are
mouseXandmouseYused in the code? -
Click here to open the code and run it:
- What happens when you move your mouse around the canvas?
- What information is displayed at the top of the canvas?
- How does the red circle behave compared to the blue square?
Concept Invention
- What do the variables
mouseXandmouseYrepresent? - How are
mouseXandmouseYdifferent from the variables we created in Model 1? - Why do you think these variables are useful in p5.js?
Practice
- Predict what would happen if you made these changes:
- Change the circle’s y-coordinate to be
mouseYbut keep its x-coordinate fixed at 200 - Use
mouseYfor the width of the rectangle instead of 80 - Change the circle’s diameter to be based on
mouseX(hint: usemouseX/4to keep a reasonable size)
- Change the circle’s y-coordinate to be
- Click here to open the code.
- Make your own copy of the code (File > Duplicate), and update the code to test each prediction you just made.
- For each prediction, write what actually happened.