What a browser does
A browser is a program that downloads web pages and turns them into something you can see and click. Chrome, Edge, Safari, Firefox and Opera are all browsers. Inside every browser is a browser engine that does the hard work.
Chrome, Edge, Opera
Made by Google and shared with many other browsers.
Safari
Made by Apple. On iPhones and iPads, almost every browser uses WebKit.
Firefox
Made by Mozilla, a non-profit group.
When you open a page, the browser follows the same steps every time. Click through them:
Every tab is a sandbox
A sandbox is a play area with walls. Each browser tab runs inside one. A web page can draw, play sound and run code, but it can't read your files or look at your other tabs. To use your camera, microphone or location it must ask for permission first. That's a big reason why playing games in the browser is safer than installing unknown programs.
The cache makes the second visit faster
The browser saves a copy of files it downloaded (pictures, code, sounds) in a cache. The next time you visit, it only downloads what changed. That's why a game you played yesterday opens faster today.
HTML, CSS and JavaScript
Every web page is built from three languages, and each one has its own job:
- HTML is the skeleton: what is on the page (headings, text, buttons, images).
- CSS is the clothes: how it looks (colors, sizes, fonts, layout).
- JavaScript is the brain: what happens when you click, tap or type.
Build a Play button in three layers
Switch between the tabs. The highlighted lines are what each layer adds. In the last tab, click the button.
How a web game draws
Web pages are made of HTML elements, but a game needs to draw anything it wants, many times a second. For that, HTML has a special element: <canvas>. It starts as a blank drawing surface. JavaScript gets a "pen" from it and draws shapes, pictures and text.
const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); // the pen ctx.fillStyle = "orange"; // pick a color ctx.fillRect(40, 30, 60, 60); // x, y, width, height
Draw a square with code
Change the numbers and watch the square move. Remember that y grows downward: the point (0, 0) is the top-left corner.
WebGL
A faster way to draw. It lets the GPU paint thousands of sprites at once. Phaser, the engine we use, picks WebGL when it can and falls back to Canvas if it can't.
Web Audio
A set of tools to play, mix and change sounds, like music loops and jump effects.
Keyboard, touch, gamepad
The browser sends events when you press a key, touch the screen or push a controller stick. The game listens for them every frame.
The game loop
A video game is a super-fast flipbook. The screen is redrawn about 60 times every second. Each drawing is called a frame, so this speed is "60 frames per second" (fps).
Between frames, the game repeats the same three steps forever. This cycle is the game loop, and every game in the world has one:
A tiny game with its insides showing
Click the game (or use the buttons on a phone), then move with ← → and jump with Space. Watch the numbers on the right change every frame. Turn on slow motion to see single frames, show the hitboxes, or switch gravity off to see what it does.
Coordinates, sprites and scenes
Coordinates
Everything on screen has a position made of two numbers: x (how far right) and y (how far down). The point (0, 0) is the top-left corner. Watch out: in games, y grows downward, the opposite of math class. So when a character falls, its y gets bigger.
Sprites
A sprite is any picture that moves around in a game: the hero, an enemy, a coin, a sparkle. Animation is the flipbook idea again: the game swaps between frames of a sprite sheet a few times per second.
Tilemaps
Levels are usually built from small square tiles, like LEGO bricks: a grass tile, a stone tile, a spike tile. A tilemap is a grid that says which tile goes where. Making a new level means arranging tiles, not drawing a whole new picture.
Scenes
A game is split into scenes, like the screens of a menu. Each scene has its own job:
Physics and collisions
Games fake physics with simple math, done once per frame:
- Velocity is speed with a direction. Every frame:
x = x + speedX. If speedX is 3, the hero moves 3 pixels right per frame, 180 pixels per second. - Gravity is just a small number added to speedY every frame. A jump sets speedY to a big negative number (up!), and gravity slowly turns it positive (down). That makes the nice curved jump you saw in the game loop lesson.
- A collision happens when two things touch. Most games check this with invisible rectangles around each sprite, called hitboxes.
Two boxes overlap when box A's left edge is left of B's right edge, A's right edge is right of B's left edge, and the same is true for top and bottom. Four comparisons, thousands of times per second.
Game designers often make hitboxes a bit smaller than the drawing. Then when an arrow just brushes your helmet, it misses, and the game feels fair.
Where your scores live
Leon Games keeps scores in two very different places:
The Hall of Fame
The Hall of Fame is the wall of records: nicknames, weekly top 20 and all-time top 20. It's the only part of Leon Games where your device sends data to the server, so it's also where security matters most.
One idea to learn first. The game runs inside an iframe, which is a window inside the page. The game and the page talk by passing notes to each other with something called postMessage. The game sends two notes: run-start when you begin and run-finish with your result when you end.
Stopping spam without storing IP addresses
To stop one person from flooding the wall, the Worker needs to notice when lots of requests come from the same place. But Leon Games never saves IP addresses. The trick is a hash.
A hash is like a one-way blender. You put in some text and get a fingerprint out. The same text always gives the same fingerprint, but you can't turn the fingerprint back into the text. Leon Games blends the IP address together with a secret word and today's date, and saves only the fingerprint. Because the date is in the mix, the fingerprint changes every day. And old records are deleted after two days.
The one-way blender (SHA-256)
This uses SHA-256, the same hash Leon Games uses. Change just one letter and the whole fingerprint changes.
- Speed limits: each fingerprint can only start and submit a limited number of runs and scores per hour. Go over and the Worker answers
429 Too many requests. - Moderation: nicknames are filtered for bad words. Anyone can report a record, and after several reports it's hidden until an adult reviews it.
Phaser, TypeScript and Vite
The games on Leon Games are built with three main tools. Each one saves a huge amount of work.
Phaser
A free library that already knows how to run the game loop, draw sprites with the graphics card (WebGL), play sounds, read keyboards and touch screens, switch scenes and do physics. You focus on the game itself.
TypeScript
The language the games are written in. It's JavaScript plus types, which let the editor catch mistakes before you even run the game.
Vite
While you work, it reloads the game instantly when you save. When you're done, it builds: turns all the source files into a few small, fast files for the browser.
Three.js and friends
Three.js is for 3D games. Howler.js for advanced sound, Matter.js for advanced physics. Leon Games only adds them when a game really needs them.
// TypeScript catches the typo before you play const knight = { health: 3, speed: 160 }; knight.healt = 2; // ✗ Error: 'healt' does not exist. // Did you mean 'health'?
// Phaser: a scene that moves the hero update() { if (this.keys.left.isDown) this.hero.setVelocityX(-160); if (this.keys.jump.isDown && onGround) this.hero.setVelocityY(-420); }
From code to website
How does a new game get from a laptop to every Cloudflare city? There are four tools in the chain:
- Code editor: where the code is written. The whole project is a folder of files.
- Git: saves snapshots of the project called commits, each with a short message. If something breaks, you can look back at history or return to a snapshot that worked.
- GitHub: keeps a copy of the project and all its commits online, as a private backup.
- Deploy: one command,
wrangler deploy, uploads thewebsitefolder to Cloudflare. In a few seconds the new version is live in every city.
The real project folder looks like this. The source code of each game lives in games/, and the finished, built version is copied into the website:
ReduxGames/ ├─ games/ ← source code of each game │ └─ true-knight/ TypeScript, scenes, levels… └─ website/ ← what gets deployed to Cloudflare ├─ public/ HTML, CSS, JS, images │ └─ games/ │ ├─ games.json the catalog of games │ └─ true-knight/ the built game ├─ src/worker.ts the Worker (Hall of Fame API) └─ wrangler.jsonc settings for Cloudflare
What makes a game fun
Code makes a game work. Design makes it fun. Most good games share these ingredients:
A clear goal
Reach the castle, collect all gems, beat your best time. Players should know it within seconds.
A difficulty curve
Start easy, then add one new challenge at a time. A jump in Level 1, a moving platform in Level 2, both together in Level 3.
Feedback
Sounds, sparkles, screen shake and popping numbers make every action feel good. Game makers call this "juice".
Quick retries
When you lose, you should be playing again in a second or two. Short waits keep players coming back.
Fair rules
Players accept losing when they understand why. Hidden traps and huge hitboxes feel unfair.
Playtesting
Watch a friend play without giving tips. Wherever they get stuck or bored is what to fix next.
Check what you learned
6 questions about this track. Pick an answer to see if you got it right. Your answers are saved only on this device.