Game making in JavaScript

Gem Jumper

Make a platform game: run, jump between ledges, mind the water, and collect all five gems against the clock.

Day 6 50 to 60 minutes 7 steps Key Stage 3
Start step 1
gem-jumper/index.html

Every platform game needs the same few ideas: gravity pulls you down, the ground stops you, and a jump launches you up until gravity wins again. Today you will build them all, then add five gems to collect, water to avoid and a clock to beat.

You will learn

  • Make things fall with gravity
  • Jump, and land on platforms
  • Check whether two rectangles overlap
  • Put a whole game together: a goal, a hazard, a timer and a restart

You will need

  • A computer with a web browser and a keyboard
  • The Web Studio, or Notepad
  • Days 1 to 3, for the game loop, keys and velocity

Words to know

gravity
A pull downwards, added to the fall speed every frame.
platform
Something to stand on: here, a rectangle the player lands on from above.
overlap
Two rectangles overlap when they cover some of the same space.
hazard
Something to avoid, such as the water.

Step by step

  1. Step 1: The scene

    Choose Day 6: Gem Jumper. The CSS paints a sky behind the canvas. The platforms are an array of rectangles, each with a grassy green top, and the player is an orange square, for now floating in the air.

    index.htmlHTML
    <!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Gem Jumper</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Gem Jumper</h1>    <canvas id="game" width="480" height="360"></canvas>    <p>Click the game. Arrow keys to run, Up or Space to jump.</p>    <script src="script.js"></script>  </body></html>
    style.cssCSS
    body {  font-family: Arial, sans-serif;  text-align: center;  background: #1b263b;  color: white;} canvas {  background: linear-gradient(#87ceeb, #e0f7ff);  border-radius: 10px;  max-width: 100%;}
    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 480, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }]; function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);} draw();
    gem-jumper/index.html
    The ground, five ledges and the player.
  2. Step 2: Gravity

    Gravity is a speed downwards, vy, that grows a little every frame, so the player falls faster and faster. To land, check every platform: if the player's bottom was above the platform's top last frame and is level with it or below now, while it is across the platform, put the player on top and stop the fall.

    New: lines 15 and 29 to 52.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 480, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5; function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);} function update() {  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }} function loop() {  update();  draw();  requestAnimationFrame(loop);} loop();
    gem-jumper/index.html
    The player falls and lands on the ground.
  3. Step 3: Run and jump

    The arrow keys set a speed across, vx. A jump sets vy to minus 10, a fast speed upwards, and gravity slows it, stops it and brings the player back down: a curve, just like a real jump. Only jump when onGround, or you could fly by pressing jump in mid air. Click the game and try to climb the ledges.

    New: lines 16, 30 to 40 and 42 to 61.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 480, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5;const keys = {}; function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {    event.preventDefault();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function update() {  // run left and right  player.vx = 0;  if (keys.ArrowLeft) {    player.vx = -4;  }  if (keys.ArrowRight) {    player.vx = 4;  }  // jump, but only from the ground  if ((keys.ArrowUp || keys[" "]) && player.onGround) {    player.vy = -10;  }  player.x = player.x + player.vx;  // stay on the screen  if (player.x < 0) {    player.x = 0;  }  if (player.x > canvas.width - player.w) {    player.x = canvas.width - player.w;  }  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }} function loop() {  update();  draw();  requestAnimationFrame(loop);} loop();
    gem-jumper/index.html
    Click the game, then run and jump up the ledges.
  4. Step 4: Gems to collect

    Five gems, one above each ledge. Each frame, check whether the player's square overlaps each gem: two boxes overlap when each one's left edge is left of the other's right edge, and the same for top and bottom. A touched gem is taken. write, as in Balloon Pop, shows the count at the top in dark blue, which stands out on the pale sky.

    New: lines 17 to 36, 46 to 57, 60, 109 to 117 and 126.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 480, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5;const keys = {};let gems = [];let collected = 0; function write(text, x, y, size) {  ctx.fillStyle = "#1b263b";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} function makeGems() {  gems = [    { x: 125, y: 235, taken: false },    { x: 265, y: 185, taken: false },    { x: 405, y: 135, taken: false },    { x: 240, y: 85, taken: false },    { x: 85, y: 65, taken: false }  ];  collected = 0;} function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  for (const g of gems) {    if (!g.taken) {      ctx.fillStyle = "deepskyblue";      ctx.beginPath();      ctx.moveTo(g.x, g.y - 10);      ctx.lineTo(g.x + 8, g.y);      ctx.lineTo(g.x, g.y + 10);      ctx.lineTo(g.x - 8, g.y);      ctx.closePath();      ctx.fill();    }  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);  write("Gems: " + collected + " of " + gems.length, canvas.width / 2, 30, 20);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {    event.preventDefault();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function update() {  // run left and right  player.vx = 0;  if (keys.ArrowLeft) {    player.vx = -4;  }  if (keys.ArrowRight) {    player.vx = 4;  }  // jump, but only from the ground  if ((keys.ArrowUp || keys[" "]) && player.onGround) {    player.vy = -10;  }  player.x = player.x + player.vx;  // stay on the screen  if (player.x < 0) {    player.x = 0;  }  if (player.x > canvas.width - player.w) {    player.x = canvas.width - player.w;  }  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }  // collect any gem the player touches  for (const g of gems) {    const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&                     player.y < g.y + 10 && player.y + player.h > g.y - 10;    if (!g.taken && touching) {      g.taken = true;      collected = collected + 1;    }  }} function loop() {  update();  draw();  requestAnimationFrame(loop);} makeGems();loop();
    gem-jumper/index.html
    Collect the blue gems. The count is at the top.
  5. Step 5: Mind the water

    Split the ground in two with a gap of water between. Falling below the canvas puts the player back at the start.

    New: lines 8, 9, 41 to 43 and 122 to 127.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 200, h: 30 },  { x: 280, y: 330, w: 200, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5;const keys = {};let gems = [];let collected = 0; function write(text, x, y, size) {  ctx.fillStyle = "#1b263b";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} function makeGems() {  gems = [    { x: 125, y: 235, taken: false },    { x: 265, y: 185, taken: false },    { x: 405, y: 135, taken: false },    { x: 240, y: 85, taken: false },    { x: 85, y: 65, taken: false }  ];  collected = 0;} function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  // the water  ctx.fillStyle = "royalblue";  ctx.fillRect(200, 340, 80, 20);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  for (const g of gems) {    if (!g.taken) {      ctx.fillStyle = "deepskyblue";      ctx.beginPath();      ctx.moveTo(g.x, g.y - 10);      ctx.lineTo(g.x + 8, g.y);      ctx.lineTo(g.x, g.y + 10);      ctx.lineTo(g.x - 8, g.y);      ctx.closePath();      ctx.fill();    }  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);  write("Gems: " + collected + " of " + gems.length, canvas.width / 2, 30, 20);} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {    event.preventDefault();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function update() {  // run left and right  player.vx = 0;  if (keys.ArrowLeft) {    player.vx = -4;  }  if (keys.ArrowRight) {    player.vx = 4;  }  // jump, but only from the ground  if ((keys.ArrowUp || keys[" "]) && player.onGround) {    player.vy = -10;  }  player.x = player.x + player.vx;  // stay on the screen  if (player.x < 0) {    player.x = 0;  }  if (player.x > canvas.width - player.w) {    player.x = canvas.width - player.w;  }  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }  // collect any gem the player touches  for (const g of gems) {    const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&                     player.y < g.y + 10 && player.y + player.h > g.y - 10;    if (!g.taken && touching) {      g.taken = true;      collected = collected + 1;    }  }  // fell in the water: back to the start  if (player.y > canvas.height) {    player.x = 30;    player.y = 200;    player.vy = 0;  }} function loop() {  update();  draw();  requestAnimationFrame(loop);} makeGems();loop();
    gem-jumper/index.html
    Fall in the water and you start again.
  6. Step 6: Win against the clock

    Count the frames since the start: sixty frames is one second. When every gem is collected the game stops and shows your time, and R starts again with all the gems back.

    New: lines 20, 21, 66 to 70, 78 to 80, 86 to 94, 146 to 148 and 152 to 155.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 200, h: 30 },  { x: 280, y: 330, w: 200, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5;const keys = {};let gems = [];let collected = 0;let frames = 0;let won = false; function write(text, x, y, size) {  ctx.fillStyle = "#1b263b";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} function makeGems() {  gems = [    { x: 125, y: 235, taken: false },    { x: 265, y: 185, taken: false },    { x: 405, y: 135, taken: false },    { x: 240, y: 85, taken: false },    { x: 85, y: 65, taken: false }  ];  collected = 0;} function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  // the water  ctx.fillStyle = "royalblue";  ctx.fillRect(200, 340, 80, 20);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  for (const g of gems) {    if (!g.taken) {      ctx.fillStyle = "deepskyblue";      ctx.beginPath();      ctx.moveTo(g.x, g.y - 10);      ctx.lineTo(g.x + 8, g.y);      ctx.lineTo(g.x, g.y + 10);      ctx.lineTo(g.x - 8, g.y);      ctx.closePath();      ctx.fill();    }  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);  write("Gems: " + collected + " of " + gems.length + "   Time: " + Math.floor(frames / 60), canvas.width / 2, 30, 20);  if (won) {    write("You got every gem!", canvas.width / 2, 150, 36);    write("in " + Math.floor(frames / 60) + " seconds. Press R to play again.", canvas.width / 2, 185, 18);  }} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {    event.preventDefault();  }  if ((event.key === "r" || event.key === "R") && won) {    restart();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function restart() {  player.x = 30;  player.y = 200;  player.vy = 0;  frames = 0;  won = false;  makeGems();} function update() {  // run left and right  player.vx = 0;  if (keys.ArrowLeft) {    player.vx = -4;  }  if (keys.ArrowRight) {    player.vx = 4;  }  // jump, but only from the ground  if ((keys.ArrowUp || keys[" "]) && player.onGround) {    player.vy = -10;  }  player.x = player.x + player.vx;  // stay on the screen  if (player.x < 0) {    player.x = 0;  }  if (player.x > canvas.width - player.w) {    player.x = canvas.width - player.w;  }  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }  // collect any gem the player touches  for (const g of gems) {    const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&                     player.y < g.y + 10 && player.y + player.h > g.y - 10;    if (!g.taken && touching) {      g.taken = true;      collected = collected + 1;    }  }  // fell in the water: back to the start  if (player.y > canvas.height) {    player.x = 30;    player.y = 200;    player.vy = 0;  }  if (collected === gems.length) {    won = true;  }} function loop() {  if (!won) {    update();    frames = frames + 1;  }  draw();  requestAnimationFrame(loop);} makeGems();loop();
    gem-jumper/index.html
    Collect all five gems as fast as you can, then press R to try again.
  7. Step 7: Give it a face

    Finally, some character. facing remembers which way the player last ran, 1 for right and minus 1 for left, and the eyes look that way.

    New: lines 4, 66 to 72, 108 and 112.

    script.jsJavaScript
    const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false, facing: 1 }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 200, h: 30 },  { x: 280, y: 330, w: 200, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5;const keys = {};let gems = [];let collected = 0;let frames = 0;let won = false; function write(text, x, y, size) {  ctx.fillStyle = "#1b263b";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} function makeGems() {  gems = [    { x: 125, y: 235, taken: false },    { x: 265, y: 185, taken: false },    { x: 405, y: 135, taken: false },    { x: 240, y: 85, taken: false },    { x: 85, y: 65, taken: false }  ];  collected = 0;} function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  // the water  ctx.fillStyle = "royalblue";  ctx.fillRect(200, 340, 80, 20);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  for (const g of gems) {    if (!g.taken) {      ctx.fillStyle = "deepskyblue";      ctx.beginPath();      ctx.moveTo(g.x, g.y - 10);      ctx.lineTo(g.x + 8, g.y);      ctx.lineTo(g.x, g.y + 10);      ctx.lineTo(g.x - 8, g.y);      ctx.closePath();      ctx.fill();    }  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);  // eyes that look the way the player is facing  ctx.fillStyle = "white";  ctx.fillRect(player.x + 5, player.y + 6, 6, 7);  ctx.fillRect(player.x + 13, player.y + 6, 6, 7);  ctx.fillStyle = "black";  ctx.fillRect(player.x + 7 + player.facing, player.y + 8, 3, 4);  ctx.fillRect(player.x + 15 + player.facing, player.y + 8, 3, 4);  write("Gems: " + collected + " of " + gems.length + "   Time: " + Math.floor(frames / 60), canvas.width / 2, 30, 20);  if (won) {    write("You got every gem!", canvas.width / 2, 150, 36);    write("in " + Math.floor(frames / 60) + " seconds. Press R to play again.", canvas.width / 2, 185, 18);  }} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {    event.preventDefault();  }  if ((event.key === "r" || event.key === "R") && won) {    restart();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function restart() {  player.x = 30;  player.y = 200;  player.vy = 0;  frames = 0;  won = false;  makeGems();} function update() {  // run left and right  player.vx = 0;  if (keys.ArrowLeft) {    player.vx = -4;    player.facing = -1;  }  if (keys.ArrowRight) {    player.vx = 4;    player.facing = 1;  }  // jump, but only from the ground  if ((keys.ArrowUp || keys[" "]) && player.onGround) {    player.vy = -10;  }  player.x = player.x + player.vx;  // stay on the screen  if (player.x < 0) {    player.x = 0;  }  if (player.x > canvas.width - player.w) {    player.x = canvas.width - player.w;  }  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }  // collect any gem the player touches  for (const g of gems) {    const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&                     player.y < g.y + 10 && player.y + player.h > g.y - 10;    if (!g.taken && touching) {      g.taken = true;      collected = collected + 1;    }  }  // fell in the water: back to the start  if (player.y > canvas.height) {    player.x = 30;    player.y = 200;    player.vy = 0;  }  if (collected === gems.length) {    won = true;  }} function loop() {  if (!won) {    update();    frames = frames + 1;  }  draw();  requestAnimationFrame(loop);} makeGems();loop();
    gem-jumper/index.html
    The finished Gem Jumper. Every gem, as fast as you can!

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. Double jump

    Let the player jump once more in mid air.

    Show a hint

    Count jumps since the player last landed. Allow a jump when onGround or the count is 1, and reset the count on landing. Use keydown for jumping, so holding the key does not use both jumps at once.

  2. A moving platform

    Make one ledge slide left and right.

    Show a hint

    Give it a dx. Each frame, add dx to its x and turn it round at the ends. A player standing on it should move by dx too.

  3. A bigger world

    Make the level wider than the screen, with the view following the player.

    Show a hint

    Keep a cameraX. Draw everything at x - cameraX, and set cameraX so the player stays near the middle of the screen.

The finished code

Show the whole game
index.htmlHTML
<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Gem Jumper</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Gem Jumper</h1>    <canvas id="game" width="480" height="360"></canvas>    <p>Click the game. Arrow keys to run, Up or Space to jump.</p>    <script src="script.js"></script>  </body></html>
style.cssCSS
body {  font-family: Arial, sans-serif;  text-align: center;  background: #1b263b;  color: white;} canvas {  background: linear-gradient(#87ceeb, #e0f7ff);  border-radius: 10px;  max-width: 100%;}
script.jsJavaScript
const canvas = document.getElementById("game");const ctx = canvas.getContext("2d"); const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false, facing: 1 }; // each platform is a rectangle: the x and y of its top left corner, its width and its heightconst platforms = [  { x: 0, y: 330, w: 200, h: 30 },  { x: 280, y: 330, w: 200, h: 30 },  { x: 80, y: 260, w: 90, h: 14 },  { x: 220, y: 210, w: 90, h: 14 },  { x: 360, y: 160, w: 90, h: 14 },  { x: 200, y: 110, w: 80, h: 14 },  { x: 40, y: 90, w: 90, h: 14 }];const GRAVITY = 0.5;const keys = {};let gems = [];let collected = 0;let frames = 0;let won = false; function write(text, x, y, size) {  ctx.fillStyle = "#1b263b";  ctx.font = "bold " + size + "px Arial";  ctx.textAlign = "center";  ctx.fillText(text, x, y);} function makeGems() {  gems = [    { x: 125, y: 235, taken: false },    { x: 265, y: 185, taken: false },    { x: 405, y: 135, taken: false },    { x: 240, y: 85, taken: false },    { x: 85, y: 65, taken: false }  ];  collected = 0;} function draw() {  ctx.clearRect(0, 0, canvas.width, canvas.height);  // the water  ctx.fillStyle = "royalblue";  ctx.fillRect(200, 340, 80, 20);  for (const p of platforms) {    ctx.fillStyle = "sienna";    ctx.fillRect(p.x, p.y, p.w, p.h);    ctx.fillStyle = "limegreen";    ctx.fillRect(p.x, p.y, p.w, 5);  }  for (const g of gems) {    if (!g.taken) {      ctx.fillStyle = "deepskyblue";      ctx.beginPath();      ctx.moveTo(g.x, g.y - 10);      ctx.lineTo(g.x + 8, g.y);      ctx.lineTo(g.x, g.y + 10);      ctx.lineTo(g.x - 8, g.y);      ctx.closePath();      ctx.fill();    }  }  ctx.fillStyle = "orange";  ctx.fillRect(player.x, player.y, player.w, player.h);  // eyes that look the way the player is facing  ctx.fillStyle = "white";  ctx.fillRect(player.x + 5, player.y + 6, 6, 7);  ctx.fillRect(player.x + 13, player.y + 6, 6, 7);  ctx.fillStyle = "black";  ctx.fillRect(player.x + 7 + player.facing, player.y + 8, 3, 4);  ctx.fillRect(player.x + 15 + player.facing, player.y + 8, 3, 4);  write("Gems: " + collected + " of " + gems.length + "   Time: " + Math.floor(frames / 60), canvas.width / 2, 30, 20);  if (won) {    write("You got every gem!", canvas.width / 2, 150, 36);    write("in " + Math.floor(frames / 60) + " seconds. Press R to play again.", canvas.width / 2, 185, 18);  }} document.addEventListener("keydown", function (event) {  keys[event.key] = true;  if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {    event.preventDefault();  }  if ((event.key === "r" || event.key === "R") && won) {    restart();  }}); document.addEventListener("keyup", function (event) {  keys[event.key] = false;}); function restart() {  player.x = 30;  player.y = 200;  player.vy = 0;  frames = 0;  won = false;  makeGems();} function update() {  // run left and right  player.vx = 0;  if (keys.ArrowLeft) {    player.vx = -4;    player.facing = -1;  }  if (keys.ArrowRight) {    player.vx = 4;    player.facing = 1;  }  // jump, but only from the ground  if ((keys.ArrowUp || keys[" "]) && player.onGround) {    player.vy = -10;  }  player.x = player.x + player.vx;  // stay on the screen  if (player.x < 0) {    player.x = 0;  }  if (player.x > canvas.width - player.w) {    player.x = canvas.width - player.w;  }  // gravity pulls down a little more every frame  player.vy = player.vy + GRAVITY;  const oldBottom = player.y + player.h;  player.y = player.y + player.vy;  // land on a platform if we were above it and have now gone through its top  player.onGround = false;  for (const p of platforms) {    const across = player.x + player.w > p.x && player.x < p.x + p.w;    if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {      player.y = p.y - player.h;      player.vy = 0;      player.onGround = true;    }  }  // collect any gem the player touches  for (const g of gems) {    const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&                     player.y < g.y + 10 && player.y + player.h > g.y - 10;    if (!g.taken && touching) {      g.taken = true;      collected = collected + 1;    }  }  // fell in the water: back to the start  if (player.y > canvas.height) {    player.x = 30;    player.y = 200;    player.vy = 0;  }  if (collected === gems.length) {    won = true;  }} function loop() {  if (!won) {    update();    frames = frames + 1;  }  draw();  requestAnimationFrame(loop);} makeGems();loop();
Notes for the teacher

Timing

  1. 10 minutes: step 1.
  2. 20 minutes: steps 2 and 3. Gravity and jumping are the big ideas: sketch the jump's curve on the board, frame by frame.
  3. 20 minutes: steps 4 to 7, then speedrun the level.

Mistakes to look out for

  1. The player falls through platforms: the landing check needs oldBottom, the bottom before this frame's move. Checking only the new position misses platforms at high speed.
  2. Infinite jumping: the jump must check player.onGround.
  3. player.vy = 10 instead of -10: on a canvas, up is negative.
  4. Gems that cannot be collected: check every gem can be reached from a platform.

Going further

These few lines are a tiny physics engine. Changing GRAVITY and the jump speed changes how the whole game feels: let students tune them and discuss which settings are the most fun.