Game making in JavaScript

Meteor Dodge

Steer a spaceship with the arrow keys and dodge a storm of meteors that gets faster the longer you last.

Day 2 45 to 60 minutes 7 steps Key Stage 3
Start step 1
meteor-dodge/index.html

Meteors rain down on your ship. Slide left and right to dodge them: three hits and you are done. The longer you survive, the higher your score, and the harder the storm gets. Today's new idea is the keyboard: the game remembers which keys are held down, so the ship glides smoothly while you hold an arrow.

You will learn

  • Read the keyboard with key events
  • Move a player and keep it on the screen
  • Spot a collision between two things
  • Make a game harder as it goes on

You will need

  • A computer with a web browser and a keyboard
  • The Web Studio, or Notepad
  • Day 1, for the canvas and the game loop

Words to know

event
Something that happens, such as a key going down, that code can respond to.
collision
Two things on the screen touching, which the game has to notice.
difficulty curve
How a game gets harder as you play.
frame
One picture of the game. The game loop draws about sixty frames every second.

Step by step

  1. Step 1: A sky full of stars

    Choose Day 2: Meteor Dodge in the Web Studio. The page has a black canvas. The script fills an array with 60 stars, each with a random place and size, and drawStars draws each one as a tiny square with fillRect.

    index.htmlHTML
    <!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Meteor Dodge</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Meteor Dodge</h1>    <canvas id="game" width="480" height="360"></canvas>    <p>Click the game, then use the arrow keys.</p>    <script src="script.js"></script>  </body></html>
    style.cssCSS
    body {  font-family: Arial, sans-serif;  text-align: center;  background: #10002b;  color: white;} canvas {  background: black;  border: 3px solid #7b2cbf;  border-radius: 10px;  max-width: 100%;}
    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });} function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} drawStars();
    meteor-dodge/index.html
    A starry sky.
  2. Step 2: Your ship

    The ship is a triangle, drawn as a path: moveTo the nose, lineTo each back corner, then closePath and fill. Its position is in the ship object, so moving it later is just changing ship.x.

    New: lines 13, 22 to 31 and 33.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 }; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} drawStars();drawShip();
    meteor-dodge/index.html
    A blue ship at the bottom of the sky.
  3. Step 3: Steer with the arrow keys

    keys remembers which keys are down. keydown sets a key to true and keyup sets it back to false. Then the game loop moves the ship while an arrow is held, unless it is already at the edge. preventDefault stops the arrow keys scrolling the page. Click the game, then try it.

    New: lines 14 and 33 to 57.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 };const keys = {}; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight") {    event.preventDefault();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function loop() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  drawStars();  if (keys.ArrowLeft && ship.x > 20) {    ship.x = ship.x - ship.speed;  }  if (keys.ArrowRight && ship.x < canvas.width - 20) {    ship.x = ship.x + ship.speed;  }  drawShip();  requestAnimationFrame(loop);} loop();
    meteor-dodge/index.html
    Click the game, then hold the arrow keys to steer.
    Watch out Key names are written exactly: "ArrowLeft", with a capital A and a capital L.
  4. Step 4: Here come the meteors

    Every 30 frames, half a second, a meteor starts just above the top with a random place, size and speed. Each frame they all fall, and the ones that have fallen off the bottom are filtered out, just like Day 1's balloons.

    New: lines 15, 16, 32 to 42 and 66 to 87.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 };const keys = {};let meteors = [];let frames = 0; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} function drawMeteor(m) {  ctx.fillStyle = "sienna";  ctx.beginPath();  ctx.arc(m.x, m.y, m.r, 0, Math.PI * 2);  ctx.fill();  ctx.fillStyle = "saddlebrown";  ctx.beginPath();  ctx.arc(m.x - m.r / 3, m.y - m.r / 4, m.r / 4, 0, Math.PI * 2);  ctx.fill();} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight") {    event.preventDefault();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function loop() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  drawStars();  if (keys.ArrowLeft && ship.x > 20) {    ship.x = ship.x - ship.speed;  }  if (keys.ArrowRight && ship.x < canvas.width - 20) {    ship.x = ship.x + ship.speed;  }  frames = frames + 1;  if (frames % 30 === 0) {    meteors.push({      x: Math.random() * canvas.width,      y: -20,      r: Math.random() * 12 + 10,      speed: Math.random() * 2 + 2,      gone: false    });  }  for (const m of meteors) {    m.y = m.y + m.speed;    if (m.y > canvas.height + 30) {      m.gone = true;    }  }  meteors = meteors.filter(function (m) {    return !m.gone;  });  for (const m of meteors) {    drawMeteor(m);  }  drawShip();  requestAnimationFrame(loop);} loop();
    meteor-dodge/index.html
    Meteors fall. They pass straight through the ship, for now.
  5. Step 5: Crash!

    A meteor hits the ship if the distance between their middles is less than the meteor's radius plus 14, about the size of the ship. Then it is removed and you lose a life. The score goes up by 1 every 10 frames you survive, and write, like the one in Balloon Pop but in white, shows the score and lives at the top.

    New: lines 17, 18, 46 to 52, 90 to 96 and 105 to 108.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 };const keys = {};let meteors = [];let frames = 0;let score = 0;let lives = 3; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} function drawMeteor(m) {  ctx.fillStyle = "sienna";  ctx.beginPath();  ctx.arc(m.x, m.y, m.r, 0, Math.PI * 2);  ctx.fill();  ctx.fillStyle = "saddlebrown";  ctx.beginPath();  ctx.arc(m.x - m.r / 3, m.y - m.r / 4, m.r / 4, 0, Math.PI * 2);  ctx.fill();} function write(text, x, y, size) {  ctx.fillStyle = "white";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight") {    event.preventDefault();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function loop() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  drawStars();  if (keys.ArrowLeft && ship.x > 20) {    ship.x = ship.x - ship.speed;  }  if (keys.ArrowRight && ship.x < canvas.width - 20) {    ship.x = ship.x + ship.speed;  }  frames = frames + 1;  if (frames % 30 === 0) {    meteors.push({      x: Math.random() * canvas.width,      y: -20,      r: Math.random() * 12 + 10,      speed: Math.random() * 2 + 2,      gone: false    });  }  for (const m of meteors) {    m.y = m.y + m.speed;    if (m.y > canvas.height + 30) {      m.gone = true;    }    // has it hit the ship?    const dx = m.x - ship.x;    const dy = m.y - ship.y;    if (Math.sqrt(dx * dx + dy * dy) < m.r + 14) {      m.gone = true;      lives = lives - 1;    }  }  meteors = meteors.filter(function (m) {    return !m.gone;  });  for (const m of meteors) {    drawMeteor(m);  }  drawShip();  if (frames % 10 === 0) {    score = score + 1;  }  write("Score: " + score + "   Lives: " + lives, canvas.width / 2, 28, 20);  requestAnimationFrame(loop);} loop();
    meteor-dodge/index.html
    Hits cost lives, and the score counts up as you survive.
  6. Step 6: Game over, and play again

    When the lives run out, the loop writes Game over and stops. start resets everything, so a click or Space starts a new game. The game now opens on a start screen and waits for you.

    New: lines 19, 58, 60 to 62, 69 to 84, 129 to 134 and 138 to 142.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 };const keys = {};let meteors = [];let frames = 0;let score = 0;let lives = 3;let playing = false; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} function drawMeteor(m) {  ctx.fillStyle = "sienna";  ctx.beginPath();  ctx.arc(m.x, m.y, m.r, 0, Math.PI * 2);  ctx.fill();  ctx.fillStyle = "saddlebrown";  ctx.beginPath();  ctx.arc(m.x - m.r / 3, m.y - m.r / 4, m.r / 4, 0, Math.PI * 2);  ctx.fill();} function write(text, x, y, size) {  ctx.fillStyle = "white";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === " ") {    event.preventDefault();  }  if (event.key === " " && !playing) {    start();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); canvas.addEventListener("click", function () {  if (!playing) {    start();  }}); function start() {  meteors = [];  frames = 0;  score = 0;  lives = 3;  ship.x = 240;  playing = true;  loop();} function loop() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  drawStars();  if (keys.ArrowLeft && ship.x > 20) {    ship.x = ship.x - ship.speed;  }  if (keys.ArrowRight && ship.x < canvas.width - 20) {    ship.x = ship.x + ship.speed;  }  frames = frames + 1;  if (frames % 30 === 0) {    meteors.push({      x: Math.random() * canvas.width,      y: -20,      r: Math.random() * 12 + 10,      speed: Math.random() * 2 + 2,      gone: false    });  }  for (const m of meteors) {    m.y = m.y + m.speed;    if (m.y > canvas.height + 30) {      m.gone = true;    }    // has it hit the ship?    const dx = m.x - ship.x;    const dy = m.y - ship.y;    if (Math.sqrt(dx * dx + dy * dy) < m.r + 14) {      m.gone = true;      lives = lives - 1;    }  }  meteors = meteors.filter(function (m) {    return !m.gone;  });  for (const m of meteors) {    drawMeteor(m);  }  drawShip();  if (frames % 10 === 0) {    score = score + 1;  }  write("Score: " + score + "   Lives: " + lives, canvas.width / 2, 28, 20);  if (lives <= 0) {    playing = false;    write("Game over!", canvas.width / 2, 170, 44);    write("Click or press Space to play again", canvas.width / 2, 210, 18);    return;  }  requestAnimationFrame(loop);} // the start screendrawStars();drawShip();write("Meteor Dodge", canvas.width / 2, 160, 44);write("Click or press Space to start", canvas.width / 2, 200, 18);
    meteor-dodge/index.html
    Click or press Space to start, then dodge!
  7. Step 7: Harder and harder

    Make the storm grow. The gap between meteors shrinks from 30 frames to as few as 8 as the score rises, and every meteor falls faster. best remembers your best score until the page is closed.

    New: lines 20, 97 to 99, 104, 131, 134 to 136, 138 and 139.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 };const keys = {};let meteors = [];let frames = 0;let score = 0;let lives = 3;let playing = false;let best = 0; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} function drawMeteor(m) {  ctx.fillStyle = "sienna";  ctx.beginPath();  ctx.arc(m.x, m.y, m.r, 0, Math.PI * 2);  ctx.fill();  ctx.fillStyle = "saddlebrown";  ctx.beginPath();  ctx.arc(m.x - m.r / 3, m.y - m.r / 4, m.r / 4, 0, Math.PI * 2);  ctx.fill();} function write(text, x, y, size) {  ctx.fillStyle = "white";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === " ") {    event.preventDefault();  }  if (event.key === " " && !playing) {    start();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); canvas.addEventListener("click", function () {  if (!playing) {    start();  }}); function start() {  meteors = [];  frames = 0;  score = 0;  lives = 3;  ship.x = 240;  playing = true;  loop();} function loop() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  drawStars();  if (keys.ArrowLeft && ship.x > 20) {    ship.x = ship.x - ship.speed;  }  if (keys.ArrowRight && ship.x < canvas.width - 20) {    ship.x = ship.x + ship.speed;  }  frames = frames + 1;  // meteors come faster and more often as the score goes up  const gap = Math.max(8, 30 - Math.floor(score / 10));  if (frames % gap === 0) {    meteors.push({      x: Math.random() * canvas.width,      y: -20,      r: Math.random() * 12 + 10,      speed: Math.random() * 2 + 2 + score / 50,      gone: false    });  }  for (const m of meteors) {    m.y = m.y + m.speed;    if (m.y > canvas.height + 30) {      m.gone = true;    }    // has it hit the ship?    const dx = m.x - ship.x;    const dy = m.y - ship.y;    if (Math.sqrt(dx * dx + dy * dy) < m.r + 14) {      m.gone = true;      lives = lives - 1;    }  }  meteors = meteors.filter(function (m) {    return !m.gone;  });  for (const m of meteors) {    drawMeteor(m);  }  drawShip();  if (frames % 10 === 0) {    score = score + 1;  }  write("Score: " + score + "   Best: " + best + "   Lives: " + lives, canvas.width / 2, 28, 20);  if (lives <= 0) {    playing = false;    if (score > best) {      best = score;    }    write("Game over!", canvas.width / 2, 170, 44);    write("Best score: " + best, canvas.width / 2, 210, 22);    write("Click or press Space to play again", canvas.width / 2, 245, 18);    return;  }  requestAnimationFrame(loop);} // the start screendrawStars();drawShip();write("Meteor Dodge", canvas.width / 2, 160, 44);write("Click or press Space to start", canvas.width / 2, 200, 18);
    meteor-dodge/index.html
    The finished Meteor Dodge. How long can you last?

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Up and down

    Let the ship move up and down too, but only in the bottom half of the sky.

    Show a hint

    Copy the ArrowLeft code for "ArrowUp" and "ArrowDown", changing ship.y. Keep ship.y between 180 and 340.

  2. Shield

    Collect a power up that makes the ship safe from hits for five seconds.

    Show a hint

    Make a falling green circle like a meteor. Catching it sets shield = 300 (frames). Each frame, take 1 off, and only lose a life when shield is 0. Draw a ring round the ship while it is on.

  3. Explosions

    Draw a quick orange flash where a meteor hits.

    Show a hint

    Push { x, y, life: 20 } into an array of flashes on a hit. Each frame, draw each flash as a circle that grows as its life runs out, and filter out the ones that are finished.

The finished code

Show the whole game
index.htmlHTML
<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Meteor Dodge</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Meteor Dodge</h1>    <canvas id="game" width="480" height="360"></canvas>    <p>Click the game, then use the arrow keys.</p>    <script src="script.js"></script>  </body></html>
style.cssCSS
body {  font-family: Arial, sans-serif;  text-align: center;  background: #10002b;  color: white;} canvas {  background: black;  border: 3px solid #7b2cbf;  border-radius: 10px;  max-width: 100%;}
script.jsJavaScript
const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); // 60 stars, each at a random placeconst stars = [];for (let i = 0; i < 60; i++) {  stars.push({    x: Math.random() * canvas.width,    y: Math.random() * canvas.height,    size: Math.random() * 2 + 1  });}const ship = { x: 240, y: 320, speed: 5 };const keys = {};let meteors = [];let frames = 0;let score = 0;let lives = 3;let playing = false;let best = 0; function drawStars() {  ctx.fillStyle = "white";  for (const s of stars) {    ctx.fillRect(s.x, s.y, s.size, s.size);  }} function drawShip() {  ctx.fillStyle = "deepskyblue";  ctx.beginPath();  ctx.moveTo(ship.x, ship.y - 20);  ctx.lineTo(ship.x - 16, ship.y + 14);  ctx.lineTo(ship.x + 16, ship.y + 14);  ctx.closePath();  ctx.fill();} function drawMeteor(m) {  ctx.fillStyle = "sienna";  ctx.beginPath();  ctx.arc(m.x, m.y, m.r, 0, Math.PI * 2);  ctx.fill();  ctx.fillStyle = "saddlebrown";  ctx.beginPath();  ctx.arc(m.x - m.r / 3, m.y - m.r / 4, m.r / 4, 0, Math.PI * 2);  ctx.fill();} function write(text, x, y, size) {  ctx.fillStyle = "white";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === " ") {    event.preventDefault();  }  if (event.key === " " && !playing) {    start();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); canvas.addEventListener("click", function () {  if (!playing) {    start();  }}); function start() {  meteors = [];  frames = 0;  score = 0;  lives = 3;  ship.x = 240;  playing = true;  loop();} function loop() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  drawStars();  if (keys.ArrowLeft && ship.x > 20) {    ship.x = ship.x - ship.speed;  }  if (keys.ArrowRight && ship.x < canvas.width - 20) {    ship.x = ship.x + ship.speed;  }  frames = frames + 1;  // meteors come faster and more often as the score goes up  const gap = Math.max(8, 30 - Math.floor(score / 10));  if (frames % gap === 0) {    meteors.push({      x: Math.random() * canvas.width,      y: -20,      r: Math.random() * 12 + 10,      speed: Math.random() * 2 + 2 + score / 50,      gone: false    });  }  for (const m of meteors) {    m.y = m.y + m.speed;    if (m.y > canvas.height + 30) {      m.gone = true;    }    // has it hit the ship?    const dx = m.x - ship.x;    const dy = m.y - ship.y;    if (Math.sqrt(dx * dx + dy * dy) < m.r + 14) {      m.gone = true;      lives = lives - 1;    }  }  meteors = meteors.filter(function (m) {    return !m.gone;  });  for (const m of meteors) {    drawMeteor(m);  }  drawShip();  if (frames % 10 === 0) {    score = score + 1;  }  write("Score: " + score + "   Best: " + best + "   Lives: " + lives, canvas.width / 2, 28, 20);  if (lives <= 0) {    playing = false;    if (score > best) {      best = score;    }    write("Game over!", canvas.width / 2, 170, 44);    write("Best score: " + best, canvas.width / 2, 210, 22);    write("Click or press Space to play again", canvas.width / 2, 245, 18);    return;  }  requestAnimationFrame(loop);} // the start screendrawStars();drawShip();write("Meteor Dodge", canvas.width / 2, 160, 44);write("Click or press Space to start", canvas.width / 2, 200, 18);
Notes for the teacher

Timing

  1. 10 minutes: steps 1 and 2.
  2. 15 minutes: step 3. Talk through why the game remembers held keys instead of moving once per key press: try it the other way and the ship stutters.
  3. 25 minutes: steps 4 to 7.

Mistakes to look out for

  1. The arrow keys do nothing: the game must be clicked first, so it hears the keyboard.
  2. "arrowleft" or "Left" instead of "ArrowLeft".
  3. Lives that vanish all at once: the meteor is not marked gone after it hits, so it hits again on every frame it overlaps.
  4. frames % gap with a gap that is not a whole number never equals 0. Math.floor keeps it whole.

Going further

Collision detection is a central idea in games programming. Circles are the easiest case: compare the distance between centres with the sum of the radii.