
Making a small game is one of the most motivating ways to practise programming: you see every change immediately, and friends can play by opening a link. A browser game needs only three things: an HTML page, a canvas element to draw on and JavaScript to make it move. Here you build a simple original game, Seed Catcher: a basket at the bottom of the screen catches falling seeds for points and dodges falling stones. If you are new to the language, it helps to learn the JavaScript basics first.
For years, most web games ran on Adobe Flash, a separate browser plug-in. Apple introduced the canvas element in 2004, and it became part of the HTML5 standard, finalised in 2014. Browsers added requestAnimationFrame for smooth animation and APIs for sound and gamepads. Flash support ended in 2020, and today browser games use plain web technology that works on desktops, tablets and phones.
In index.html, add a canvas and load your script at the end of the body:
<canvas id="game" width="480" height="320"></canvas>
<script src="game.js"></script>
In game.js, get the drawing context, which holds the drawing commands:
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
Coordinates start at the top-left corner: x grows to the right and y grows downwards. Try ctx.fillStyle = "green" and ctx.fillRect(200, 290, 80, 20) to draw the basket as a rectangle.
Every game repeats three jobs many times per second: read input, update the state and draw. requestAnimationFrame calls your function before each screen refresh.
Screens refresh at 60, 120 or more frames per second. The loop receives a timestamp in milliseconds; subtract the previous one and divide by 1000 to get dt, the seconds since the last frame, then move objects by speed * dt. The game now runs at the same speed everywhere.
Do not move the basket inside the key event, because key repeat is slow and uneven. Remember which keys are held and move it in the update step:
const keys = {};
addEventListener("keydown", e => keys[e.key] = true);
addEventListener("keyup", e => keys[e.key] = false);
In the update, check keys["ArrowLeft"] and keys["ArrowRight"], and use Math.max and Math.min to keep the basket inside the canvas. For phones, add pointer events such as pointermove for touch and mouse.
Most simple 2D games use axis-aligned bounding boxes: you treat each object as a rectangle and check whether two rectangles overlap. Two boxes collide when one starts before the other ends on both axes: a.x < b.x + b.w && a.x + a.w > b.x, plus the same test for y and the height. A caught seed adds a point and is removed from the array; a stone ends the game. Remove items that fall off the bottom too, or the array grows forever.
A state variable such as "start", "playing" or "over" tells the loop whether to show a title screen, run the game or show the final score.
Your game is just static files, so you can deploy it for free on services such as GitHub Pages, Netlify or itch.io. Test it on a phone and in several browsers first. Next, try a second small game with a new idea, such as jumping, or a framework like Phaser, which handles sprites, physics and sound. Game jams, events where people build a game around a theme in a weekend, are a friendly way to get feedback.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions