Building Web Games with MVT
This book teaches MVT (Model-View-Ticker), a way to structure games that run in a web browser. It splits a game into models that decide what happens, views that show it, and a ticker that keeps time. The result is a game you can test without a screen, pause and replay, and draw any way you like. The book starts with a ball bouncing on a canvas, and adds one idea at a time until you have everything a full arcade game needs.
Who This Book Is For
This book is for programmers who want to build games that run in a web browser, and want those games to stay easy to change as they grow.
You might be a web developer who knows React or Vue, and wants to make a game. You might have made a game or two with a renderer such as Pixi, and watched the code tangle as it grew: a timer that keeps running while the game is paused, a rule that you can't test without drawing the screen, or a change to how something looks that breaks how it plays. Or you might know an engine such as Unity or Godot, and want to build for the web without one.
You'll need to be comfortable writing TypeScript or JavaScript, and know the basics of a web page, such as elements and events. You won't need any experience of a game engine or a renderer. The examples draw with Pixi, a popular 2D renderer, and use only a small part of it.
If you build for another platform, or write programs that animate but aren't games, most of the book still applies. Appendix H says what carries over.
What You'll Learn
- Write a game's rules as models. A model is plain code, which a test can check in milliseconds without drawing anything.
- Show the game with views. You can add, change or remove a view without touching the rules, whether it draws with Pixi, three.js or HTML, or plays sound.
- Control time. A game can pause, slow down, speed up, step a frame at a time, or replay.
- Polish the presentation. Transitions, effects and lists of things that come and go keep their own state, out of the rules.
- Test and tune. You'll test how each view looks with images, and keep a busy game fast.
- Decide new cases. When you meet a situation that no rule covers, the eight principles behind the rules show what to do.
How the Book Is Organised
| Part | Contents |
|---|---|
| I. The Big Picture | Where web games come from, why MVT exists, and a first game built with it: Games in the Browser, Why MVT, A First Game |
| II. The Simulation | Models, and the time that moves them: Models, The Tick, Time and Phases |
| III. The Presentation | Views, from a single sprite to lists that change, and why they poll: Views, Bindings, Noticing Changes, Why Views Poll, Presentation State, Writing Views in JSX, Composing Views, Lists and Switches |
| IV. Testing | Testing each part on its own: Testing Models, Testing Views, Visual Tests |
| V. Advanced Presentation | Views that grow complex, and what games add: Growing a Complex View, Lists in Motion, Sound and Music, Reading Layout Back, Reduced Motion |
| VI. Performance | Keeping a game fast: Why Performance Matters, Hot Paths |
| VII. Principles Behind MVT | The principles behind the rules, one chapter each, and how to use them on a case the rules don't cover: The Eight Principles, then SoC, SSOT, CQS, DIP, CRP, DbC, MISU, DfT, and Using the Principles |
| Conclusion | What MVT gives you, what it doesn't, and where to go next: Conclusion |
| Appendices | Pages to look things up in: MVT on One Page (every rule), Glossary, Measurements, Measuring Performance, Timelines with GSAP, Troubleshooting, API Cheat Sheets, MVT Beyond the Web |
Each chapter builds only on the chapters before it. When one has to mention something that comes later, it says so, and doesn't depend on it.
You can also read the book as a PDF, typeset for print or for reading offline. It's printed each time the site is published, so it always matches these pages. In place of each experiment that runs here, it shows the experiment's picture and the address of its page.
If You're in a Hurry
Read Parts I to III in order. They teach everything you need to build a game with MVT, in about the order you'll need it. Part IV, on testing, is short, and worth reading next. Then dip into the rest as questions come up: Part V when a view gets complicated, Part VI when a frame starts to feel slow, and Part VII when you want to know why a rule exists. If something isn't working, Troubleshooting lists the mistakes people make most, and where each one is explained.
Conventions
Examples are in TypeScript, and run in a browser. Most of them draw with Pixi. The ideas don't depend on either, as Appendix H shows. The examples follow one style throughout: factory functions that take one options object, undefined rather than null, and string-literal unions rather than enums. That style is a choice, not part of MVT, and each chapter says which is which.
"In this book" asides show how the @mvtjs libraries do something, or which of the book's own conventions an example follows. MVT requires an order of calls and a few rules, not any particular library. The @mvtjs libraries include a small library for each renderer that the book uses (@mvtjs/pixi, @mvtjs/three and @mvtjs/html), and one for what they share (@mvtjs/utils). Where an example uses one, an aside says so:
In this book
setRefresh, from @mvtjs/pixi, sets a Pixi container's refresh step: the step that reads the state and shows it, every frame.
Experiments appear throughout the online book. Each is a small example that runs in the page, written for the passage it sits beside, and numbered by its chapter, such as Experiment 4A. It shows a picture until you press Run it here. Then it runs, and View code opens its code below it, so you can change the code and watch it run again. The link below each experiment, Open it in the playground, opens it in the playground, a small editor in your browser for whole projects. The text never depends on an experiment, and a printed copy shows each one's picture and the address of its page online instead.
The website's games, demos and puzzles supply the bigger examples. This book is part of the MVT Games website, which also has an arcade of original games, a collection of demos and a collection of puzzles. They're all built with MVT, in the same repo as this book. When a chapter needs more than a sketch, it points to one of them, such as Burrow Bust, a digging game in the arcade, and links to its code.
For AI Agents
If you're an AI coding agent working in this repo, start with AGENTS.md. It sums up the rules and conventions, and points to the chapters behind them.