ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
Robo EgyptRobo Egypt
โ˜ฐ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

๐Ÿ› ๏ธ 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.max and Math.min stop 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: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.
  1. Make the ball a little faster after every 3 points.
  2. 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.

๐ŸŒ The paddle with hearts
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: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 lessonor 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
๐ŸŒ 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:The page in Robo Web

Result: a red ball and a green paddle.

Medium

Every 3 points the ball gets faster.

๐Ÿ‘€ Show the solution
๐ŸŒ 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:The page in Robo Web

Result: faster after 3 points.

Hard

Best score: it stays after the ball is missed.

๐Ÿ‘€ Show the solution
๐ŸŒ 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:The page in Robo Web

Result: the best score stays.

Finished the lesson? Mark it so your progress counts.