🧠 “Code + Logic + Fun = You + JavaScript, baby.”
JavaScript is sometimes like an annoying ex,
and sometimes like that adorable partner who surprises you every single day.
But today…
We’re building a mini JS game in just 30 lines, and you’ll be like:
“Wait—30 lines? Are you kidding me?”
Our game type:
👉 Reaction Game
A box on the screen turns green at a random moment,
and you click it as fast as you can.
And yes, we’re doing all of this in… 30 lines.
🚀 1) The Game Logic — The Tiny Brain Surgery Behind 30 Lines
This game has three simple phases:
✔️ 1. Setup
The box starts gray. “Not click-ready.”
✔️ 2. Waiting
JavaScript loves suspense.
We use setTimeout() to turn the box green after a random delay.
✔️ 3. Reaction Measurement
Box turns green → you click → we record timestamps → boom, your reaction time.
That’s it.
There is no NASA-grade algorithm hiding in here.
🧩 2) The HTML: The Stage (Only 7 Lines)
Baby, this is literally the entire stage:
<div id="box">Click me!</div>
<p id="result"></p>
📌 TIP:
Keep the HTML minimal. “Less HTML, more magic.”
🎨 3) The CSS: Just Dressing Up the Box
We’re flexing with JavaScript today,
so CSS can go as wild as it wants 😘
#box {
width: 200px;
height: 200px;
background: gray;
display: flex;
justify-content: center;
align-items: center;
color: white;
font-size: 20px;
cursor: pointer;
border-radius: 20px;
}
💡 Fun Fact:
The prettier the box looks,
the faster your reflex becomes. Probably.
(Totally made up but it feels true.)
⚙️ 4) The 30-Line JavaScript Legend
And here it is.
Count it—it’s really 30 lines 👇
let box = document.getElementById("box");
let result = document.getElementById("result");
let startTime, endTime;
let clickable = false;
function startGame() {
box.style.background = "gray";
box.textContent = "Get ready...";
clickable = false;
let delay = Math.random() * 3000 + 1000;
setTimeout(() => {
box.style.background = "green";
box.textContent = "Click!";
startTime = Date.now();
clickable = true;
}, delay);
}
box.onclick = () => {
if (!clickable) {
result.textContent = "Too early, babe 😘";
return;
}
endTime = Date.now();
let reaction = endTime - startTime;
result.textContent = `Reaction Time: ${reaction} ms 🔥`;
startGame();
};
startGame();
🧠 5) Let’s Break Down the Code (University Class Mode 😘)
🎯 1) Random Delay
let delay = Math.random() * 3000 + 1000;
JavaScript is like:
“Surprise! You don’t know when it’ll turn green.
Have fun.”
This makes the wait:
✔ minimum 1 second
✔ maximum 4 seconds
🎯 2) Making the Box “Clickable”
clickable = true;
State management is a huge concept in game dev.
This boolean basically says:
“Yes king/queen, now you may click.”
🎯 3) Reaction Measurement
let reaction = endTime - startTime;
If you want to get more pro:
✨ fastest reaction
✨ average reaction
✨ high-score table
✨ streak system
You can add all of this.
💥 6) 10 Crazy Ideas to Upgrade the Game
Because developers never leave things alone once they work 😌
🌟 1. Make the box jump to random positions
(Infuriating but fun.)
🌟 2. Add a background animationtransition: background 0.3s;
🌟 3. Add sound effects
For early clicks, the box can roast you:
“Hey hey relax champion.”
🌟 4. Add a red → yellow → green countdown animation
🌟 5. Make it fully mobile-friendly
🌟 6. Add a scoring system
🌟 7. Add a 30-second challenge mode
🌟 8. Animate the box (scale up/down)transform: scale()
🌟 9. Add a start screen
🌟 10. CHAOS MODE
Random colors, sizes, positions…
Pure madness.
Pure fun.
❤️ 7) Why This Tiny Game Is an Amazing Learning Tool
Because in just 30 lines, you learn:
✔ setTimeout()
✔ Math.random()
✔ DOM manipulation
✔ State management
✔ Event handling
✔ Time measurement (Date.now())
✔ Game loop logic
✔ UI feedback (color/text changes)
Let me tell you a secret, babe:
These 30 lines will make you FALL IN LOVE with JavaScript.
