Playground · mini game
Neon Tower: stack the blocks
A block swings back and forth, you drop it, and whatever sticks out falls off. How high can you go? One HTML file from one prompt, with mouse, finger or space bar.
A click, a tap or the space bar drops the block. Esc or ✕ quits the round.
Open in a new tab ↗ Download as a file
The prompt
Copy it and paste it into Claude, ChatGPT or Gemini. The AI answers with the complete code.
Build me a stacking game as a single HTML file, no external libraries.
- Above the tower, a flat block swings from left to right.
- A click, a tap or the space bar drops it. Whatever sticks out over the block below is cut off and falls down. The next block is only as wide as the piece that stayed on the tower.
- If I place it exactly right, I get a bonus point and the block lights up briefly.
- The tower grows and the view moves up with it. Every new block is a little faster.
- If I miss the tower completely, the game is over. Then "Again" with a click or a tap.
- Dark background, the blocks glow in shades of green. Score big at the top, the best score is saved.
- Works on a PC and on a phone, portrait and landscape.How to run it
- Copy the code from the AI's answer (there is usually a copy button on the code block).
- Open a text editor, Notepad for example, paste the code and save it as
neon-turm.html. The.htmlending is what matters. - Double-click the file. It opens in your browser, done. No server, no install.
Fixed afterwards
After the first test, these follow-ups were added. Same chat, simply as the next message:
The blocks blur into one single green area. Draw a thin dark gap between the blocks, so you can see each one.I want to be able to quit a round at any time: with Esc and with a small ✕ at the top right to tap. Then go back to the start screen. While playing, show "Esc or ✕: quit" small below the best score.After every fall and on the start screen, show a high score list with my 5 best rounds, the round I just played highlighted. The list only lasts for this session, until the tab is closed.Make every fifth block orange, as a little milestone. The bottom block counts as block 1.In the high score list, the marked line does not line up with the others. Put rank and points in columns, with the arrow in its own column after them.The code
This is the code that came out of the prompt and the follow-ups above (with Claude). Every AI builds it a little differently, that's normal. If your version doesn't run, paste the error message or a description back into the chat.
Show code (355 lines)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Neon Tower</title>
<style>
html, body { margin: 0; height: 100%; background: #07090a; overflow: hidden; }
canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: pointer;
user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
</style>
</head>
<body>
<canvas id="field"></canvas>
<script>
const canvas = document.getElementById('field');
const ctx = canvas.getContext('2d');
const TEXT = {
title: 'Neon Tower',
start: 'Click, tap or press space to start',
perfect: 'Perfect! +1',
over: 'Missed!',
best: 'Best',
newBest: 'New best score!',
again: 'Again: click, tap or press space',
quit: 'Esc or ✕: quit',
list: 'Your best rounds (this session)'
};
const STORAGE = 'neon-tower-best';
const SESSION = 'neon-tower-rounds'; // high score list for this session only (until the tab is closed)
// Everything is measured in "units", so the game looks the same size on every screen.
const WIDTH = 50; // width of the first block
const HEIGHT = 8; // height of a block
const TOLERANCE = 1.2; // this close counts as "exactly right"
const GRAVITY = 260;
let dpr, sw, sh, u; // u = pixels per unit
let tower, block, debris, score, best, camera, state, message, direction, newBest, lock;
let rounds, lastRound = null; // top 5 of this session: [{ p: points, id }]
function loadBest() { try { return parseInt(localStorage.getItem(STORAGE), 10) || 0; } catch (x) { return 0; } }
function saveBest(n) { try { localStorage.setItem(STORAGE, String(n)); } catch (x) {} }
function loadRounds() { try { return JSON.parse(sessionStorage.getItem(SESSION)) || []; } catch (x) { return []; } }
// Add the round to this session's list (only with points), the best 5 stay
function rememberRound() {
if (score <= 0) { lastRound = null; return; }
lastRound = Date.now() + Math.random();
rounds.push({ p: score, id: lastRound });
rounds.sort((a, b) => b.p - a.p);
rounds = rounds.slice(0, 5);
try { sessionStorage.setItem(SESSION, JSON.stringify(rounds)); } catch (x) {}
}
function resize() {
dpr = window.devicePixelRatio || 1;
sw = window.innerWidth;
sh = window.innerHeight;
canvas.width = Math.round(sw * dpr);
canvas.height = Math.round(sh * dpr);
u = Math.min(sw / 130, sh / 110);
}
// Shades of green that slowly drift between yellow-green and turquoise as the tower grows.
// Every fifth block is orange, as a milestone (the bottom one counts as block 1).
function colour(i) {
if ((i + 1) % 5 === 0) return 'hsl(28, 95%, 58%)';
return `hsl(${125 + 30 * Math.sin(i * 0.35)}, 80%, 55%)`;
}
function topHeight() { return tower.length * HEIGHT; }
// How far the swinging block travels left and right (centre of the block)
function range(w) {
return Math.max(8, Math.min(sw / u / 2 - w / 2 - 3, 70));
}
function speed() { return Math.min(38 + tower.length * 3, 120); }
function newGame() {
tower = [{ x: 0, w: WIDTH, colour: colour(0), glow: 0 }];
debris = [];
score = 0;
newBest = false;
message = null;
direction = 1;
camera = topHeight();
block = null;
}
function nextBlock() {
const top = tower[tower.length - 1];
direction = -direction;
const r = range(top.w);
block = { x: -direction * r, w: top.w, colour: colour(tower.length) };
}
function start() {
state = 'play';
nextBlock();
}
function drop() {
const below = tower[tower.length - 1];
const offset = block.x - below.x;
const rest = block.w - Math.abs(offset);
const y = topHeight();
if (rest <= 0) {
// missed completely: the whole block falls
debris.push({ x: block.x, y, w: block.w, colour: block.colour,
vx: direction * 12, vy: 0, rot: 0, vr: direction * 1.5 });
block = null;
gameOver();
return;
}
if (Math.abs(offset) <= TOLERANCE) {
tower.push({ x: below.x, w: below.w, colour: block.colour, glow: 1 });
score += 2;
message = { text: TEXT.perfect, t: 1 };
} else {
const newX = below.x + offset / 2;
tower.push({ x: newX, w: rest, colour: block.colour, glow: 0 });
const side = Math.sign(offset);
const cut = Math.abs(offset);
debris.push({ x: newX + side * (rest / 2 + cut / 2), y, w: cut, colour: block.colour,
vx: side * 15, vy: 0, rot: 0, vr: side * 2 });
score += 1;
}
nextBlock();
}
function gameOver() {
state = 'over';
lock = 0.5; // short wait, so a quick double click doesn't restart right away
if (score > best) {
best = score;
newBest = score > 0;
saveBest(best);
}
rememberRound();
}
// Quit the round: back to the start screen, the points so far count for the best score
function quit() {
if (state === 'play') { // after a fall the round has already been counted
if (score > best) { best = score; saveBest(best); }
rememberRound();
}
newGame();
state = 'start';
}
// The ✕ at the top right (in screen pixels)
function button() {
const g = Math.max(34, Math.min(sw, sh) * 0.07);
return { x: sw - g / 2 - 12, y: g / 2 + 12, r: g / 2 };
}
function onButton(ev) {
const b = button();
return Math.hypot(ev.clientX - b.x, ev.clientY - b.y) <= b.r + 6;
}
function action() {
if (state === 'start') start();
else if (state === 'play') drop();
else if (state === 'over' && lock <= 0) { newGame(); start(); }
}
canvas.addEventListener('pointerdown', ev => {
ev.preventDefault();
if (state === 'play' && onButton(ev)) quit();
else action();
});
window.addEventListener('keydown', ev => {
if (ev.code === 'Space' || ev.key === ' ') {
ev.preventDefault();
if (!ev.repeat) action();
}
if (ev.key === 'Escape' && state !== 'start') quit();
});
window.addEventListener('resize', resize);
// ---------- Movement ----------
function update(dt) {
if (state === 'play' && block) {
const r = range(block.w);
block.x += direction * speed() * dt;
if (block.x > r) { block.x = r; direction = -1; }
if (block.x < -r) { block.x = -r; direction = 1; }
}
if (state === 'over') lock -= dt;
for (const d of debris) {
d.vy -= GRAVITY * dt;
d.x += d.vx * dt;
d.y += d.vy * dt;
d.rot += d.vr * dt;
}
debris = debris.filter(d => screenY(d.y) < sh + 200);
for (const b of tower) b.glow = Math.max(0, b.glow - dt * 1.8);
if (message) { message.t -= dt; if (message.t <= 0) message = null; }
camera += (topHeight() - camera) * Math.min(1, dt * 4);
}
// World → screen: the top of the tower sits at about 62 % of the height
function screenX(x) { return sw / 2 + x * u; }
function screenY(y) { return sh * 0.62 - (y - camera) * u; }
// ---------- Drawing ----------
// y is always the bottom edge of a block
function drawBlock(x, y, w, c, glow, rot) {
const bw = w * u, bh = HEIGHT * u;
ctx.save();
ctx.translate(screenX(x), screenY(y) - bh / 2);
if (rot) ctx.rotate(rot);
ctx.shadowColor = c;
ctx.shadowBlur = 14 + glow * 30;
ctx.fillStyle = c;
ctx.fillRect(-bw / 2, -bh / 2, bw, bh);
ctx.shadowBlur = 0;
ctx.fillStyle = 'rgba(255,255,255,0.18)'; // light strip on top
ctx.fillRect(-bw / 2, -bh / 2, bw, Math.max(1, bh * 0.18));
if (glow > 0) {
ctx.fillStyle = `rgba(255,255,255,${glow * 0.7})`;
ctx.fillRect(-bw / 2, -bh / 2, bw, bh);
}
const gap = Math.max(1.5, bh * 0.1); // thin dark gap at the bottom, so every block stands out
ctx.fillStyle = 'rgba(4,7,10,0.75)';
ctx.fillRect(-bw / 2, bh / 2 - gap, bw, gap);
ctx.restore();
}
function text(t, x, y, size, colourText, glow, align) {
ctx.font = `700 ${size}px system-ui, -apple-system, "Segoe UI", sans-serif`;
ctx.textAlign = align || 'center';
ctx.textBaseline = 'middle';
ctx.shadowColor = '#5abe41';
ctx.shadowBlur = glow || 0;
ctx.fillStyle = colourText;
ctx.fillText(t, x, y);
ctx.shadowBlur = 0;
}
// This session's high score list, returns the y position below it
function drawList(y, small) {
if (!rounds.length) return y;
text(TEXT.list, sw / 2, y, small * 0.75, '#6f9a66', 0);
y += small * 1.25;
rounds.forEach((r, i) => {
const now = r.id === lastRound;
// rank right-aligned, points left-aligned, arrow in its own column: so all lines line up
const f = now ? '#e8f5e2' : '#9fd88f', glow = now ? 12 : 0;
text(`${i + 1}.`, sw / 2 - small * 0.25, y, small * 0.9, f, glow, 'right');
text(String(r.p), sw / 2 + small * 0.25, y, small * 0.9, f, glow, 'left');
if (now) text('◀', sw / 2 + small * 2.0, y, small * 0.8, f, glow, 'left');
y += small * 1.15;
});
return y;
}
function draw() {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const bg = ctx.createLinearGradient(0, 0, 0, sh);
bg.addColorStop(0, '#04070a');
bg.addColorStop(1, '#0a1510');
ctx.fillStyle = bg;
ctx.fillRect(0, 0, sw, sh);
// Base under the first block, down to the bottom edge
const baseY = screenY(0);
if (baseY < sh) {
const g = ctx.createLinearGradient(0, baseY, 0, sh);
g.addColorStop(0, 'rgba(90,190,65,0.25)');
g.addColorStop(1, 'rgba(90,190,65,0.02)');
ctx.fillStyle = g;
ctx.fillRect(screenX(-WIDTH / 2), baseY, WIDTH * u, sh - baseY);
}
tower.forEach((b, i) => {
const y = i * HEIGHT;
if (screenY(y) < sh + HEIGHT * u) drawBlock(b.x, y, b.w, b.colour, b.glow, 0);
});
for (const d of debris) drawBlock(d.x, d.y, d.w, d.colour, 0, d.rot);
if (block) drawBlock(block.x, topHeight(), block.w, block.colour, 0, 0);
const big = Math.min(sh * 0.13, sw * 0.22);
const small = Math.max(14, Math.min(sh * 0.035, sw * 0.045));
if (state === 'start') {
text(TEXT.title, sw / 2, sh * 0.16, big * 0.75, '#e8f5e2', 24);
text(TEXT.start, sw / 2, sh * 0.16 + big * 0.7, small, '#9fd88f', 0);
if (best > 0) text(`${TEXT.best}: ${best}`, sw / 2, sh * 0.16 + big * 0.7 + small * 1.8, small * 0.85, '#6f9a66', 0);
drawList(sh * 0.16 + big * 0.7 + small * 3.6, small * 0.85);
} else {
text(String(score), sw / 2, sh * 0.13, big, 'rgba(232,245,226,0.92)', 26);
text(`${TEXT.best}: ${best}`, sw / 2, sh * 0.13 + big * 0.62, small * 0.85, '#6f9a66', 0);
}
if (state === 'play') {
const b = button(), a = b.r * 0.4;
ctx.strokeStyle = 'rgba(159,216,143,0.75)';
ctx.lineWidth = 2;
ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2); ctx.stroke();
ctx.beginPath();
ctx.moveTo(b.x - a, b.y - a); ctx.lineTo(b.x + a, b.y + a);
ctx.moveTo(b.x + a, b.y - a); ctx.lineTo(b.x - a, b.y + a);
ctx.stroke();
text(TEXT.quit, sw / 2, sh * 0.13 + big * 0.62 + small * 1.3, small * 0.75, '#56784f', 0);
}
if (message) {
ctx.globalAlpha = Math.min(1, message.t * 2);
text(message.text, sw / 2, screenY(topHeight() + HEIGHT * 3) - (1 - message.t) * 20, small * 1.3, '#d8ffc8', 18);
ctx.globalAlpha = 1;
}
if (state === 'over') {
const top = sh * 0.27;
const tall = small * (5.2 + (newBest ? 1.6 : 0) + (rounds.length ? 1.9 + rounds.length * 1.15 : 0));
ctx.fillStyle = 'rgba(4,7,10,0.7)';
ctx.fillRect(0, top, sw, tall);
let y = top + small * 1.6;
text(TEXT.over, sw / 2, y, small * 2, '#e8f5e2', 18);
y += small * 2;
if (newBest) { text(TEXT.newBest, sw / 2, y, small, '#9fd88f', 10); y += small * 1.6; }
if (rounds.length) y = drawList(y + small * 0.3, small) + small * 0.3;
if (lock <= 0) text(TEXT.again, sw / 2, y + small * 0.4, small * 0.9, '#9fd88f', 0);
}
}
let last = performance.now();
function loop(now) {
const dt = Math.min(0.05, (now - last) / 1000);
last = now;
update(dt);
draw();
requestAnimationFrame(loop);
}
resize();
best = loadBest();
rounds = loadRounds();
newGame();
state = 'start';
requestAnimationFrame(loop);
</script>
</body>
</html>
Keep building
A few ideas for your next message in the same chat:
Add sound: a short tone when a block lands, a little higher with every perfect drop in a row. Web Audio API only, no files.If I place three perfect drops in a row, the block grows a bit wider again.Let the background slowly change colour as the tower grows, from night blue to violet, and fade in stars higher up.After the fall, show the whole tower from bottom to top before "Again" appears.