Building a 3D Web Game with AI: 14 Rounds of Engineering
No multiplayer, no backend, no art assets — all code written by AI, with the human only making judgment calls. The output was 4158 lines of game code plus four test scripts. But the real takeaway isn’t the game; it’s how to verify AI edits didn’t break something else. That’s the part worth reusing.
Starting point: a prototype only its author could play
The first version had three things: move, shoot, and a few crates for cover. It ran in 200 lines of HTML and JavaScript, with no tests and no structure.
At that size, AI rarely fails to write code — it hands you a complete working file and you paste it in to see the result. The problem is that as the project grows, the risk shifts from "it cannot write this" to "it broke something and you did not notice."
This project eventually reached 4158 lines of game code. From that point on, "it still looks fine after the change" stopped being good enough.
The fixed four steps of every iteration round
Fourteen rounds, each following the same loop. What matters is not the feature but how you know nothing else broke.
- Write down the change — not "I asked AI to add a feature" but exactly which functions and which data tables changed. New field names and new DOM ids have to be traceable.
- Syntax and smoke test — first make sure the file parses and survives an empty environment without throwing. This is the basic gate and catches most low-level mistakes.
- Reimplement the core algorithm in a standalone script — this is the most valuable part of the whole method. More on it below.
- Grep for reference consistency — confirm new variables, functions and DOM ids exist on both the JS and HTML sides. A mistyped id throws no error; it just does nothing.
The highest-value trick: extract the algorithm and run it alone
This solves the most dangerous problem in AI collaboration: after the code is written, you cannot tell by eye whether the logic is right — especially the math.
The method: copy the few lines of core algorithm into a standalone script, feed it fixed inputs, and check whether the output matches expectations. Bullet penetration decay, for example:
// 穿透衰减:障碍越厚,伤害越低,超过上限就不算穿透
mul = pen * clamp(1 - thickness / maxThick, 0.15, 1)
if (mul > 0.06) { /* 算穿透 */ }This script caught a real bug. The first version used maxThick = 0.6 + pen * 3.2, giving wooden crates a 2.58-metre limit, while the crates are only 1.8–2.6 metres thick — meaning you could shoot through an entire crate, just at heavily reduced damage. That makes no physical sense. Changing it to 0.5 + pen * 1.8 (a 1.62-metre limit) made thin-edge penetration meaningful while whole crates still block.
The lesson: the penetration limit must be smaller than the obstacle’s actual thickness. AI will not invent that constraint on its own, but the moment it is expressed as an executable assertion, AI cannot break it either.
Three places where AI gives false confidence
The mistakes AI made here shared a pattern: the code looked completely normal, threw no error, and behaved incorrectly.
Sound feedback attached to the wrong layer
The goal was a whizzing-bullet cue — a warning that reaches your ears before the health bar drops. The first version hooked the end of hitscan(), triggering on how close the bullet path passed to the player. The direction was exactly backwards: hitscan is shared physics, so your own shots went through it too, and you heard your own bullets as if you were being shot.
The fix was to hook the miss branch of botFire() — "an enemy shot went past you" is the event itself.
The rule: attach sound feedback where the event happens, not where the physics is computed. The two layers sit close together but mean completely different things.
Random numbers inside a state selector
The BOT's trade-kill window was originally written as Math.random() < tr, sitting inside the state-selection logic. It flickered between trading and turning every frame, which looked like a twitch. Switching to a pure duration check made it stable instantly.
The rule: never put randomness in a state selector — any logic that must stay consistent for the duration of a decision will break.
A global replace that rewrote the function too
Replacing every DIFF[match.difficulty] with adaptiveDiff() also rewrote the line inside the function, so adaptiveDiff() ended up calling itself — infinite recursion.
The rule: exclude the function's own line before any global replace, then grep the function body afterwards to confirm.
What you write down is the actual asset
Fourteen rounds, each with an engineering log recording three things: what changed, how it was verified, and what went wrong.
That log paid for itself repeatedly. Three examples:
- "Grep the count after rewriting a function" — after rewriting the tracer logic, the old function was still in the file, and in JavaScript the later definition wins, silently shadowing the new one. The check is
grep -c "function updateTracers"always equalling 1. - "Run the numbers before finalising" — the first bullet-drop coefficient of K=0.06 produced a 0.02-metre drop over 50 metres, completely invisible, so the work was wasted. At K=0.42 the drop reaches a visible 0.44 metres.
- "Grep every cleanup site after changing a container's structure" — after pickups changed from a bare Mesh to an object
{group, glow, ...}, twoscene.remove(m)calls had to becomescene.remove(d.group); otherwisescene.remove(undefined)fails silently and pickups leak across rounds.
These lessons share a trait: none of them are about how to write code, they are about how to confirm you did not write it wrong. The first keeps getting easier as AI improves; the second barely improves at all — which is exactly why it is worth writing down.
What I would do differently
The biggest waste in this project was not having test infrastructure for the first eight rounds. Those rounds relied on "play it once after the change", which let two bugs through that only appear over a long session:
- The noise array only grew — every sound pushed to the array, never cleaned up. Memory grew without bound over long sessions and BOT scans got slower every frame.
- The minimap crashed in the menu — reading player yaw while the player object was null threw immediately. The menu hid it in the browser, but the console filled with 60 errors per second while rendering did nothing.
Neither bug was complex. Both were missing a "run 1000 frames without crashing" check. After the test scripts were built in round nine, this class of problem never recurred in the remaining five rounds.
So if I started over, I would build the test scripts in round two rather than after round eight. The earlier you build the tooling, the more every later round saves.