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
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.
Download a runnable canvas game with WHAT / WHY / TRY THIS comments, checkpoints, challenges, troubleshooting, and a finished core reference.
๐จ 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.
Looks
Controls how a sprite appears: costume, size, speech, visibility.
Sound
Plays and mixes audio: sound effects, notes, volume.
Events
Rounded "hat" blocks that start a script when something happens.
Control
Directs the flow of a script: loops, conditionals, waiting, cloning.
Sensing
Hexagonal Boolean blocks that ask true/false questions about the world.
Operators
Hexagonal math, comparison, string, and randomness blocks that plug into other blocks.
Variables
Custom variables and lists you create to store data: score, lives, names.
My Blocks
Custom blocks you define yourself, to reuse a group of blocks under one name.
๐งญ 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.
1๐งฑ Anatomy of a Browser Game
A browser game is just three files working together:
<!-- 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.
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:
Check input
Resolve physics
Draw everything
Show to player
(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:
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); }
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.
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; }
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.
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.
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.
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.
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(). ๐งฉ 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.