Improve an existing browser game's code (structure, readability, performance) without changing how it plays. Records how the game behaves first, then changes in small verified steps. Splitting a single giant file into modules, pulling numbers into config, adding a proper loop with dt, a scene stack, input actions, removing globals, fixing leaks, and replacing per-frame speeds without changing f...
Download skill (.zip) Files on GitHubGame refactoring
Refactoring changes the code, not the game. Same controls, speeds, timings, difficulty, visuals and saves. If behavior should change, that’s a separate step the user asks for.
0. Safety net (always)
- Commit or snapshot first.
git statusclean, or copy the folder (cp -r game /tmp/game-before-refactor). - Record behavior. Play it and write a short checklist in
docs/HANDOFF.mdor a scratch file. Screenshots of each state help (look_at_preview, or Playwright for 2D):- every state and how you get there (title → play → pause → gameover → retry),
- controls (keyboard and touch) and what each does,
- key numbers you can observe: player speed (time to cross the screen), jump height, fire rate, spawn timing, lives, scoring,
- save behavior (what persists after a reload),
- known bugs. Keep them unless asked to fix them, and list them separately.
- Note the frame rate and load time if performance is the goal.
1. Plan
List the steps in order, each small enough to test in a minute. Typical order for a messy single-file game:
- Move tunable numbers into
config.js(no logic change). - Split the file into modules by responsibility, cut and paste only:
core/(loop, input, audio, save, screen),scenes/,entities/(the game-scaffolding template shows the target layout). - Replace globals with one
gameobject passed in. - Introduce the scene stack and turn
if (state === …)chains into scene files. - Input actions instead of raw key checks.
- Fix timing:
requestAnimationFrame+ dt or fixed step (feel-preserving: see below). - Fix leaks: listeners removed in
exit(), pools for bullets and particles. - Performance changes, measured before and after.
Tell the user the plan in a few lines if it’s more than three steps.
2. Do one step at a time
After each step: reload, run the behavior checklist (at least the states touched), check the console is clean, then commit
(git commit -m "refactor: move tunables into config"). If something changed, fix it or revert the step (git checkout -- .),
and never stack new changes on a broken step.
3. Keep the feel identical
| Change | How to keep feel |
|---|---|
| Per-frame → per-second movement | speedPerSecond = speedPerFrame × 60 (if it was tuned at 60 fps). Same for acceleration (× 3600 for per-frame²) |
| Frame-count timers → seconds | frames / 60 |
| setInterval(16) loop → rAF fixed step | Fixed step 1/60 gives the same simulation as before at 60 fps |
| Variable dt → fixed step | Keep the old dt cap. Gravity and jumps should then match exactly |
| Collision rewrite | Same hitbox sizes and offsets. Compare positions in a few known situations |
| New random generator | Same distribution. Seeded runs will differ, so say so |
| Save format change | Bump the version and migrate old saves. Never lose a player’s progress |
Measure if unsure: log the player’s position after holding right for one second, before and after.
4. Finish
- Run the full behavior checklist on desktop and a phone size, plus the code scanner (game-code-review) for regressions.
- Update
README.md(where things are),docs/GDD.md(if values moved) anddocs/HANDOFF.md. - Tell the user in one or two lines: what’s cleaner now, and that it plays the same. List anything that did change, for example “fixed: it no longer runs fast on 120 Hz screens”, only if they agreed.
Don’ts
- Big-bang rewrites (“I rewrote it in Phaser”). That’s a new game, not a refactor
- Renaming and moving and fixing in one commit
- Changing art, text, balance or controls “while you’re in there”
- Removing code you don’t understand without checking what calls it (
rg "functionName") - Leaving the game broken between steps in a live Bash preview people are watching