๐ ๏ธ Section project: the paddle game
Section 9: Canvas: drawing and gamesLesson 9.3 ยท โฑ 15 min
๐ฏ Project goal
A ball falls, and the paddle moves with the arrows to hit it back. Every hit is a point, and if the ball falls it starts again.
- We keep the keys being pressed in
keys: a key becomes true on keydown and false on keyup, so the movement is smooth. Math.maxandMath.minstop the paddle going out of the canvas.- A hit: the ball is at the paddle's height, between its ends, and going down.
HTML
<p id="score">Points: 0</p>
<canvas id="c" width="320" height="220"></canvas>
<p>Move the paddle with the arrows โฌ
๏ธ โก๏ธ</p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
canvas { background: #14213d; border-radius: 12px; }
#score { font-size: 20px; font-weight: bold; margin: 4px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let ball = { x: 160, y: 30, dx: 2, dy: 3 };
let paddle = 120; // left side of the paddle (80 wide)
let score = 0;
const keys = {};
document.addEventListener("keydown", e => keys[e.key] = true);
document.addEventListener("keyup", e => keys[e.key] = false);
function frame() {
if (keys.ArrowLeft) paddle = Math.max(0, paddle - 6);
if (keys.ArrowRight) paddle = Math.min(240, paddle + 6);
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x < 10 || ball.x > 310) ball.dx = -ball.dx;
if (ball.y < 10) ball.dy = -ball.dy;
// the paddle catches the ball
if (ball.y > 195 && ball.y < 205 && ball.x > paddle && ball.x < paddle + 80 && ball.dy > 0) {
ball.dy = -ball.dy;
score++;
document.getElementById("score").textContent = "Points: " + score;
}
// missed: start again from the top
if (ball.y > 230) { ball = { x: 160, y: 30, dx: 2, dy: 3 }; }
ctx.clearRect(0, 0, 320, 220);
ctx.fillStyle = "#ffd23f";
ctx.beginPath();
ctx.arc(ball.x, ball.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#4cc9f0";
ctx.fillRect(paddle, 205, 80, 10);
requestAnimationFrame(frame);
}
frame();
๐ฅ๏ธ The page in Robo Web:

๐ฅ๏ธ What happens on the stage?
We played in the lab for nine seconds, the paddle hit the ball back and the points went up.
- Make the ball a little faster after every 3 points.
- Add โTries: 3โ, and the game ends when it reaches zero.
๐ Another example: the paddle with hearts
Three hearts: every ball you miss takes a heart, and at zero the game ends.
HTML
<p id="score">Points: 0</p>
<canvas id="c" width="320" height="220"></canvas>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
canvas { background: #14213d; border-radius: 12px; }
#score { font-size: 20px; font-weight: bold; margin: 4px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let ball = { x: 160, y: 30, dx: 2, dy: 3 };
let paddle = 120; // left side of the paddle (80 wide)
let score = 0;
let lives = 3;
const keys = {};
document.addEventListener("keydown", e => keys[e.key] = true);
document.addEventListener("keyup", e => keys[e.key] = false);
function frame() {
if (lives === 0) return;
if (keys.ArrowLeft) paddle = Math.max(0, paddle - 6);
if (keys.ArrowRight) paddle = Math.min(240, paddle + 6);
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x < 10 || ball.x > 310) ball.dx = -ball.dx;
if (ball.y < 10) ball.dy = -ball.dy;
if (ball.y > 195 && ball.y < 205 && ball.x > paddle && ball.x < paddle + 80 && ball.dy > 0) {
ball.dy = -ball.dy;
score++;
document.getElementById("score").textContent = "Points: " + score + " โค๏ธ " + lives;
}
if (ball.y > 230 && lives > 0) {
lives--;
document.getElementById("score").textContent = lives > 0 ? "Points: " + score + " โค๏ธ " + lives : "Game over ๐ Points: " + score;
if (lives === 0) ball.dy = 0;
ball = { x: 160, y: 30, dx: 2, dy: 3 };
}
ctx.clearRect(0, 0, 320, 220);
ctx.fillStyle = "#ffd23f";
ctx.beginPath();
ctx.arc(ball.x, ball.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#4cc9f0";
ctx.fillRect(paddle, 205, 80, 10);
requestAnimationFrame(frame);
}
frame();
๐ฅ๏ธ The page in Robo Web:

โ๏ธ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
Change the ball's colour to red and the paddle's to green.
๐ Show the solution๐ Hide the solution
HTML
<p id="score">Points: 0</p>
<canvas id="c" width="320" height="220"></canvas>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
canvas { background: #14213d; border-radius: 12px; }
#score { font-size: 20px; font-weight: bold; margin: 4px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let ball = { x: 160, y: 30, dx: 2, dy: 3 };
let paddle = 120; // left side of the paddle (80 wide)
let score = 0;
const keys = {};
document.addEventListener("keydown", e => keys[e.key] = true);
document.addEventListener("keyup", e => keys[e.key] = false);
function frame() {
if (keys.ArrowLeft) paddle = Math.max(0, paddle - 6);
if (keys.ArrowRight) paddle = Math.min(240, paddle + 6);
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x < 10 || ball.x > 310) ball.dx = -ball.dx;
if (ball.y < 10) ball.dy = -ball.dy;
if (ball.y > 195 && ball.y < 205 && ball.x > paddle && ball.x < paddle + 80 && ball.dy > 0) {
ball.dy = -ball.dy;
score++;
document.getElementById("score").textContent = "Points: " + score;
}
if (ball.y > 230) {
ball = { x: 160, y: 30, dx: 2, dy: 3 };
}
ctx.clearRect(0, 0, 320, 220);
ctx.fillStyle = "red";
ctx.beginPath();
ctx.arc(ball.x, ball.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "lime";
ctx.fillRect(paddle, 205, 80, 10);
requestAnimationFrame(frame);
}
frame();
๐ฅ๏ธ The page in Robo Web:

Result: a red ball and a green paddle.
Medium
Every 3 points the ball gets faster.
๐ Show the solution๐ Hide the solution
HTML
<p id="score">Points: 0</p>
<canvas id="c" width="320" height="220"></canvas>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
canvas { background: #14213d; border-radius: 12px; }
#score { font-size: 20px; font-weight: bold; margin: 4px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let ball = { x: 160, y: 30, dx: 2, dy: 3 };
let paddle = 120; // left side of the paddle (80 wide)
let score = 0;
const keys = {};
document.addEventListener("keydown", e => keys[e.key] = true);
document.addEventListener("keyup", e => keys[e.key] = false);
function frame() {
if (keys.ArrowLeft) paddle = Math.max(0, paddle - 6);
if (keys.ArrowRight) paddle = Math.min(240, paddle + 6);
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x < 10 || ball.x > 310) ball.dx = -ball.dx;
if (ball.y < 10) ball.dy = -ball.dy;
if (ball.y > 195 && ball.y < 205 && ball.x > paddle && ball.x < paddle + 80 && ball.dy > 0) {
ball.dy = -ball.dy;
score++;
if (score % 3 === 0) ball.dy = ball.dy * 1.3;
document.getElementById("score").textContent = "Points: " + score;
}
if (ball.y > 230) {
ball = { x: 160, y: 30, dx: 2, dy: 3 };
}
ctx.clearRect(0, 0, 320, 220);
ctx.fillStyle = "#ffd23f";
ctx.beginPath();
ctx.arc(ball.x, ball.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#4cc9f0";
ctx.fillRect(paddle, 205, 80, 10);
requestAnimationFrame(frame);
}
frame();
๐ฅ๏ธ The page in Robo Web:

Result: faster after 3 points.
Hard
Best score: it stays after the ball is missed.
๐ Show the solution๐ Hide the solution
HTML
<p id="score">Points: 0</p>
<canvas id="c" width="320" height="220"></canvas>
<p>Best score: <b id="best">0</b></p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
canvas { background: #14213d; border-radius: 12px; }
#score { font-size: 20px; font-weight: bold; margin: 4px; }
JavaScript
const ctx = document.getElementById("c").getContext("2d");
let ball = { x: 160, y: 30, dx: 2, dy: 3 };
let paddle = 120; // left side of the paddle (80 wide)
let score = 0;
let best = 0;
const keys = {};
document.addEventListener("keydown", e => keys[e.key] = true);
document.addEventListener("keyup", e => keys[e.key] = false);
function frame() {
if (keys.ArrowLeft) paddle = Math.max(0, paddle - 6);
if (keys.ArrowRight) paddle = Math.min(240, paddle + 6);
ball.x += ball.dx;
ball.y += ball.dy;
if (ball.x < 10 || ball.x > 310) ball.dx = -ball.dx;
if (ball.y < 10) ball.dy = -ball.dy;
if (ball.y > 195 && ball.y < 205 && ball.x > paddle && ball.x < paddle + 80 && ball.dy > 0) {
ball.dy = -ball.dy;
score++;
if (score > best) { best = score; document.getElementById("best").textContent = best; }
document.getElementById("score").textContent = "Points: " + score;
}
if (ball.y > 230) {
score = 0;
document.getElementById("score").textContent = "Points: 0";
ball = { x: 160, y: 30, dx: 2, dy: 3 };
}
ctx.clearRect(0, 0, 320, 220);
ctx.fillStyle = "#ffd23f";
ctx.beginPath();
ctx.arc(ball.x, ball.y, 10, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = "#4cc9f0";
ctx.fillRect(paddle, 205, 80, 10);
requestAnimationFrame(frame);
}
frame();
๐ฅ๏ธ The page in Robo Web:

Result: the best score stays.

