Bash.tv → GitHub → itch.io

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 GitHub

Game 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)

  1. Commit or snapshot first. git status clean, or copy the folder (cp -r game /tmp/game-before-refactor).
  2. Record behavior. Play it and write a short checklist in docs/HANDOFF.md or 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.
  3. 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:

  1. Move tunable numbers into config.js (no logic change).
  2. 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).
  3. Replace globals with one game object passed in.
  4. Introduce the scene stack and turn if (state === …) chains into scene files.
  5. Input actions instead of raw key checks.
  6. Fix timing: requestAnimationFrame + dt or fixed step (feel-preserving: see below).
  7. Fix leaks: listeners removed in exit(), pools for bullets and particles.
  8. 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

Don’ts