📂 The Stage 3 Files
Stage 3 is the first stage where the behavior changes, not just the code structure. Click the bomb — it disappears, replaced by the explosion. When the sparks fade, a hint tells you to click again to reset. Same three files, one new idea:
| File | What it does | Stage 3 changes |
|---|---|---|
bombStg3.html |
HTML shell — loads p5.js, CSS, and sketch | Filename + sketch pointer updated |
bombStg3Sketch.js |
The p5.js sketch — all drawing & logic | State machine, conditional rendering, hint text |
bombStg3Styles.css |
Minimal CSS reset — canvas flush, no margin | Intentionally unchanged — see WHY below |
The entire disappearance mechanic is two variables and one
if:let bombVisible = true;let exploded = false;And in
draw():if (bombVisible) { drawBomb(); }When
bombVisible is false, drawBomb()
never runs — so the bomb is simply absent from that frame's image.
No erase step, no CSS tricks. The background clears the canvas every
frame; conditional rendering decides what gets drawn back on top.
🗺️ The State Machine
Stage 3 introduces a formal state machine — a description of every “mode” the program can be in and every transition between modes. There are three states; a click triggers two of the transitions automatically:
| State | bombVisible | exploded | particles.length | What draws | Transition |
|---|---|---|---|---|---|
| A — Waiting | true |
false |
0 |
Sky + clouds + bomb | Click → State B |
| B — Exploding | false |
true |
> 0 |
Sky + clouds + sparks | Sparks fade → State C |
| C — Done | false |
true |
0 |
Sky + clouds + hint text | Click → State A |
Why two variables?
particles.length === 0 is true in both State A (startup) and
State C (post-explosion). A single check can’t tell them apart.
exploded resolves the ambiguity: it is only true
after a click has happened, pinning us precisely to State C.
📜 Source Code
Loading bombStg3.html…
Loading bombStg3Sketch.js…
Loading bombStg3Styles.css…
💪 Try It Yourself
Use the Copy button above to grab any file, then:
-
Copy all three files into a project folder with
sketches/andstyles/sub-folders. -
Open
bombStg3.htmlin your browser. Click the bomb — watch it disappear. Wait for the sparks to fade, then click again to reset. -
Experiment 1: Open
bombStg3Sketch.jsand changeHINT_TEXTto your own message. Try changingHINT_TEXT_SIZEorHINT_TEXT_Ytoo. -
Experiment 2: Inside
mousePressed(), find the State A → B transition and changeBOMB_CX, BOMB_CYtomouseX, mouseY. How does that feel different? Why did we change it back to the bomb center? -
Challenge: Add a fourth state — a brief
“FUSE BURNING” pause of ~2 seconds between the click
and the explosion. Hint: p5.js’s
frameCountvariable and afuseStartFramevariable are all you need.
⚠️ The Copy button requires a secure context
(https:// or localhost). On a bare file:// URL,
right-click → “View Page Source” to read the code instead.