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.
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
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.
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Gem Jumper</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1>Gem Jumper</h1>10 <canvas id="game" width="480" height="360"></canvas>11 <p>Click the game. Arrow keys to run, Up or Space to jump.</p>12 <script src="script.js"></script>13 </body>14</html>1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #1b263b;5 color: white;6}7 8canvas {9 background: linear-gradient(#87ceeb, #e0f7ff);10 border-radius: 10px;11 max-width: 100%;12}1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 480, h: 30 },9 { x: 80, y: 260, w: 90, h: 14 },10 { x: 220, y: 210, w: 90, h: 14 },11 { x: 360, y: 160, w: 90, h: 14 },12 { x: 200, y: 110, w: 80, h: 14 },13 { x: 40, y: 90, w: 90, h: 14 }14];15 16function draw() {17 ctx.clearRect(0, 0, canvas.width, canvas.height);18 for (const p of platforms) {19 ctx.fillStyle = "sienna";20 ctx.fillRect(p.x, p.y, p.w, p.h);21 ctx.fillStyle = "limegreen";22 ctx.fillRect(p.x, p.y, p.w, 5);23 }24 ctx.fillStyle = "orange";25 ctx.fillRect(player.x, player.y, player.w, player.h);26}27 28draw();The ground, five ledges and the player. 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.
1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 480, h: 30 },9 { x: 80, y: 260, w: 90, h: 14 },10 { x: 220, y: 210, w: 90, h: 14 },11 { x: 360, y: 160, w: 90, h: 14 },12 { x: 200, y: 110, w: 80, h: 14 },13 { x: 40, y: 90, w: 90, h: 14 }14];15const GRAVITY = 0.5;16 17function draw() {18 ctx.clearRect(0, 0, canvas.width, canvas.height);19 for (const p of platforms) {20 ctx.fillStyle = "sienna";21 ctx.fillRect(p.x, p.y, p.w, p.h);22 ctx.fillStyle = "limegreen";23 ctx.fillRect(p.x, p.y, p.w, 5);24 }25 ctx.fillStyle = "orange";26 ctx.fillRect(player.x, player.y, player.w, player.h);27}28 29function update() {30 // gravity pulls down a little more every frame31 player.vy = player.vy + GRAVITY;32 const oldBottom = player.y + player.h;33 player.y = player.y + player.vy;34 // land on a platform if we were above it and have now gone through its top35 player.onGround = false;36 for (const p of platforms) {37 const across = player.x + player.w > p.x && player.x < p.x + p.w;38 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {39 player.y = p.y - player.h;40 player.vy = 0;41 player.onGround = true;42 }43 }44}45 46function loop() {47 update();48 draw();49 requestAnimationFrame(loop);50}51 52loop();The player falls and lands on the ground. Step 3: Run and jump
The arrow keys set a speed across,
vx. A jump setsvyto 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 whenonGround, 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.
1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 480, h: 30 },9 { x: 80, y: 260, w: 90, h: 14 },10 { x: 220, y: 210, w: 90, h: 14 },11 { x: 360, y: 160, w: 90, h: 14 },12 { x: 200, y: 110, w: 80, h: 14 },13 { x: 40, y: 90, w: 90, h: 14 }14];15const GRAVITY = 0.5;16const keys = {};17 18function draw() {19 ctx.clearRect(0, 0, canvas.width, canvas.height);20 for (const p of platforms) {21 ctx.fillStyle = "sienna";22 ctx.fillRect(p.x, p.y, p.w, p.h);23 ctx.fillStyle = "limegreen";24 ctx.fillRect(p.x, p.y, p.w, 5);25 }26 ctx.fillStyle = "orange";27 ctx.fillRect(player.x, player.y, player.w, player.h);28}29 30document.addEventListener("keydown", function (event) {31 keys[event.key] = true;32 if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {33 event.preventDefault();34 }35});36 37document.addEventListener("keyup", function (event) {38 keys[event.key] = false;39});40 41function update() {42 // run left and right43 player.vx = 0;44 if (keys.ArrowLeft) {45 player.vx = -4;46 }47 if (keys.ArrowRight) {48 player.vx = 4;49 }50 // jump, but only from the ground51 if ((keys.ArrowUp || keys[" "]) && player.onGround) {52 player.vy = -10;53 }54 player.x = player.x + player.vx;55 // stay on the screen56 if (player.x < 0) {57 player.x = 0;58 }59 if (player.x > canvas.width - player.w) {60 player.x = canvas.width - player.w;61 }62 // gravity pulls down a little more every frame63 player.vy = player.vy + GRAVITY;64 const oldBottom = player.y + player.h;65 player.y = player.y + player.vy;66 // land on a platform if we were above it and have now gone through its top67 player.onGround = false;68 for (const p of platforms) {69 const across = player.x + player.w > p.x && player.x < p.x + p.w;70 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {71 player.y = p.y - player.h;72 player.vy = 0;73 player.onGround = true;74 }75 }76}77 78function loop() {79 update();80 draw();81 requestAnimationFrame(loop);82}83 84loop();Click the game, then run and jump up the ledges. 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.
1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 480, h: 30 },9 { x: 80, y: 260, w: 90, h: 14 },10 { x: 220, y: 210, w: 90, h: 14 },11 { x: 360, y: 160, w: 90, h: 14 },12 { x: 200, y: 110, w: 80, h: 14 },13 { x: 40, y: 90, w: 90, h: 14 }14];15const GRAVITY = 0.5;16const keys = {};17let gems = [];18let collected = 0;19 20function write(text, x, y, size) {21 ctx.fillStyle = "#1b263b";22 ctx.font = "bold " + size + "px Arial";23 ctx.textAlign = "center";24 ctx.fillText(text, x, y);25}26 27function makeGems() {28 gems = [29 { x: 125, y: 235, taken: false },30 { x: 265, y: 185, taken: false },31 { x: 405, y: 135, taken: false },32 { x: 240, y: 85, taken: false },33 { x: 85, y: 65, taken: false }34 ];35 collected = 0;36}37 38function draw() {39 ctx.clearRect(0, 0, canvas.width, canvas.height);40 for (const p of platforms) {41 ctx.fillStyle = "sienna";42 ctx.fillRect(p.x, p.y, p.w, p.h);43 ctx.fillStyle = "limegreen";44 ctx.fillRect(p.x, p.y, p.w, 5);45 }46 for (const g of gems) {47 if (!g.taken) {48 ctx.fillStyle = "deepskyblue";49 ctx.beginPath();50 ctx.moveTo(g.x, g.y - 10);51 ctx.lineTo(g.x + 8, g.y);52 ctx.lineTo(g.x, g.y + 10);53 ctx.lineTo(g.x - 8, g.y);54 ctx.closePath();55 ctx.fill();56 }57 }58 ctx.fillStyle = "orange";59 ctx.fillRect(player.x, player.y, player.w, player.h);60 write("Gems: " + collected + " of " + gems.length, canvas.width / 2, 30, 20);61}62 63document.addEventListener("keydown", function (event) {64 keys[event.key] = true;65 if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {66 event.preventDefault();67 }68});69 70document.addEventListener("keyup", function (event) {71 keys[event.key] = false;72});73 74function update() {75 // run left and right76 player.vx = 0;77 if (keys.ArrowLeft) {78 player.vx = -4;79 }80 if (keys.ArrowRight) {81 player.vx = 4;82 }83 // jump, but only from the ground84 if ((keys.ArrowUp || keys[" "]) && player.onGround) {85 player.vy = -10;86 }87 player.x = player.x + player.vx;88 // stay on the screen89 if (player.x < 0) {90 player.x = 0;91 }92 if (player.x > canvas.width - player.w) {93 player.x = canvas.width - player.w;94 }95 // gravity pulls down a little more every frame96 player.vy = player.vy + GRAVITY;97 const oldBottom = player.y + player.h;98 player.y = player.y + player.vy;99 // land on a platform if we were above it and have now gone through its top100 player.onGround = false;101 for (const p of platforms) {102 const across = player.x + player.w > p.x && player.x < p.x + p.w;103 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {104 player.y = p.y - player.h;105 player.vy = 0;106 player.onGround = true;107 }108 }109 // collect any gem the player touches110 for (const g of gems) {111 const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&112 player.y < g.y + 10 && player.y + player.h > g.y - 10;113 if (!g.taken && touching) {114 g.taken = true;115 collected = collected + 1;116 }117 }118}119 120function loop() {121 update();122 draw();123 requestAnimationFrame(loop);124}125 126makeGems();127loop();Collect the blue gems. The count is at the top. 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.
1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 200, h: 30 },9 { x: 280, y: 330, w: 200, h: 30 },10 { x: 80, y: 260, w: 90, h: 14 },11 { x: 220, y: 210, w: 90, h: 14 },12 { x: 360, y: 160, w: 90, h: 14 },13 { x: 200, y: 110, w: 80, h: 14 },14 { x: 40, y: 90, w: 90, h: 14 }15];16const GRAVITY = 0.5;17const keys = {};18let gems = [];19let collected = 0;20 21function write(text, x, y, size) {22 ctx.fillStyle = "#1b263b";23 ctx.font = "bold " + size + "px Arial";24 ctx.textAlign = "center";25 ctx.fillText(text, x, y);26}27 28function makeGems() {29 gems = [30 { x: 125, y: 235, taken: false },31 { x: 265, y: 185, taken: false },32 { x: 405, y: 135, taken: false },33 { x: 240, y: 85, taken: false },34 { x: 85, y: 65, taken: false }35 ];36 collected = 0;37}38 39function draw() {40 ctx.clearRect(0, 0, canvas.width, canvas.height);41 // the water42 ctx.fillStyle = "royalblue";43 ctx.fillRect(200, 340, 80, 20);44 for (const p of platforms) {45 ctx.fillStyle = "sienna";46 ctx.fillRect(p.x, p.y, p.w, p.h);47 ctx.fillStyle = "limegreen";48 ctx.fillRect(p.x, p.y, p.w, 5);49 }50 for (const g of gems) {51 if (!g.taken) {52 ctx.fillStyle = "deepskyblue";53 ctx.beginPath();54 ctx.moveTo(g.x, g.y - 10);55 ctx.lineTo(g.x + 8, g.y);56 ctx.lineTo(g.x, g.y + 10);57 ctx.lineTo(g.x - 8, g.y);58 ctx.closePath();59 ctx.fill();60 }61 }62 ctx.fillStyle = "orange";63 ctx.fillRect(player.x, player.y, player.w, player.h);64 write("Gems: " + collected + " of " + gems.length, canvas.width / 2, 30, 20);65}66 67document.addEventListener("keydown", function (event) {68 keys[event.key] = true;69 if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {70 event.preventDefault();71 }72});73 74document.addEventListener("keyup", function (event) {75 keys[event.key] = false;76});77 78function update() {79 // run left and right80 player.vx = 0;81 if (keys.ArrowLeft) {82 player.vx = -4;83 }84 if (keys.ArrowRight) {85 player.vx = 4;86 }87 // jump, but only from the ground88 if ((keys.ArrowUp || keys[" "]) && player.onGround) {89 player.vy = -10;90 }91 player.x = player.x + player.vx;92 // stay on the screen93 if (player.x < 0) {94 player.x = 0;95 }96 if (player.x > canvas.width - player.w) {97 player.x = canvas.width - player.w;98 }99 // gravity pulls down a little more every frame100 player.vy = player.vy + GRAVITY;101 const oldBottom = player.y + player.h;102 player.y = player.y + player.vy;103 // land on a platform if we were above it and have now gone through its top104 player.onGround = false;105 for (const p of platforms) {106 const across = player.x + player.w > p.x && player.x < p.x + p.w;107 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {108 player.y = p.y - player.h;109 player.vy = 0;110 player.onGround = true;111 }112 }113 // collect any gem the player touches114 for (const g of gems) {115 const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&116 player.y < g.y + 10 && player.y + player.h > g.y - 10;117 if (!g.taken && touching) {118 g.taken = true;119 collected = collected + 1;120 }121 }122 // fell in the water: back to the start123 if (player.y > canvas.height) {124 player.x = 30;125 player.y = 200;126 player.vy = 0;127 }128}129 130function loop() {131 update();132 draw();133 requestAnimationFrame(loop);134}135 136makeGems();137loop();Fall in the water and you start again. 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.
1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 200, h: 30 },9 { x: 280, y: 330, w: 200, h: 30 },10 { x: 80, y: 260, w: 90, h: 14 },11 { x: 220, y: 210, w: 90, h: 14 },12 { x: 360, y: 160, w: 90, h: 14 },13 { x: 200, y: 110, w: 80, h: 14 },14 { x: 40, y: 90, w: 90, h: 14 }15];16const GRAVITY = 0.5;17const keys = {};18let gems = [];19let collected = 0;20let frames = 0;21let won = false;22 23function write(text, x, y, size) {24 ctx.fillStyle = "#1b263b";25 ctx.font = "bold " + size + "px Arial";26 ctx.textAlign = "center";27 ctx.fillText(text, x, y);28}29 30function makeGems() {31 gems = [32 { x: 125, y: 235, taken: false },33 { x: 265, y: 185, taken: false },34 { x: 405, y: 135, taken: false },35 { x: 240, y: 85, taken: false },36 { x: 85, y: 65, taken: false }37 ];38 collected = 0;39}40 41function draw() {42 ctx.clearRect(0, 0, canvas.width, canvas.height);43 // the water44 ctx.fillStyle = "royalblue";45 ctx.fillRect(200, 340, 80, 20);46 for (const p of platforms) {47 ctx.fillStyle = "sienna";48 ctx.fillRect(p.x, p.y, p.w, p.h);49 ctx.fillStyle = "limegreen";50 ctx.fillRect(p.x, p.y, p.w, 5);51 }52 for (const g of gems) {53 if (!g.taken) {54 ctx.fillStyle = "deepskyblue";55 ctx.beginPath();56 ctx.moveTo(g.x, g.y - 10);57 ctx.lineTo(g.x + 8, g.y);58 ctx.lineTo(g.x, g.y + 10);59 ctx.lineTo(g.x - 8, g.y);60 ctx.closePath();61 ctx.fill();62 }63 }64 ctx.fillStyle = "orange";65 ctx.fillRect(player.x, player.y, player.w, player.h);66 write("Gems: " + collected + " of " + gems.length + " Time: " + Math.floor(frames / 60), canvas.width / 2, 30, 20);67 if (won) {68 write("You got every gem!", canvas.width / 2, 150, 36);69 write("in " + Math.floor(frames / 60) + " seconds. Press R to play again.", canvas.width / 2, 185, 18);70 }71}72 73document.addEventListener("keydown", function (event) {74 keys[event.key] = true;75 if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {76 event.preventDefault();77 }78 if ((event.key === "r" || event.key === "R") && won) {79 restart();80 }81});82 83document.addEventListener("keyup", function (event) {84 keys[event.key] = false;85});86 87function restart() {88 player.x = 30;89 player.y = 200;90 player.vy = 0;91 frames = 0;92 won = false;93 makeGems();94}95 96function update() {97 // run left and right98 player.vx = 0;99 if (keys.ArrowLeft) {100 player.vx = -4;101 }102 if (keys.ArrowRight) {103 player.vx = 4;104 }105 // jump, but only from the ground106 if ((keys.ArrowUp || keys[" "]) && player.onGround) {107 player.vy = -10;108 }109 player.x = player.x + player.vx;110 // stay on the screen111 if (player.x < 0) {112 player.x = 0;113 }114 if (player.x > canvas.width - player.w) {115 player.x = canvas.width - player.w;116 }117 // gravity pulls down a little more every frame118 player.vy = player.vy + GRAVITY;119 const oldBottom = player.y + player.h;120 player.y = player.y + player.vy;121 // land on a platform if we were above it and have now gone through its top122 player.onGround = false;123 for (const p of platforms) {124 const across = player.x + player.w > p.x && player.x < p.x + p.w;125 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {126 player.y = p.y - player.h;127 player.vy = 0;128 player.onGround = true;129 }130 }131 // collect any gem the player touches132 for (const g of gems) {133 const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&134 player.y < g.y + 10 && player.y + player.h > g.y - 10;135 if (!g.taken && touching) {136 g.taken = true;137 collected = collected + 1;138 }139 }140 // fell in the water: back to the start141 if (player.y > canvas.height) {142 player.x = 30;143 player.y = 200;144 player.vy = 0;145 }146 if (collected === gems.length) {147 won = true;148 }149}150 151function loop() {152 if (!won) {153 update();154 frames = frames + 1;155 }156 draw();157 requestAnimationFrame(loop);158}159 160makeGems();161loop();Collect all five gems as fast as you can, then press R to try again. Step 7: Give it a face
Finally, some character.
facingremembers 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.
1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false, facing: 1 };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 200, h: 30 },9 { x: 280, y: 330, w: 200, h: 30 },10 { x: 80, y: 260, w: 90, h: 14 },11 { x: 220, y: 210, w: 90, h: 14 },12 { x: 360, y: 160, w: 90, h: 14 },13 { x: 200, y: 110, w: 80, h: 14 },14 { x: 40, y: 90, w: 90, h: 14 }15];16const GRAVITY = 0.5;17const keys = {};18let gems = [];19let collected = 0;20let frames = 0;21let won = false;22 23function write(text, x, y, size) {24 ctx.fillStyle = "#1b263b";25 ctx.font = "bold " + size + "px Arial";26 ctx.textAlign = "center";27 ctx.fillText(text, x, y);28}29 30function makeGems() {31 gems = [32 { x: 125, y: 235, taken: false },33 { x: 265, y: 185, taken: false },34 { x: 405, y: 135, taken: false },35 { x: 240, y: 85, taken: false },36 { x: 85, y: 65, taken: false }37 ];38 collected = 0;39}40 41function draw() {42 ctx.clearRect(0, 0, canvas.width, canvas.height);43 // the water44 ctx.fillStyle = "royalblue";45 ctx.fillRect(200, 340, 80, 20);46 for (const p of platforms) {47 ctx.fillStyle = "sienna";48 ctx.fillRect(p.x, p.y, p.w, p.h);49 ctx.fillStyle = "limegreen";50 ctx.fillRect(p.x, p.y, p.w, 5);51 }52 for (const g of gems) {53 if (!g.taken) {54 ctx.fillStyle = "deepskyblue";55 ctx.beginPath();56 ctx.moveTo(g.x, g.y - 10);57 ctx.lineTo(g.x + 8, g.y);58 ctx.lineTo(g.x, g.y + 10);59 ctx.lineTo(g.x - 8, g.y);60 ctx.closePath();61 ctx.fill();62 }63 }64 ctx.fillStyle = "orange";65 ctx.fillRect(player.x, player.y, player.w, player.h);66 // eyes that look the way the player is facing67 ctx.fillStyle = "white";68 ctx.fillRect(player.x + 5, player.y + 6, 6, 7);69 ctx.fillRect(player.x + 13, player.y + 6, 6, 7);70 ctx.fillStyle = "black";71 ctx.fillRect(player.x + 7 + player.facing, player.y + 8, 3, 4);72 ctx.fillRect(player.x + 15 + player.facing, player.y + 8, 3, 4);73 write("Gems: " + collected + " of " + gems.length + " Time: " + Math.floor(frames / 60), canvas.width / 2, 30, 20);74 if (won) {75 write("You got every gem!", canvas.width / 2, 150, 36);76 write("in " + Math.floor(frames / 60) + " seconds. Press R to play again.", canvas.width / 2, 185, 18);77 }78}79 80document.addEventListener("keydown", function (event) {81 keys[event.key] = true;82 if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {83 event.preventDefault();84 }85 if ((event.key === "r" || event.key === "R") && won) {86 restart();87 }88});89 90document.addEventListener("keyup", function (event) {91 keys[event.key] = false;92});93 94function restart() {95 player.x = 30;96 player.y = 200;97 player.vy = 0;98 frames = 0;99 won = false;100 makeGems();101}102 103function update() {104 // run left and right105 player.vx = 0;106 if (keys.ArrowLeft) {107 player.vx = -4;108 player.facing = -1;109 }110 if (keys.ArrowRight) {111 player.vx = 4;112 player.facing = 1;113 }114 // jump, but only from the ground115 if ((keys.ArrowUp || keys[" "]) && player.onGround) {116 player.vy = -10;117 }118 player.x = player.x + player.vx;119 // stay on the screen120 if (player.x < 0) {121 player.x = 0;122 }123 if (player.x > canvas.width - player.w) {124 player.x = canvas.width - player.w;125 }126 // gravity pulls down a little more every frame127 player.vy = player.vy + GRAVITY;128 const oldBottom = player.y + player.h;129 player.y = player.y + player.vy;130 // land on a platform if we were above it and have now gone through its top131 player.onGround = false;132 for (const p of platforms) {133 const across = player.x + player.w > p.x && player.x < p.x + p.w;134 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {135 player.y = p.y - player.h;136 player.vy = 0;137 player.onGround = true;138 }139 }140 // collect any gem the player touches141 for (const g of gems) {142 const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&143 player.y < g.y + 10 && player.y + player.h > g.y - 10;144 if (!g.taken && touching) {145 g.taken = true;146 collected = collected + 1;147 }148 }149 // fell in the water: back to the start150 if (player.y > canvas.height) {151 player.x = 30;152 player.y = 200;153 player.vy = 0;154 }155 if (collected === gems.length) {156 won = true;157 }158}159 160function loop() {161 if (!won) {162 update();163 frames = frames + 1;164 }165 draw();166 requestAnimationFrame(loop);167}168 169makeGems();170loop();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.
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
onGroundor the count is 1, and reset the count on landing. Usekeydownfor jumping, so holding the key does not use both jumps at once.A moving platform
Make one ledge slide left and right.
Show a hint
Give it a
dx. Each frame, adddxto itsxand turn it round at the ends. A player standing on it should move bydxtoo.A bigger world
Make the level wider than the screen, with the view following the player.
Show a hint
Keep a
cameraX. Draw everything atx - cameraX, and setcameraXso the player stays near the middle of the screen.
The finished code
Show the whole game
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Gem Jumper</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1>Gem Jumper</h1>10 <canvas id="game" width="480" height="360"></canvas>11 <p>Click the game. Arrow keys to run, Up or Space to jump.</p>12 <script src="script.js"></script>13 </body>14</html>1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #1b263b;5 color: white;6}7 8canvas {9 background: linear-gradient(#87ceeb, #e0f7ff);10 border-radius: 10px;11 max-width: 100%;12}1const canvas = document.getElementById("game");2const ctx = canvas.getContext("2d");3 4const player = { x: 30, y: 200, w: 24, h: 24, vx: 0, vy: 0, onGround: false, facing: 1 };5 6// each platform is a rectangle: the x and y of its top left corner, its width and its height7const platforms = [8 { x: 0, y: 330, w: 200, h: 30 },9 { x: 280, y: 330, w: 200, h: 30 },10 { x: 80, y: 260, w: 90, h: 14 },11 { x: 220, y: 210, w: 90, h: 14 },12 { x: 360, y: 160, w: 90, h: 14 },13 { x: 200, y: 110, w: 80, h: 14 },14 { x: 40, y: 90, w: 90, h: 14 }15];16const GRAVITY = 0.5;17const keys = {};18let gems = [];19let collected = 0;20let frames = 0;21let won = false;22 23function write(text, x, y, size) {24 ctx.fillStyle = "#1b263b";25 ctx.font = "bold " + size + "px Arial";26 ctx.textAlign = "center";27 ctx.fillText(text, x, y);28}29 30function makeGems() {31 gems = [32 { x: 125, y: 235, taken: false },33 { x: 265, y: 185, taken: false },34 { x: 405, y: 135, taken: false },35 { x: 240, y: 85, taken: false },36 { x: 85, y: 65, taken: false }37 ];38 collected = 0;39}40 41function draw() {42 ctx.clearRect(0, 0, canvas.width, canvas.height);43 // the water44 ctx.fillStyle = "royalblue";45 ctx.fillRect(200, 340, 80, 20);46 for (const p of platforms) {47 ctx.fillStyle = "sienna";48 ctx.fillRect(p.x, p.y, p.w, p.h);49 ctx.fillStyle = "limegreen";50 ctx.fillRect(p.x, p.y, p.w, 5);51 }52 for (const g of gems) {53 if (!g.taken) {54 ctx.fillStyle = "deepskyblue";55 ctx.beginPath();56 ctx.moveTo(g.x, g.y - 10);57 ctx.lineTo(g.x + 8, g.y);58 ctx.lineTo(g.x, g.y + 10);59 ctx.lineTo(g.x - 8, g.y);60 ctx.closePath();61 ctx.fill();62 }63 }64 ctx.fillStyle = "orange";65 ctx.fillRect(player.x, player.y, player.w, player.h);66 // eyes that look the way the player is facing67 ctx.fillStyle = "white";68 ctx.fillRect(player.x + 5, player.y + 6, 6, 7);69 ctx.fillRect(player.x + 13, player.y + 6, 6, 7);70 ctx.fillStyle = "black";71 ctx.fillRect(player.x + 7 + player.facing, player.y + 8, 3, 4);72 ctx.fillRect(player.x + 15 + player.facing, player.y + 8, 3, 4);73 write("Gems: " + collected + " of " + gems.length + " Time: " + Math.floor(frames / 60), canvas.width / 2, 30, 20);74 if (won) {75 write("You got every gem!", canvas.width / 2, 150, 36);76 write("in " + Math.floor(frames / 60) + " seconds. Press R to play again.", canvas.width / 2, 185, 18);77 }78}79 80document.addEventListener("keydown", function (event) {81 keys[event.key] = true;82 if (event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "ArrowUp" || event.key === " ") {83 event.preventDefault();84 }85 if ((event.key === "r" || event.key === "R") && won) {86 restart();87 }88});89 90document.addEventListener("keyup", function (event) {91 keys[event.key] = false;92});93 94function restart() {95 player.x = 30;96 player.y = 200;97 player.vy = 0;98 frames = 0;99 won = false;100 makeGems();101}102 103function update() {104 // run left and right105 player.vx = 0;106 if (keys.ArrowLeft) {107 player.vx = -4;108 player.facing = -1;109 }110 if (keys.ArrowRight) {111 player.vx = 4;112 player.facing = 1;113 }114 // jump, but only from the ground115 if ((keys.ArrowUp || keys[" "]) && player.onGround) {116 player.vy = -10;117 }118 player.x = player.x + player.vx;119 // stay on the screen120 if (player.x < 0) {121 player.x = 0;122 }123 if (player.x > canvas.width - player.w) {124 player.x = canvas.width - player.w;125 }126 // gravity pulls down a little more every frame127 player.vy = player.vy + GRAVITY;128 const oldBottom = player.y + player.h;129 player.y = player.y + player.vy;130 // land on a platform if we were above it and have now gone through its top131 player.onGround = false;132 for (const p of platforms) {133 const across = player.x + player.w > p.x && player.x < p.x + p.w;134 if (across && player.vy >= 0 && oldBottom <= p.y && player.y + player.h >= p.y) {135 player.y = p.y - player.h;136 player.vy = 0;137 player.onGround = true;138 }139 }140 // collect any gem the player touches141 for (const g of gems) {142 const touching = player.x < g.x + 8 && player.x + player.w > g.x - 8 &&143 player.y < g.y + 10 && player.y + player.h > g.y - 10;144 if (!g.taken && touching) {145 g.taken = true;146 collected = collected + 1;147 }148 }149 // fell in the water: back to the start150 if (player.y > canvas.height) {151 player.x = 30;152 player.y = 200;153 player.vy = 0;154 }155 if (collected === gems.length) {156 won = true;157 }158}159 160function loop() {161 if (!won) {162 update();163 frames = frames + 1;164 }165 draw();166 requestAnimationFrame(loop);167}168 169makeGems();170loop();Notes for the teacher
Timing
- 10 minutes: step 1.
- 20 minutes: steps 2 and 3. Gravity and jumping are the big ideas: sketch the jump's curve on the board, frame by frame.
- 20 minutes: steps 4 to 7, then speedrun the level.
Mistakes to look out for
- 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. - Infinite jumping: the jump must check
player.onGround. player.vy = 10instead of-10: on a canvas, up is negative.- 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.