Students use if and else to bounce a circle off the edges and change its colour, learn the difference between = (set) and === (ask), use mouseX in a condition, and build a bouncing ball with a paddle that follows the mouse.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 15 min
Read the unit primer first. This lesson revisits the turtle course's decisions lesson in JavaScript: the same idea, new spellings (if (x > 300) { ... } with brackets and braces; || and && for or and and). The hard part is = versus ===: one sets, three asks. The bug task is that slip. speed = -speed turns a direction round and is the engine of every bounce. Students often write x === 300 for the edge, which can be jumped over by a step bigger than 1; the lesson suggests > and <. The Make step is a small Pong: the paddle's collision check is the hardest line, so offer the finished sol-make idea only after students have tried the first three parts.
= versus === in an if, and the long condition for the paddle. Read conditions aloud in words before typing them.
| What you will see | A question to ask | The fix |
|---|---|---|
| The circle sticks at an edge or jumps. | Is the if using one = or three ===? | Use > or < to ask about the edge. A single = sets the variable. |
| A red line: Unexpected token. | Does each if have its ( ) and its { }, and does each { have a }? | Count the brackets and braces on the line the hint names. |
| The ball passes through the paddle. | Is the check on the ball's x and its y, and is dy turned round? | Check ballY is near the paddle's row and ballX is between its edges, then dy = -dy. |
| Nothing happens when the mouse moves. | Did you click the sketch first, and is mouseX used in draw()? | mouseX only changes while the pointer is over the canvas. |
One possible solution for each task. Other correct programs are fine.
let x = 150;
let speed = 3;
function setup() {
createCanvas(300, 200);
}
function draw() {
background(20, 30, 60);
if (mouseX > 150) {
fill(255, 120, 160);
} else {
fill(120, 220, 255);
}
circle(x, 100, 40);
x = x + speed;
if (x > 300 || x < 0) {
speed = -speed;
}
}
let x = 150;
let y = 100;
let dx = 3;
let dy = 2;
function setup() {
createCanvas(300, 200);
}
function draw() {
background(20, 30, 60);
fill(255, 210, 0);
circle(x, y, 40);
x = x + dx;
y = y + dy;
if (x > 300 || x < 0) {
dx = -dx;
}
if (y > 200 || y < 0) {
dy = -dy;
}
}
let ballX = 150;
let ballY = 60;
let dx = 3;
let dy = 3;
function setup() {
createCanvas(300, 200);
}
function draw() {
background(20, 30, 60);
fill(255, 210, 0);
circle(ballX, ballY, 20);
fill(120, 220, 255);
rect(mouseX - 30, 185, 60, 8);
ballX = ballX + dx;
ballY = ballY + dy;
if (ballX > 300 || ballX < 0) {
dx = -dx;
}
if (ballY < 0) {
dy = -dy;
}
if (ballY > 175 && dy > 0 && ballX > mouseX - 30 && ballX < mouseX + 30) {
dy = -dy;
}
if (ballY > 200) {
ballY = 0;
}
}
Stretch: Add a score that goes up on each paddle hit, and make the ball a little faster each time.
Support: Give students the wall checks written and ask only for the paddle.
What is the difference between = and === ?
One = sets a variable to a value; three === asks whether two values are equal.
Next lesson: lists and loops, so one piece of code handles many things.