Build this website with blocks

Try another way to build: snap together HTML, CSS and behavior blocks, change their fields, and watch your website update. Start with this lessonโ€™s project or try a code example below.

Game Design & Development ยท Lesson 2

From Scratch: Build a Browser Game

HTML5 Canvas Game Loop Input Handling Collision Vanilla JavaScript + Scratch Path Beginner Friendly

Before engines and libraries, every game was written from scratch. Understanding the foundations (the game loop, canvas drawing, input, and collision) makes you a better developer regardless of which tools you use later. Every section below shows the idea two ways: in typed JavaScript, and in Scratch's drag-and-drop blocks, so build it in whichever one fits you, or both.

Build alongside the lesson

Download a runnable canvas game with WHAT / WHY / TRY THIS comments, checkpoints, challenges, troubleshooting, and a finished core reference.

Download Browser Game Pack

๐ŸŽจ Scratch's Block Categories, at a Glance

Every block in Scratch lives in one of nine color-coded categories down the left side of the editor. You don't need to memorize this table. Skim it once so the colors mean something, then come back to it whenever a "๐Ÿงฉ The Same Idea in Scratch" box below mentions a category by name.

Motion

Moves a sprite around the Stage: position, direction, gliding.

move (10) steps go to x: () y: () glide (1) secs to x y
JS: player.x += speed

Looks

Controls how a sprite appears: costume, size, speech, visibility.

switch costume to () say () for (2) secs show / hide
JS: ctx.fillRect(...) / ctx.drawImage(...)

Sound

Plays and mixes audio: sound effects, notes, volume.

play sound () until done start sound () change volume by ()
JS: new Audio('hit.mp3').play()

Events

Rounded "hat" blocks that start a script when something happens.

when ๐Ÿณ๏ธ clicked when key () pressed when I receive ()
JS: addEventListener('keydown', fn)

Control

Directs the flow of a script: loops, conditionals, waiting, cloning.

forever if () then wait (1) seconds
JS: while / if / setTimeout

Sensing

Hexagonal Boolean blocks that ask true/false questions about the world.

touching ()? key () pressed? ask () and wait
JS: collides(a, b) / keys['ArrowUp']

Operators

Hexagonal math, comparison, string, and randomness blocks that plug into other blocks.

() + () () < () pick random () to ()
JS: + - * /, Math.random()

Variables

Custom variables and lists you create to store data: score, lives, names.

set () to () change () by () add () to (list)
JS: let score = 0; score++;

My Blocks

Custom blocks you define yourself, to reuse a group of blocks under one name.

define spawnEnemy
JS: function spawnEnemy() { ... }
Where the game loop lives: almost every category shows up somewhere in the dodge game: Events starts it, Control repeats it, Sensing checks input and collisions, Motion/Variables update state, and Looks/Sound give the player feedback. That's the whole architecture of a game, in nine colors.

๐Ÿงญ Before You Dive In

This lesson has more code on the page than Lesson 1 did, and that can feel like a lot at once. You don't need to memorize any of it. Read a section once, then type the code yourself (or drag the matching blocks in Scratch) and watch it run. Getting something on screen is most of the learning; the rest comes from tinkering with it afterward.

If something breaks the first time, that's completely normal. It happens to professional developers every day. Reading the error message and fixing one small thing at a time is the skill, not a sign you're doing it wrong.

Two meanings of "Scratch" in this lesson: building a game "from scratch" means writing every line yourself, with no libraries or engines. That's the JavaScript path below. Scratch (capital S) is a specific, free tool from MIT where you build games by snapping together colored blocks instead of typing. They're two different things that happen to share a name. This lesson teaches the same ideas in both.
What you need: for the JavaScript path, a code editor (VS Code works great) and a web browser. For the Scratch path, just a free account at scratch.mit.edu. Neither path requires installing a game engine. Budget 45โ€“60 minutes.

1๐Ÿงฑ Anatomy of a Browser Game

A browser game is just three files working together:

๐Ÿ“„ index.html Creates the canvas element and loads your scripts. The stage on which the game lives.
๐ŸŽจ style.css Optional. Makes the page look good around the canvas: background color, no scrollbars, centered layout.
โšก game.js Where all the logic lives: the game loop, player, enemies, score, and collision detection.
<!-- index.html: the bare minimum -->
<canvas id="gameCanvas" width="480" height="320"></canvas>
<script src="game.js"></script>

๐Ÿงฉ The Same Idea in Scratch

Scratch skips the file setup entirely. Open a new project and you already have a Stage (the canvas), a default Sprite (the player), and a script area next to it: the equivalent of index.html and game.js already wired together for you.

โœ… Checkpoint before moving on: you should be able to name the three files (or Scratch equivalents) and say what each one is responsible for. If you can, you're ready for Step 2: the part that makes any of this move.

2๐Ÿ” The Game Loop

Think of a flipbook: each page looks a little different from the last, and flipping through them fast tricks your eye into seeing motion. Every game, from Pong to Cyberpunk 2077, uses the exact same trick, just automated by the browser. That repeating cycle is called a game loop, and it fires about 60 times per second, doing two jobs each time:

Update Move objects
Check input
Resolve physics
โ†’
Render Clear canvas
Draw everything
Show to player
โ†
Wait ~16ms
(60 fps)
repeatโ€ฆ
function gameLoop() {
  update();  // move everything, check input, collisions
  render();  // draw the current frame to the canvas
  requestAnimationFrame(gameLoop); // call again next frame
}
requestAnimationFrame(gameLoop); // start the loop

requestAnimationFrame is the browser's built-in game loop. It syncs to your monitor's refresh rate (usually 60 fps) and pauses automatically when you switch tabs, saving battery and CPU.

๐Ÿงฉ The Same Idea in Scratch

Live blocks: drag them, click to select, or reveal the JS they translate to.

"Green flag clicked" is Scratch's requestAnimationFrame(gameLoop) โ€” the signal that starts everything. "Forever" is the loop itself: everything snapped inside it runs again and again, exactly like update() and render() firing every frame in the JS version. Scratch just hides the ~16ms timing math from you.

The loop you just wrote, the keys you will read in Step 4 and the overlap test you will write in Step 5 exist in every engine, under different names:

โœ… Checkpoint: type the gameLoop function into a blank game.js and confirm it doesn't crash (it won't draw anything yet; that's Step 3). If you're on the Scratch path, drag the block above into a real project and click the green flag.

3๐Ÿ–Œ๏ธ Drawing with Canvas

Picture a whiteboard that gets wiped clean and redrawn from scratch every single frame. That's the Canvas API. It hands JavaScript a blank surface and lets you control every pixel. The one rule that trips people up at first: clear it, then redraw everything, every single frame.

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

function render() {
  // 1. Clear the canvas each frame
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  // 2. Draw the player (a blue square)
  ctx.fillStyle = '#3b82f6';
  ctx.fillRect(player.x, player.y, player.w, player.h);

  // 3. Draw score text
  ctx.fillStyle = '#fff';
  ctx.font = '16px Inter, sans-serif';
  ctx.fillText('Score: ' + score, 8, 22);
}
Important: Never draw without clearing first! If you skip clearRect, you'll see a smear trail of every position the object has ever been.

๐Ÿงฉ The Same Idea in Scratch

Scratch has no equivalent code to write here at all. That's the trade-off. Every sprite just carries its own x/y position and current costume, and Scratch's engine automatically clears and redraws the whole Stage every frame for you. The "clear, then redraw" rule still happens underneath. You just never have to remember it yourself.

โœ… Checkpoint: call render() once from inside gameLoop and confirm you see a blue square and score text on your canvas. Nothing moves yet. That's expected, since nothing is changing player.x/player.y yet. Step 4 fixes that.

4โŒจ๏ธ Keyboard Input

Think of each key like a light switch: pressing it flips the switch on, releasing it flips it back off. Instead of reacting to every single press and release, we just keep a little switchboard (a keys object) and check which switches are on during each update.

const keys = {};
const scrollKeys = ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Space'];

document.addEventListener('keydown', e => {
  keys[e.code] = true;
  if (scrollKeys.includes(e.code)) e.preventDefault(); // stop the page from scrolling
});
document.addEventListener('keyup',   e => keys[e.code] = false);

function update() {
  if (keys['ArrowLeft']  || keys['KeyA']) player.x -= 4;
  if (keys['ArrowRight'] || keys['KeyD']) player.x += 4;
  if (keys['ArrowUp']    || keys['KeyW']) player.y -= 4;
  if (keys['ArrowDown']  || keys['KeyS']) player.y += 4;
}
Common glitch: by default, the browser uses arrow keys and space to scroll the page. If you skip e.preventDefault() on those keys, the page will scroll up and down while someone is trying to play, breaking the controls. This one line is the fix for almost every "my arrow keys aren't working right" bug you'll hit.

That's it. The same switchboard pattern runs every keyboard-controlled game you've ever played, from Mario's jump button to a rhythm game's tap keys.

๐Ÿงฉ The Same Idea in Scratch

Live blocks: try swapping the dropdown or the number, then reveal the JS.

The hexagonal piece is a Sensing block called "key (_) pressed?", a Boolean, just like the true/false values sitting inside our keys object. Snap it inside an "if โ€ฆ then" block, and it checks the exact same switch every single frame, since it lives inside the "forever" loop from before.

โœ… Checkpoint: add the keys object and the two event listeners, call update() from your loop, and confirm the blue square actually moves with the arrow keys/WASD. This is the first moment the game feels alive. Don't skip ahead until it moves.

5๐Ÿ’ฅ Collision Detection

Picture two picture frames hanging on a wall. They only overlap if they overlap left-to-right and top-to-bottom at the same time; lining up on just one direction isn't enough. That simple idea has a formal name (AABB, short for Axis-Aligned Bounding Box), and it's the collision check almost every simple 2D game relies on.

a (player) b (enemy) overlap

The yellow region is where all four AABB conditions are true at once. That's the collision. Slide either box away in any direction and the yellow region (and the collision) disappears.

function collides(a, b) {
  return a.x < b.x + b.w &&  // a's left is left of b's right
         a.x + a.w > b.x &&  // a's right is right of b's left
         a.y < b.y + b.h &&  // a's top is above b's bottom
         a.y + a.h > b.y;    // a's bottom is below b's top
}

This exact check is how Pac-Man knows he touched a ghost, how Mario knows he's landed on a platform, and how the dodge game below knows you got hit.

๐Ÿงฉ The Same Idea in Scratch

Live blocks: edit the sprite name or the number, then reveal the JS.

Scratch's "touching (_)?" block, another Sensing Boolean, is the whole collides() function above, already written for you. It's even more precise than AABB: it checks actual sprite shapes, not just rectangles. Same job, zero formula to type.

See the Scratch Wiki's guide to collision blocks โ†’
โœ… Checkpoint: write collides(a, b), call it against your player and a test rectangle, and console.log the result while you move the player through it. You should see false โ†’ true โ†’ false as you pass through. That's every ingredient of Step 6 assembled.

6๐ŸŽฎ Play It โ€” Dodge Game

This complete dodge game was built using only the techniques above: Canvas, game loop, keyboard input, and AABB collision. Move with arrow keys or WASD. Avoid the red enemies. Score rises as you survive.

Score: 0 Arrow keys / WASD โ™ฅ โ™ฅ โ™ฅ
What you see in the code: the game loop runs every frame via requestAnimationFrame. Enemies spawn at random X positions and fall from the top. AABB collision checks each enemy against the player every frame. The score ticks up with Date.now().
Which tiny-loop archetype is this? This is the Dodging Game archetype from What Makes a Game?: one mechanic (move), one lose condition (collision), one climbing variable (score). Flip the collision logic (good items add score instead of costing a life) and you've rebuilt the Catch Game archetype using the exact same loop.

๐Ÿงฉ Build This Exact Game in Scratch

Scratch's own team built an official tutorial for this exact archetype: move a sprite, avoid (or chase) another one, and keep score. It walks you through the same green-flag โ†’ forever โ†’ touching โ†’ score pattern you just read above, one block at a time.

Open the official Scratch Chase Game tutorial โ†’

7๐Ÿ› ๏ธ Your Turn โ€” Extend the Game

Now that you've seen the pattern, try one of these small, safe edits to code that already works. Pick whichever sounds most fun. You don't need to do all six, and it's completely fine to keep the code above open while you work.

Change player speed: find the 4 in the movement code and try 6 or 2. How does this change the difficulty feel?

Add a score multiplier: make enemies worth more points the longer you survive (multiply score by a factor that increases over time).

Add a power-up: spawn a green square at random intervals. When the player collides with it, restore a life.

Add sound: use the Web Audio API to play a beep when the player is hit: new AudioContext() โ†’ create an oscillator โ†’ start() and stop(0.1).

Add a high score: use localStorage.setItem('highscore', score) to save the best score between sessions.

Turn Dodge into Catch: in the collision check, instead of always subtracting a life, make half the falling squares "good" (green, +1 score) and half "bad" (red, โˆ’1 life). You've just built a second tiny-loop archetype from the same code.

๐Ÿงฉ The Same Six Ideas โ€” in Scratch

Working in Scratch instead? Every edit above has a direct block equivalent: change the number inside "change x by (4)" for speed ยท multiply the "change score by" amount using a timer variable for a multiplier ยท add a power-up sprite with its own "if touching (_)? then change lives by (1)" script ยท drop in a "play sound" block inside your collision check ยท add a "highscore" variable and an "if score > highscore then" block to save the best run ยท and swap "change lives by (โˆ’1)" for "change score by (1)" on your good sprites to turn Dodge into Catch.

Ready for the next dimension?

You've mastered the 2D game loop, canvas drawing, and rectangular collision detection. How do games turn these flat 2D canvases into first-person 3D worlds?

Continue to 3D from Scratch: Projection, Raycasting & Render Engines to learn perspective division math, rotation matrices, Wolfenstein 2D raycasting, SNES Mode 7 scanlines, and software rasterization from first principles.

Keep AI coding manageable as your project grows

Use Code Bridge to prepare one small request with current source, review the AI's complete-file answer, and keep test checkpoints alongside VS Code. Continue using your teacher-approved Google AI chat; the site makes no AI API calls.

Next: Code with AI: Small Tasks, Current Files & Real Tests walks through a jump-bug practice project, dependency selection, and a fresh-chat handoff.