Dodge the Blocks Game: Neon Stages, Lasers and Dash
Dodge the Falling Blocks Game · Games · Plain HTML, CSS & JS · Live preview
About this UI Snippet
Dodge the Blocks: Stage-Based Difficulty, Near-Miss Combos and a Synthwave Look

Dodge the Blocks is a one-axis survival game: hazards fall from the top and you steer a hover-car left and right at the bottom to avoid them for as long as you can. This snippet turns that simple idea into a full game on one HTML5 <canvas>, with no libraries and no image files. It has a synthwave look (a striped sun, mountains and a scrolling perspective grid), new hazard types introduced stage by stage, a dash move, near-miss combos, collectible stars and three power-ups. It plays with the arrow keys or A and D, on-screen buttons, or by dragging on the game.
Stages that teach one hazard at a time
Every 20 seconds a new stage begins, announced with a banner, a 1.6-second breather with no spawns, and a bonus. Stage 1 is plain falling blocks; stage 2 adds zig-zag blocks that swing side to side as they fall; stage 3 adds walls that span the whole width except for one gap; stage 4 adds lasers that flash a dashed warning line for almost a second before firing a full-height beam; stage 5 and beyond mixes everything. Fall speed and spawn rate rise continuously, and the gaps in walls slowly narrow, so the difficulty keeps climbing between stage changes too.
Movement that feels responsive
With keys or buttons the car accelerates and glides to a stop instead of moving a fixed step per frame, and it tilts with its speed. Dragging on the canvas pulls the car toward your finger like a spring. The dash fires the car sideways at high speed for 0.18 seconds, during which it cannot be hit, which makes it the tool for squeezing through a closing wall gap or escaping a laser warning; it then needs 1.6 seconds to recharge, shown by a meter.
Near misses, stars and power-ups
While a hazard passes the car's row, the game tracks the smallest horizontal gap between them. If it was under 9 pixels when the hazard leaves, you get a "CLOSE!" bonus, and consecutive near misses within two seconds build a combo up to x5 that also multiplies stars. Stars are worth 50 points. Power-ups are a shield (absorbs one hit and destroys the hazard), slow motion (the world runs at half speed for five seconds but your car does not) and shrink (a smaller car for six seconds). The score is ten points per second survived plus all bonuses, and the best score is saved in localStorage.
Neon rendering
Hazards are drawn as dark slabs with a bright outline, a soft outer glow and a highlight strip, in a random colour from a neon palette. The backdrop is painted once into an off-screen canvas; the grid floor scrolls faster as the game speeds up, speed streaks fall through the scene, and particles use additive blending so engine sparks, dash trails and the crash explosion glow. Sound effects come from the Web Audio API, with a mute button, and the game pauses on P, a button or a tab switch.
What's included
Features
Step by step
How to Use
- 1Start and steerPress Start. Move with the arrow keys or A and D, the ◀ ▶ buttons, or by dragging on the game. The car accelerates and glides, so tap for small moves.
- 2Survive the stagesEvery 20 seconds a new stage adds a hazard: zig-zag blocks, then walls with a single gap, then lasers. A banner names the new hazard and you get a short breather.
- 3Dash through troublePress Space, Shift, the up arrow or DASH to shoot sideways for a split second; you cannot be hit while dashing. The meter at the bottom left shows when it is ready again.
- 4Watch for laser warningsA blinking dashed red line with a ! means a laser will fire there in under a second. Move away or dash across it before the beam appears.
- 5Skim for combosPass a block with less than 9 pixels to spare for a CLOSE! bonus. Near misses in quick succession build a combo up to x5, which also multiplies stars.
- 6Grab stars and power-upsStars are 50 points. S is a shield, T is slow motion for the world and M shrinks your car. Your best score is saved in your browser.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
While a falling hazard overlaps the car's row vertically, the game measures the horizontal gap between the two rectangles and keeps the smallest one. When the hazard has passed below the car, a gap under 9 pixels counts as a near miss, adds 10 points times the current combo and raises the combo by one (up to x5).
A laser first spends about 0.95 seconds as a blinking dashed warning line with an exclamation mark, then fires a full-height beam for about 0.38 seconds. It only collides while the beam is active, so you always have time to move or dash away.
Dash launches the car sideways in the direction you last moved at high speed for 0.18 seconds. During a dash the car cannot be hit, so you can pass through a wall edge or a laser beam. It then recharges for 1.6 seconds.
Ten points per second survived, plus near-miss bonuses, stars (50 times the combo), and a stage bonus of 100 times the stage number reached minus one. The best score is stored in localStorage.
fallSpeed() sets how fast hazards fall over time, the spawn interval in update() sets how often they appear, STAGE_LEN sets how long each stage lasts, and spawn() holds the chance of each hazard type. Larger wall gaps and a shorter DASH_CD make it easier.
Copy & paste
Source Code
<div class="game-card">
<div class="game-header">
<div class="game-title">
<span class="game-icon">🟪</span>
<h2>Dodge the Blocks</h2>
</div>
<div class="best-badge">Best: <span id="best-score">0</span></div>
</div>
<div class="canvas-wrap">
<canvas id="game-canvas" width="360" height="480"></canvas>
<div class="hud">
<span id="hud-score">0</span>
<span id="hud-time">0.0s</span>
</div>
<div class="overlay" id="start-overlay">
<div class="overlay-card">
<p class="overlay-title">Dodge the Blocks</p>
<p class="overlay-sub">Steer your hover-car through falling neon. Every 20 seconds a new stage adds zig-zags, walls and lasers. Skim past blocks for combo points, and DASH to slip through gaps.</p>
<button class="btn btn-primary" id="btn-start">Start</button>
</div>
</div>
<div class="overlay hidden" id="pause-overlay">
<div class="overlay-card">
<p class="overlay-title">Paused</p>
<button class="btn btn-primary" id="btn-resume">Resume</button>
</div>
</div>
<div class="overlay hidden" id="gameover-overlay">
<div class="overlay-card">
<p class="overlay-title">Game Over</p>
<p class="overlay-sub" id="final-score">Survived 0.0s</p>
<p class="overlay-best" id="best-msg"></p>
<button class="btn btn-primary" id="btn-retry">Try again</button>
</div>
</div>
</div>
<div class="controls">
<button class="ctrl-btn" id="btn-left" aria-label="Move left">◀</button>
<button class="dash-btn" id="btn-dash" aria-label="Dash">DASH</button>
<button class="ctrl-btn" id="btn-right" aria-label="Move right">▶</button>
<div class="mini-col">
<button class="mini-btn" id="btn-pause" type="button" aria-label="Pause">⏸</button>
<button class="mini-btn" id="btn-mute" type="button" aria-label="Sound on" aria-pressed="false">🔊</button>
</div>
</div>
<p class="hint-text">← → / A D or drag to move · Space or Shift to dash · P to pause</p>
</div>* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, sans-serif; background: #f8fafc; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.game-card { width: 100%; max-width: 400px; background: #fff; border-radius: 18px; border: 1px solid #e2e8f0; box-shadow: 0 12px 40px rgba(15,23,42,0.08); padding: 18px; user-select: none; -webkit-user-select: none; }
.game-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.game-title { display: flex; align-items: center; gap: 8px; }
.game-icon { font-size: 20px; }
.game-title h2 { font-size: 17px; font-weight: 700; color: #0f172a; }
.best-badge { font-size: 12px; font-weight: 700; color: #db2777; background: #fdf2f8; padding: 5px 10px; border-radius: 20px; }
.canvas-wrap { position: relative; border-radius: 12px; overflow: hidden; line-height: 0; touch-action: none; background: #12052b; }
canvas { display: block; width: 100%; height: auto; }
.hud { position: absolute; inset: 8px 12px auto 12px; display: flex; justify-content: space-between; font-size: 14px; font-weight: 800; color: #fff; font-variant-numeric: tabular-nums; text-shadow: 0 1px 4px rgba(0,0,0,0.7); pointer-events: none; }
.overlay { position: absolute; inset: 0; background: rgba(18,5,43,0.86); display: flex; align-items: center; justify-content: center; padding: 20px; line-height: 1.4; }
.overlay.hidden { display: none; }
.overlay-card { text-align: center; }
.overlay-title { font-size: 22px; font-weight: 800; color: #fff; margin-bottom: 8px; }
.overlay-sub { font-size: 13px; color: #f5d0fe; margin-bottom: 4px; max-width: 290px; }
.overlay-best { font-size: 12px; color: #fbbf24; font-weight: 700; margin: 6px 0 0; min-height: 16px; }
.btn { padding: 11px 26px; font-size: 14px; font-weight: 700; border-radius: 10px; cursor: pointer; font-family: inherit; transition: background 0.15s; border: none; margin-top: 14px; }
.btn-primary { background: #db2777; color: #fff; }
.btn-primary:hover { background: #be185d; }
.controls { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.ctrl-btn, .dash-btn { -webkit-tap-highlight-color: transparent; touch-action: none; cursor: pointer; font-family: inherit; border: 1px solid #e2e8f0; transition: transform 0.06s, background 0.12s; height: 56px; border-radius: 14px; }
.ctrl-btn { width: 64px; background: #f8fafc; color: #334155; font-size: 22px; font-weight: 700; box-shadow: 0 2px 0 #e2e8f0; }
.ctrl-btn:active { transform: translateY(2px); box-shadow: none; background: #fce7f3; }
.dash-btn { flex: 1; background: #db2777; color: #fff; font-size: 15px; font-weight: 800; letter-spacing: 1.5px; border-color: #db2777; box-shadow: 0 3px 0 #9d174d; }
.dash-btn:active { transform: translateY(3px); box-shadow: none; }
.mini-col { display: flex; flex-direction: column; gap: 6px; }
.mini-btn { width: 34px; height: 25px; border: 1px solid #e2e8f0; background: #f8fafc; border-radius: 8px; font-size: 12px; line-height: 1; cursor: pointer; }
.mini-btn[aria-pressed="true"] { opacity: 0.55; }
.hint-text { font-size: 11px; color: #94a3b8; text-align: center; margin-top: 10px; }const $ = id => document.getElementById(id);
const canvas = $('game-canvas');
const ctx = canvas.getContext('2d');
const W = canvas.width, H = canvas.height;
const BEST_KEY = 'dodge-blocks-best-score';
const PY = H - 54, PW = 34, PH = 16; // player car: y, width, height
const STAGE_LEN = 20, DASH_TIME = 0.18, DASH_CD = 1.6;
const NEON = ['#f472b6', '#22d3ee', '#a78bfa', '#fb923c', '#4ade80', '#facc15'];
const STAGES = ['', 'Falling blocks', 'NEW: zig-zag blocks', 'NEW: walls with a gap', 'NEW: lasers', 'Everything, faster'];
let player, hazards, items, parts, pops, bgCanvas;
let running = false, paused = false, rafId, lastFrame, phase = 'play', phaseT = 0;
let elapsed, spawnT, bonus, combo, comboT, stage, banner, breather, shield, slowT, shrinkT, shake, time = 0;
let keys = { left: false, right: false }, dragX = null, muted = false, ac = null;
const startOverlay = $('start-overlay'), pauseOverlay = $('pause-overlay'), gameoverOverlay = $('gameover-overlay');
const hudTime = $('hud-time'), hudScore = $('hud-score'), finalScoreEl = $('final-score'), bestMsgEl = $('best-msg'), bestScoreEl = $('best-score'), muteBtn = $('btn-mute');
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
function getBest() { try { return parseInt(localStorage.getItem(BEST_KEY), 10) || 0; } catch (e) { return 0; } }
function setBest(v) { try { localStorage.setItem(BEST_KEY, String(v)); } catch (e) {} }
const score = () => Math.floor(elapsed * 10) + bonus;
// ── Sound ──
function tone(f, dur, type, to, vol, delay) {
if (muted) return;
try {
if (!ac) ac = new (window.AudioContext || window.webkitAudioContext)();
if (ac.state === 'suspended') ac.resume();
const t0 = ac.currentTime + (delay || 0), o = ac.createOscillator(), g = ac.createGain();
o.type = type || 'square'; o.frequency.setValueAtTime(f, t0);
if (to) o.frequency.exponentialRampToValueAtTime(to, t0 + dur);
g.gain.setValueAtTime(vol || 0.04, t0); g.gain.exponentialRampToValueAtTime(0.0001, t0 + dur);
o.connect(g); g.connect(ac.destination); o.start(t0); o.stop(t0 + dur + 0.02);
} catch (e) {}
}
const sfx = {
dash: () => tone(300, 0.15, 'sawtooth', 1200, 0.035),
near: c => tone(700 + c * 90, 0.08, 'triangle', 0, 0.045),
star: () => [988, 1318].forEach((f, i) => tone(f, 0.08, 'square', 0, 0.035, i * 0.06)),
power: () => [523, 784, 1046].forEach((f, i) => tone(f, 0.1, 'triangle', 0, 0.05, i * 0.07)),
warn: () => tone(1500, 0.05, 'square', 0, 0.02),
laser: () => tone(220, 0.3, 'sawtooth', 110, 0.04),
crash: () => { tone(200, 0.6, 'sawtooth', 40, 0.08); tone(110, 0.5, 'square', 30, 0.05); },
shieldHit: () => tone(400, 0.25, 'triangle', 120, 0.06),
stage: () => [392, 523, 659, 784].forEach((f, i) => tone(f, 0.13, 'square', 0, 0.035, i * 0.09)),
};
// ── Synthwave backdrop painted once: sky, striped sun, mountains ──
function buildBackground() {
const cv = document.createElement('canvas'); cv.width = W; cv.height = H;
const g = cv.getContext('2d');
const sky = g.createLinearGradient(0, 0, 0, H * 0.72); sky.addColorStop(0, '#12052b'); sky.addColorStop(0.6, '#3b0764'); sky.addColorStop(1, '#9d174d');
g.fillStyle = sky; g.fillRect(0, 0, W, H);
for (let i = 0; i < 70; i++) { g.fillStyle = 'rgba(255,255,255,' + (0.3 + (i % 5) * 0.12).toFixed(2) + ')'; g.fillRect((i * 97.3) % W, (i * 41.7) % (H * 0.55), i % 7 ? 1 : 2, i % 7 ? 1 : 2); }
const cy = H * 0.6, r = 62, sun = g.createLinearGradient(0, cy - r, 0, cy + r); sun.addColorStop(0, '#fde047'); sun.addColorStop(1, '#f472b6');
g.save(); g.beginPath(); g.arc(W / 2, cy, r, 0, 6.3); g.clip(); g.fillStyle = sun; g.fillRect(W / 2 - r, cy - r, r * 2, r * 2);
g.fillStyle = '#3b0764'; for (let k = 0; k < 6; k++) g.fillRect(W / 2 - r, cy + 6 + k * 9, r * 2, 2 + k * 0.8); g.restore();
g.fillStyle = '#1e0b3d'; g.beginPath(); g.moveTo(0, H * 0.72);
[[0, 0.62], [0.12, 0.57], [0.22, 0.64], [0.34, 0.55], [0.46, 0.66], [0.6, 0.6], [0.72, 0.53], [0.84, 0.63], [1, 0.58]].forEach(p => g.lineTo(p[0] * W, p[1] * H));
g.lineTo(W, H * 0.72); g.closePath(); g.fill();
g.strokeStyle = 'rgba(244,114,182,0.5)'; g.lineWidth = 1; g.stroke();
return cv;
}
function drawGrid() {
const top = H * 0.72;
ctx.fillStyle = '#12052b'; ctx.fillRect(0, top, W, H - top);
ctx.strokeStyle = 'rgba(236,72,153,0.55)'; ctx.lineWidth = 1;
ctx.beginPath();
for (let i = -8; i <= 8; i++) { ctx.moveTo(W / 2 + i * 12, top); ctx.lineTo(W / 2 + i * 70, H); }
const off = (time * (1.2 + elapsed * 0.03)) % 1;
for (let k = 0; k < 9; k++) { const t = (k + off) / 9, y = top + (H - top) * t * t; ctx.moveTo(0, y); ctx.lineTo(W, y); }
ctx.stroke();
ctx.fillStyle = 'rgba(244,114,182,0.7)'; ctx.fillRect(0, top - 1, W, 2);
}
// ── Hazards ──
const fallSpeed = () => Math.min(8, 2.4 + elapsed * 0.05);
function spawn() {
const sp = fallSpeed(), r = Math.random();
if (stage >= 4 && r < 0.14) { // laser: a warning line, then a beam
hazards.push({ type: 'laser', x: 20 + Math.random() * (W - 40), w: 14, warn: 0.95, on: 0.38, warned: false });
} else if (stage >= 3 && r < 0.3) { // wall with a gap
const gw = Math.max(70, 112 - elapsed * 0.4), gx = 14 + Math.random() * (W - gw - 28);
hazards.push({ type: 'wall', y: -20, h: 16, gx, gw, vy: sp * 0.75, col: NEON[(Math.random() * NEON.length) | 0], minD: 99, done: false });
} else if (stage >= 2 && r < 0.48) { // zig-zag block
const w = 26 + Math.random() * 24;
hazards.push({ type: 'zig', x0: 30 + Math.random() * (W - 60 - w), x: 0, y: -20, w, h: 18, vy: sp * 0.9, amp: 30 + Math.random() * 40, ph: Math.random() * 6, col: NEON[(Math.random() * NEON.length) | 0], minD: 99, done: false });
} else {
const w = 26 + Math.random() * 54;
hazards.push({ type: 'block', x: Math.random() * (W - w), y: -20, w, h: 18, vy: sp * (0.8 + Math.random() * 0.45), col: NEON[(Math.random() * NEON.length) | 0], minD: 99, done: false });
}
const p = Math.random();
if (p < 0.1) items.push({ type: 'star', x: 20 + Math.random() * (W - 40), y: -16, vy: sp * 0.8 });
else if (p < 0.135) items.push({ type: ['shield', 'slow', 'shrink'][(Math.random() * 3) | 0], x: 20 + Math.random() * (W - 40), y: -16, vy: sp * 0.7 });
}
function rectsOf(h) { // the collision rectangles of a hazard
if (h.type === 'wall') return [{ x: -10, y: h.y, w: h.gx + 10, h: h.h }, { x: h.gx + h.gw, y: h.y, w: W - h.gx - h.gw + 10, h: h.h }];
if (h.type === 'laser') return h.warn <= 0 && h.on > 0 ? [{ x: h.x - h.w / 2, y: -10, w: h.w, h: H + 20 }] : [];
return [{ x: h.x, y: h.y, w: h.w, h: h.h }];
}
function resetState() {
player = { x: W / 2, vx: 0, dir: 1, dashT: 0, dashCd: 0, tilt: 0 };
hazards = []; items = []; parts = []; pops = [];
elapsed = 0; spawnT = 0.6; bonus = 0; combo = 0; comboT = 0; stage = 1; breather = 0;
shield = false; slowT = 0; shrinkT = 0; shake = 0; phase = 'play';
banner = { text: 'STAGE 1', sub: STAGES[1], t: 1.8 };
syncHud();
}
function syncHud() { hudTime.textContent = elapsed.toFixed(1) + 's'; hudScore.textContent = score(); }
function burst(x, y, n, colors, spd) {
for (let i = 0; i < n && parts.length < 450; i++) {
const a = Math.random() * 6.283, v = (0.3 + Math.random()) * (spd || 3);
parts.push({ x, y, vx: Math.cos(a) * v, vy: Math.sin(a) * v, life: 20 + Math.random() * 22, max: 42, c: colors[(Math.random() * colors.length) | 0], s: 1.5 + Math.random() * 2.5 });
}
}
function popup(x, y, text, color) { pops.push({ x, y, text, color: color || '#fde68a', t: 0.9 }); }
function dash() {
if (!running || phase !== 'play' || player.dashCd > 0) return;
player.dashT = DASH_TIME; player.dashCd = DASH_CD; player.vx = player.dir * 15; sfx.dash();
}
const pw = () => PW * (shrinkT > 0 ? 0.6 : 1), ph = () => PH * (shrinkT > 0 ? 0.6 : 1);
function crash(hz) {
if (player.dashT > 0) return false; // dashing makes you untouchable
if (shield) { shield = false; sfx.shieldHit(); burst(player.x, PY, 20, ['#67e8f9', '#fff'], 3); hz.dead = true; return false; }
phase = 'dying'; phaseT = 1.0; shake = 14; sfx.crash();
burst(player.x, PY, 60, ['#22d3ee', '#f472b6', '#fde047', '#ffffff'], 4.5);
return true;
}
// ── Frame update ──
function update(dt) {
time += dt;
const step = dt * 60;
parts.forEach(p => { p.x += p.vx * step; p.y += p.vy * step; p.vx *= 0.96; p.vy *= 0.96; p.life -= step; });
parts = parts.filter(p => p.life > 0);
pops.forEach(p => { p.t -= dt; p.y -= 0.6 * step; }); pops = pops.filter(p => p.t > 0);
if (banner) { banner.t -= dt; if (banner.t <= 0) banner = null; }
if (shake > 0.3) shake *= Math.pow(0.86, step); else shake = 0;
if (phase === 'dying') { phaseT -= dt; if (phaseT <= 0) endGame(); return; }
elapsed += dt;
const ws = slowT > 0 ? 0.5 : 1; // slow-mo slows the world, not you
if (slowT > 0) slowT -= dt;
if (shrinkT > 0) shrinkT -= dt;
if (comboT > 0) { comboT -= dt; if (comboT <= 0) combo = 0; }
// Stages: every 20 seconds, with a short breather and a banner
const st = 1 + Math.floor(elapsed / STAGE_LEN);
if (st !== stage) { stage = st; breather = 1.6; banner = { text: 'STAGE ' + st, sub: STAGES[Math.min(st, 5)], t: 2 }; sfx.stage(); bonus += 100 * (st - 1); popup(W / 2, H / 2 + 30, '+' + 100 * (st - 1), '#fde047'); }
// Player: acceleration and drag for keys, a spring toward your finger for touch
const p = player, dir = (keys.right ? 1 : 0) - (keys.left ? 1 : 0);
if (dir) p.dir = dir;
if (p.dashT > 0) { p.dashT -= dt; parts.push({ x: p.x, y: PY + 4, vx: -p.dir * 0.5, vy: 0, life: 14, max: 14, c: '#f472b6', s: 6 }); }
else if (dragX !== null) { p.vx = (dragX - p.x) * 0.3; if (Math.abs(dragX - p.x) > 2) p.dir = Math.sign(dragX - p.x); }
else { p.vx += dir * 1.2 * step; p.vx *= Math.pow(0.8, step); }
if (p.dashCd > 0) p.dashCd -= dt;
p.x = clamp(p.x + p.vx * step, pw() / 2 + 2, W - pw() / 2 - 2);
p.tilt += (clamp(p.vx * 0.05, -0.4, 0.4) - p.tilt) * 0.25;
if (Math.random() < 0.6) parts.push({ x: p.x + (Math.random() - 0.5) * pw() * 0.6, y: PY + ph() / 2, vx: 0, vy: 1.2 + Math.random(), life: 12, max: 12, c: Math.random() < 0.5 ? '#22d3ee' : '#a78bfa', s: 2 });
// Spawning
if (breather > 0) breather -= dt;
else {
spawnT -= dt * ws;
if (spawnT <= 0) { spawn(); spawnT = Math.max(0.26, 1.0 - elapsed * 0.012) * (0.75 + Math.random() * 0.5); }
}
// Hazards
const pr = { x: p.x - pw() / 2 + 2, y: PY - ph() / 2 + 2, w: pw() - 4, h: ph() - 4 };
for (const h of hazards) {
if (h.type === 'laser') {
if (h.warn > 0) { h.warn -= dt * ws; if (Math.floor(h.warn * 10) % 3 === 0 && !h.beeped) { h.beeped = true; sfx.warn(); } if (h.warn <= 0) sfx.laser(); }
else h.on -= dt * ws;
if (h.on <= -0.2) h.dead = true;
} else {
h.y += h.vy * ws * step;
if (h.type === 'zig') h.x = h.x0 + Math.sin(h.ph + h.y * 0.03) * h.amp;
if (h.y > H + 30) h.dead = true;
}
for (const r of rectsOf(h)) {
if (pr.x < r.x + r.w && pr.x + pr.w > r.x && pr.y < r.y + r.h && pr.y + pr.h > r.y) { if (crash(h)) return; }
// near miss: the horizontal gap while the hazard passes your row
else if (h.type !== 'laser' && r.y + r.h > pr.y && r.y < pr.y + pr.h) { const d = Math.max(r.x - (pr.x + pr.w), pr.x - (r.x + r.w)); if (d >= 0 && d < h.minD) h.minD = d; }
}
if (h.type !== 'laser' && !h.done && h.y > PY + 14) {
h.done = true;
if (h.minD < 9) { combo = Math.min(5, combo + 1); comboT = 2; const pts = 10 * combo; bonus += pts; sfx.near(combo); popup(p.x, PY - 22, 'CLOSE! +' + pts, '#fde047'); }
}
}
hazards = hazards.filter(h => !h.dead);
// Pickups
for (const it of items) {
it.y += it.vy * ws * step;
if (Math.hypot(it.x - p.x, it.y - PY) < 18) {
it.dead = true;
if (it.type === 'star') { const pts = 50 * Math.max(1, combo); bonus += pts; sfx.star(); popup(it.x, it.y - 10, '+' + pts, '#fde047'); burst(it.x, it.y, 10, ['#fde047', '#fff'], 2); }
else {
sfx.power(); burst(it.x, it.y, 14, ['#67e8f9', '#f0abfc', '#fff'], 2.5);
if (it.type === 'shield') { shield = true; popup(it.x, it.y - 10, 'SHIELD!', '#67e8f9'); }
else if (it.type === 'slow') { slowT = 5; popup(it.x, it.y - 10, 'SLOW-MO!', '#c4b5fd'); }
else { shrinkT = 6; popup(it.x, it.y - 10, 'SHRINK!', '#86efac'); }
}
}
if (it.y > H + 20) it.dead = true;
}
items = items.filter(i => !i.dead);
syncHud();
}
// ── Drawing ──
function neonRect(x, y, w, h, col) {
ctx.fillStyle = col; ctx.globalAlpha = 0.18; ctx.fillRect(x - 3, y - 3, w + 6, h + 6);
ctx.globalAlpha = 0.85; ctx.fillStyle = '#1a0838'; ctx.fillRect(x, y, w, h);
ctx.globalAlpha = 1; ctx.strokeStyle = col; ctx.lineWidth = 2; ctx.strokeRect(x + 1, y + 1, w - 2, h - 2);
ctx.fillStyle = col; ctx.globalAlpha = 0.35; ctx.fillRect(x + 3, y + 3, w - 6, 3); ctx.globalAlpha = 1;
}
function drawPlayer() {
if (phase === 'dying') return;
const p = player, s = shrinkT > 0 ? 0.6 : 1;
ctx.save(); ctx.translate(p.x, PY); ctx.rotate(p.tilt); ctx.scale(s, s);
const glow = ctx.createRadialGradient(0, 10, 2, 0, 10, 34); glow.addColorStop(0, 'rgba(236,72,153,0.6)'); glow.addColorStop(1, 'rgba(236,72,153,0)');
ctx.fillStyle = glow; ctx.fillRect(-34, -10, 68, 50);
ctx.fillStyle = p.dashT > 0 ? '#ffffff' : '#0e7490';
ctx.beginPath(); ctx.moveTo(-17, 7); ctx.lineTo(-12, -6); ctx.lineTo(12, -6); ctx.lineTo(17, 7); ctx.closePath(); ctx.fill();
ctx.strokeStyle = '#22d3ee'; ctx.lineWidth = 2; ctx.stroke();
ctx.fillStyle = '#a5f3fc'; ctx.beginPath(); ctx.moveTo(-7, -6); ctx.lineTo(-4, -11); ctx.lineTo(4, -11); ctx.lineTo(7, -6); ctx.fill();
ctx.fillStyle = '#f472b6'; ctx.fillRect(-16, 4, 5, 3); ctx.fillRect(11, 4, 5, 3);
ctx.restore();
if (shield) { ctx.strokeStyle = 'rgba(103,232,249,' + (0.6 + Math.sin(time * 6) * 0.25).toFixed(2) + ')'; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(p.x, PY, 22 * s, 0, 6.3); ctx.stroke(); }
}
function draw() {
ctx.drawImage(bgCanvas, 0, 0);
drawGrid();
// speed streaks
ctx.fillStyle = 'rgba(255,255,255,0.08)';
for (let i = 0; i < 12; i++) { const x = (i * 71.3) % W, len = 10 + fallSpeed() * 3, y = ((time * 160 * (1 + i % 3) + i * 97) % (H + len)) - len; ctx.fillRect(x, y, 1, len); }
ctx.save();
if (shake) ctx.translate((Math.random() - 0.5) * shake, (Math.random() - 0.5) * shake);
hazards.forEach(h => {
if (h.type === 'laser') {
if (h.warn > 0) {
if (Math.floor(h.warn * 12) % 2) { ctx.strokeStyle = 'rgba(248,113,113,0.8)'; ctx.setLineDash([6, 6]); ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(h.x, 0); ctx.lineTo(h.x, H); ctx.stroke(); ctx.setLineDash([]); }
ctx.fillStyle = '#f87171'; ctx.font = 'bold 14px system-ui'; ctx.textAlign = 'center'; ctx.fillText('!', h.x, 46);
} else if (h.on > 0) {
ctx.globalCompositeOperation = 'lighter';
const g = ctx.createLinearGradient(h.x - h.w, 0, h.x + h.w, 0); g.addColorStop(0, 'rgba(239,68,68,0)'); g.addColorStop(0.5, 'rgba(255,255,255,0.95)'); g.addColorStop(1, 'rgba(239,68,68,0)');
ctx.fillStyle = 'rgba(239,68,68,0.45)'; ctx.fillRect(h.x - h.w, 0, h.w * 2, H);
ctx.fillStyle = g; ctx.fillRect(h.x - h.w / 2, 0, h.w, H);
ctx.globalCompositeOperation = 'source-over';
}
return;
}
if (h.type === 'wall') { rectsOf(h).forEach(r => neonRect(Math.max(0, r.x), r.y, Math.min(W, r.x + r.w) - Math.max(0, r.x), r.h, h.col)); return; }
neonRect(h.x, h.y, h.w, h.h, h.col);
});
items.forEach(it => {
const bob = Math.sin(time * 6 + it.x) * 2;
if (it.type === 'star') {
ctx.fillStyle = '#fde047'; ctx.beginPath();
for (let k = 0; k < 10; k++) { const a = -Math.PI / 2 + k * Math.PI / 5 + time * 2, r = k % 2 ? 4 : 9; ctx.lineTo(it.x + Math.cos(a) * r, it.y + bob + Math.sin(a) * r); }
ctx.fill();
} else {
const c = it.type === 'shield' ? '#67e8f9' : it.type === 'slow' ? '#c4b5fd' : '#86efac';
ctx.strokeStyle = c; ctx.lineWidth = 2; ctx.fillStyle = 'rgba(18,5,43,0.85)';
ctx.beginPath(); ctx.arc(it.x, it.y + bob, 10, 0, 6.3); ctx.fill(); ctx.stroke();
ctx.fillStyle = c; ctx.font = 'bold 11px system-ui'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
ctx.fillText(it.type === 'shield' ? 'S' : it.type === 'slow' ? 'T' : 'M', it.x, it.y + bob + 1);
}
});
ctx.globalCompositeOperation = 'lighter';
parts.forEach(q => { ctx.globalAlpha = clamp(q.life / q.max, 0, 1); ctx.fillStyle = q.c; ctx.fillRect(q.x - q.s / 2, q.y - q.s / 2, q.s, q.s); });
ctx.globalAlpha = 1; ctx.globalCompositeOperation = 'source-over';
drawPlayer();
ctx.font = '800 11px system-ui, sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
pops.forEach(q => { ctx.globalAlpha = clamp(q.t * 2, 0, 1); ctx.fillStyle = q.color; ctx.fillText(q.text, q.x, q.y); });
ctx.globalAlpha = 1;
ctx.restore();
// Dash meter, power-up timers, combo, stage
const ready = player.dashCd <= 0;
ctx.fillStyle = 'rgba(255,255,255,0.15)'; ctx.fillRect(10, H - 10, 60, 4);
ctx.fillStyle = ready ? '#f472b6' : '#9d174d'; ctx.fillRect(10, H - 10, 60 * (1 - clamp(player.dashCd / DASH_CD, 0, 1)), 4);
ctx.font = 'bold 9px system-ui'; ctx.textAlign = 'left'; ctx.fillStyle = ready ? '#fbcfe8' : '#9d174d'; ctx.fillText(ready ? 'DASH READY' : 'DASH', 10, H - 17);
const tags = [];
if (shield) tags.push('SHIELD'); if (slowT > 0) tags.push('SLOW ' + Math.ceil(slowT)); if (shrinkT > 0) tags.push('SHRINK ' + Math.ceil(shrinkT));
if (tags.length) { ctx.textAlign = 'center'; ctx.fillStyle = '#e9d5ff'; ctx.fillText(tags.join(' · '), W / 2, H - 12); }
ctx.textAlign = 'right'; ctx.font = '800 12px system-ui';
ctx.fillStyle = '#e9d5ff'; ctx.fillText('STAGE ' + stage, W - 10, H - 24);
if (combo > 1) { ctx.fillStyle = '#fde047'; ctx.fillText('x' + combo, W - 10, H - 10); }
if (banner) {
ctx.globalAlpha = clamp(banner.t * 2, 0, 1); ctx.textAlign = 'center';
ctx.font = '900 30px system-ui, sans-serif'; ctx.fillStyle = '#fdf4ff'; ctx.fillText(banner.text, W / 2, H * 0.36);
ctx.font = '700 13px system-ui, sans-serif'; ctx.fillStyle = '#f9a8d4'; ctx.fillText(banner.sub, W / 2, H * 0.36 + 26);
ctx.globalAlpha = 1;
}
}
// ── Game flow ──
function loop(ts) {
if (!running) return;
if (!lastFrame) lastFrame = ts;
const dt = Math.min((ts - lastFrame) / 1000, 1 / 30);
lastFrame = ts;
update(dt);
if (running) { draw(); rafId = requestAnimationFrame(loop); }
}
function startGame() {
resetState(); running = true; paused = false; lastFrame = 0;
startOverlay.classList.add('hidden'); pauseOverlay.classList.add('hidden'); gameoverOverlay.classList.add('hidden');
rafId = requestAnimationFrame(loop);
}
function setPaused(pz) {
if (!running && !paused) return;
if (pz === paused) return;
paused = pz;
if (pz) { running = false; cancelAnimationFrame(rafId); keys = { left: false, right: false }; dragX = null; draw(); pauseOverlay.classList.remove('hidden'); }
else { pauseOverlay.classList.add('hidden'); running = true; lastFrame = 0; rafId = requestAnimationFrame(loop); }
}
function endGame() {
running = false; cancelAnimationFrame(rafId); draw();
const best = getBest(), sc = score();
finalScoreEl.textContent = 'Score ' + sc + ' · survived ' + elapsed.toFixed(1) + 's · stage ' + stage;
if (sc > best) { setBest(sc); bestScoreEl.textContent = sc; bestMsgEl.textContent = '🏆 New best score!'; }
else bestMsgEl.textContent = '';
gameoverOverlay.classList.remove('hidden');
}
// ── Input ──
document.addEventListener('keydown', e => {
if (e.key === 'p' || e.key === 'P' || e.key === 'Escape') { if (running || paused) setPaused(!paused); return; }
if (!running) {
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); if (paused) setPaused(false); else if (!startOverlay.classList.contains('hidden') || !gameoverOverlay.classList.contains('hidden')) startGame(); }
return;
}
if (e.key === 'ArrowLeft' || e.key === 'a' || e.key === 'A') { keys.left = true; e.preventDefault(); }
if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') { keys.right = true; e.preventDefault(); }
if ((e.key === ' ' || e.key === 'Shift' || e.key === 'ArrowUp') && !e.repeat) { e.preventDefault(); dash(); }
});
document.addEventListener('keyup', e => {
if (e.key === 'ArrowLeft' || e.key === 'a' || e.key === 'A') keys.left = false;
if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') keys.right = false;
});
document.addEventListener('visibilitychange', () => { if (document.hidden && running) setPaused(true); });
function hold(id, on, off) {
const el = $(id);
el.addEventListener('pointerdown', e => { e.preventDefault(); on(); });
['pointerup', 'pointercancel', 'pointerleave'].forEach(ev => el.addEventListener(ev, e => { e.preventDefault(); off(); }));
el.addEventListener('contextmenu', e => e.preventDefault());
}
hold('btn-left', () => { keys.left = true; }, () => { keys.left = false; });
hold('btn-right', () => { keys.right = true; }, () => { keys.right = false; });
$('btn-dash').addEventListener('pointerdown', e => { e.preventDefault(); dash(); });
const toX = e => { const r = canvas.getBoundingClientRect(); return ((e.clientX - r.left) / r.width) * W; };
canvas.addEventListener('pointerdown', e => { dragX = toX(e); });
canvas.addEventListener('pointermove', e => { if (e.buttons > 0 || e.pointerType === 'touch') dragX = toX(e); });
['pointerup', 'pointerleave', 'pointercancel'].forEach(ev => canvas.addEventListener(ev, () => { dragX = null; }));
$('btn-start').addEventListener('click', startGame);
$('btn-retry').addEventListener('click', startGame);
$('btn-resume').addEventListener('click', () => setPaused(false));
$('btn-pause').addEventListener('click', () => { if (running || paused) setPaused(!paused); });
muteBtn.addEventListener('click', () => {
muted = !muted; muteBtn.textContent = muted ? '🔇' : '🔊';
muteBtn.setAttribute('aria-pressed', String(muted)); muteBtn.setAttribute('aria-label', muted ? 'Sound off' : 'Sound on');
});
bgCanvas = buildBackground();
bestScoreEl.textContent = getBest();
resetState();
draw();
/* wdp-share:start */
(function () {
var TITLE = 'Dodge the Falling Blocks Game', URL = 'https://webdevpuneet.com/ui-snippets/dodge-the-blocks-game/';
if (window.__wdpShare) return;
window.__wdpShare = true;
var blob = null, file = null, lastInfo = null;
/* ---- UI ---- */
var st = document.createElement('style');
st.textContent =
'.wdps-in,.wdps-btn,.wdps-box{touch-action:manipulation;-webkit-tap-highlight-color:transparent}.wdps-in{display:block;margin:12px auto 0;border:0;border-radius:12px;padding:11px 22px;font:800 15px/1 system-ui,sans-serif;background:#16a34a;color:#fff;cursor:pointer;box-shadow:0 3px 0 #11793a;pointer-events:auto;position:relative;z-index:5}' +
'.wdps-in:hover{background:#15803d}.wdps-in:active{transform:translateY(2px);box-shadow:0 1px 0 #11793a}' +
'.wdps-btn{position:fixed;right:12px;bottom:12px;z-index:2147483000;border:0;border-radius:99px;padding:9px 16px;font:700 13px/1 system-ui,sans-serif;background:#16a34a;color:#fff;cursor:pointer;box-shadow:0 3px 12px rgba(0,0,0,.3)}' +
'.wdps-btn.wdps-hot{animation:wdps-p 1.4s ease-in-out infinite}' +
'@keyframes wdps-p{0%,100%{box-shadow:0 0 0 0 rgba(22,163,74,.6),0 3px 12px rgba(0,0,0,.3)}50%{box-shadow:0 0 0 9px rgba(22,163,74,0),0 3px 12px rgba(0,0,0,.3)}}' +
'.wdps-btn:active{transform:translateY(1px)}' +
'.wdps-in[hidden],.wdps-btn[hidden],.wdps-box[hidden],.wdps-box [hidden]{display:none}' +
'.wdps-box{position:fixed;left:50%;bottom:12px;transform:translateX(-50%);z-index:2147483001;width:max-content;max-width:calc(100vw - 24px);box-sizing:border-box;background:#fff;color:#64748b;border:1px solid #d5e2ee;border-radius:12px;padding:8px 12px;font:700 12px/1.4 system-ui,sans-serif;box-shadow:0 8px 26px rgba(15,23,42,.25);display:flex;flex-wrap:wrap;gap:8px;align-items:center}' +
'.wdps-box button,.wdps-box a{border:1px solid #d5e2ee;background:#f8fafc;color:#1e293b;border-radius:99px;padding:5px 12px;font:700 12px system-ui,sans-serif;text-decoration:none;cursor:pointer}' +
'.wdps-box .wdps-x{border:0;background:none;font-size:19px;line-height:1;padding:0 4px;margin-left:auto;color:#475569}' +
'@media (max-width:700px){.wdps-box{width:calc(100vw - 24px);justify-content:center;padding:10px 12px}.wdps-box>span{width:100%;text-align:center}.wdps-box button,.wdps-box a{padding:9px 14px;font-size:13px}.wdps-box .wdps-x{position:absolute;top:4px;right:4px}.wdps-in{width:100%;max-width:320px}}';
document.head.appendChild(st);
var pill = document.createElement('button');
pill.type = 'button'; pill.className = 'wdps-btn'; pill.textContent = 'Share result';
var inl = document.createElement('button');
inl.type = 'button'; inl.className = 'wdps-in'; inl.textContent = 'Share result'; inl.hidden = true;
var box = document.createElement('div');
box.className = 'wdps-box'; box.hidden = true;
box.setAttribute('role', 'dialog'); box.setAttribute('aria-label', 'Share your result');
box.innerHTML =
'<span data-a="msg">Share your result:</span>' +
'<button type="button" data-a="copy">Copy image</button>' +
'<a data-a="dl" download="result.png">Download image</a>' +
'<button type="button" data-a="more" hidden>More…</button>' +
'<a data-a="wa" target="_blank" rel="noopener">WhatsApp</a>' +
'<a data-a="x" target="_blank" rel="noopener">X</a>' +
'<a data-a="fb" target="_blank" rel="noopener">Facebook</a>' +
'<button type="button" class="wdps-x" data-a="close" aria-label="Close share options">×</button>';
document.body.appendChild(box);
document.body.appendChild(pill);
function q(a) { return box.querySelector('[data-a="' + a + '"]'); }
function ours(el) { return el === inl || el === pill || !!el.closest('.wdps-box'); }
/* ---- reading the result from the screen ---- */
function shown(el) {
if (!el || !el.getClientRects().length) return false;
for (var n = el; n && n !== document.body; n = n.parentElement) {
var cs = getComputedStyle(n);
if (cs.display === 'none' || cs.visibility === 'hidden' || parseFloat(cs.opacity) < 0.1) return false;
if (n.hidden || n.getAttribute('aria-hidden') === 'true') return false;
}
return true;
}
function txt(el) { return (el.textContent || '').replace(/\s+/g, ' ').trim(); }
function sig(el) { return ((el.id || '') + ' ' + (typeof el.className === 'string' ? el.className : '')).toLowerCase(); }
var OUTCOME = /(you win|you won|win!|wins|victory|game over|time.?s up|cleared|complete|solved|defeat|you lose|you lost|lost|draw|crash|failed|success|congrat|score|result|finished|beat|all matched|well done|perfect|new best)/i;
var HEADSIG = /(title|headline|msg|message|banner|result|verdict|status)/;
var WIN = /(you win|you won|win!|wins|victory|cleared|complete|solved|success|congrat|beat|matched|perfect|new best|record)/i;
var LOSS = /(game over|time.?s up|defeat|you lose|you lost|lost|crash|failed|out of)/i;
var BOX = /(overlay|modal|banner|result|over|end|dialog|screen|popup|panel|card|finish|win)/;
function readResult() {
var head = '', sub = '', root = null, headEl = null;
var all = document.body.querySelectorAll('h1,h2,h3,h4,p,strong,div,span');
for (var i = 0; i < all.length && !head; i++) {
var el = all[i];
if (ours(el) || el.children.length > 2) continue;
if (!HEADSIG.test(sig(el))) continue;
/* a banner that holds its own title: use the title inside it instead */
var inner = false;
for (var c = 0; c < el.children.length; c++) if (HEADSIG.test(sig(el.children[c]))) inner = true;
if (inner) continue;
var t = txt(el);
if (t.length < 3 || t.length > 70 || !OUTCOME.test(t) || !shown(el)) continue;
head = t; headEl = el; root = el.parentElement;
}
if (root) {
var kids = root.querySelectorAll('p,span,div');
for (var j = 0; j < kids.length && !sub; j++) {
var k = kids[j];
if (ours(k) || k.children.length > 2 || !/(sub|text|summary|note|desc|detail)/.test(sig(k))) continue;
var tt = txt(k);
if (tt && tt !== head && tt.length >= 5 && tt.length <= 160 && shown(k)) sub = tt;
}
}
/* the result screen the button goes into: the nearest overlay / banner / result box around the headline */
var host = null;
for (var n = headEl, d = 0; n && n !== document.body && d < 4; n = n.parentElement, d++) {
if (d > 0 && BOX.test(sig(n)) && n.tagName !== 'BUTTON') { host = n; break; }
}
return { head: head, sub: sub, host: host };
}
var STATRE = /(score|moves|time|wpm|best|level|streak|accuracy|combo|coins|distance|height|points|stars|lives|wins|attempt|guess)/;
function readStats() {
var out = [], seen = {};
var all = document.body.querySelectorAll('span,strong,b,div,p,output');
for (var i = 0; i < all.length && out.length < 4; i++) {
var el = all[i];
if (ours(el) || el.children.length > 1) continue;
if (!STATRE.test(sig(el))) continue;
var t = txt(el);
if (!t || t.length > 28 || !/[0-9]/.test(t) || !shown(el)) continue;
if (!/[a-zA-Z]/.test(t) && el.parentElement) {
var parts = [], ch = el.parentElement.childNodes;
for (var p = 0; p < ch.length; p++) { var pv = (ch[p].textContent || '').replace(/\s+/g, ' ').trim(); if (pv) parts.push(pv); }
var pt = parts.join(' ');
if (pt.length <= 30 && /[a-zA-Z]/.test(pt)) t = pt;
}
var key = t.toLowerCase();
if (seen[key]) continue;
seen[key] = 1; out.push(t);
}
return out;
}
function bestCanvas() {
var cs = document.querySelectorAll('canvas'), best = null, area = 0;
for (var i = 0; i < cs.length; i++) {
var r = cs[i].getBoundingClientRect(), a = r.width * r.height;
if (a > area && a > 8000 && shown(cs[i])) { area = a; best = cs[i]; }
}
return best;
}
/* ---- show the button when a result is on screen ---- */
var lastHost = null, timer = 0;
function sync() {
timer = 0;
var r = readResult();
if (r.head && r.host) {
if (inl.parentElement !== r.host) r.host.appendChild(inl);
if (inl.hidden) inl.hidden = false;
if (!pill.hidden) pill.hidden = true;
} else {
if (!inl.hidden) inl.hidden = true;
if (pill.hidden) pill.hidden = false;
pill.classList.toggle('wdps-hot', !!r.head);
}
if (lastHost && !r.head && !box.hidden) box.hidden = true;
lastHost = r.head ? (r.host || document.body) : null;
}
function later() { if (!timer) timer = setTimeout(sync, 200); }
try {
new MutationObserver(function (list) {
for (var i = 0; i < list.length; i++) {
var t = list[i].target;
if (t.nodeType !== 1) t = t.parentElement;
if (t && !ours(t)) { later(); return; }
}
}).observe(document.body, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ['class', 'style', 'hidden'] });
} catch (e) {}
setInterval(later, 1500);
later();
/* ---- the card (1080 x 1080, drawn in memory, light like Pocket Artillery's) ---- */
var outline = 0;
function put(g, t, x, y) { if (outline) { g.lineJoin = 'round'; g.lineWidth = outline; g.strokeStyle = 'rgba(8,12,28,.85)'; g.strokeText(t, x, y); } g.fillText(t, x, y); }
function wrap(g, text, x, y, maxW, lh, maxLines) {
var words = text.split(' '), line = '', n = 0;
for (var i = 0; i < words.length; i++) {
var tst = line ? line + ' ' + words[i] : words[i];
if (g.measureText(tst).width > maxW && line) {
put(g, line, x, y); y += lh; n++; line = words[i];
if (n >= maxLines - 1) { line = words.slice(i).join(' '); break; }
} else line = tst;
}
if (line) {
while (line.length > 3 && g.measureText(line).width > maxW) line = line.slice(0, -2);
put(g, line, x, y); y += lh;
}
return y;
}
/* Biggest-screenshot card: the game frame fills the whole image, the text sits on top of it as an overlay. */
function bgOf(el) {
for (var n = el; n; n = n.parentElement) {
var c = getComputedStyle(n).backgroundColor;
if (c && c !== 'transparent' && c.slice(-4) !== ', 0)') return c;
}
return '#0f172a';
}
function drawBig(info, cv) {
var W = 1080, r = cv.width / cv.height, H = Math.max(810, Math.min(1350, Math.round(W / r)));
var c = document.createElement('canvas'); c.width = W; c.height = H;
var g = c.getContext('2d');
g.fillStyle = bgOf(cv.parentElement || cv); g.fillRect(0, 0, W, H);
var w = W, h = w / r;
if (h > H) { h = H; w = h * r; }
g.imageSmoothingEnabled = true; g.imageSmoothingQuality = 'high';
try { g.drawImage(cv, (W - w) / 2, (H - h) / 2, w, h); } catch (e) { return null; }
/* dark fades under the text so it stays readable on any game colours */
var g1 = g.createLinearGradient(0, 0, 0, H * 0.55); g1.addColorStop(0, 'rgba(8,12,28,.74)'); g1.addColorStop(1, 'rgba(8,12,28,0)');
g.fillStyle = g1; g.fillRect(0, 0, W, H * 0.55);
var g2 = g.createLinearGradient(0, H - 200, 0, H); g2.addColorStop(0, 'rgba(8,12,28,0)'); g2.addColorStop(1, 'rgba(8,12,28,.85)');
g.fillStyle = g2; g.fillRect(0, H - 200, W, 200);
var head = info.head || 'Can you beat me?';
var kind = /draw|tie/i.test(head) ? 'draw' : LOSS.test(head) && !WIN.test(head) ? 'loss' : 'win';
g.textAlign = 'center';
var y = 36;
g.fillStyle = 'rgba(255,255,255,.93)'; g.font = '800 34px system-ui, sans-serif'; outline = 7;
y = wrap(g, TITLE.toUpperCase(), W / 2, y + 34, 1000, 42, 1);
g.fillStyle = kind === 'draw' ? '#e2e8f0' : kind === 'loss' ? '#9db8ff' : '#ffc233';
var big = head.length > 22 ? 84 : 112; g.font = '900 ' + big + 'px system-ui, sans-serif'; outline = 16;
y = wrap(g, head, W / 2, y + big * 0.85, 1000, big * 1.05, 2);
if (info.sub) { g.fillStyle = '#fff'; g.font = '700 40px system-ui, sans-serif'; outline = 9; y = wrap(g, info.sub, W / 2, y + 6, 980, 50, 2); }
outline = 0;
if (info.stats.length) {
g.font = '800 38px system-ui, sans-serif';
var gap = 16, pad = 26, ws = [], tot = -gap;
for (var i = 0; i < info.stats.length; i++) { ws.push(g.measureText(info.stats[i]).width + pad * 2); tot += ws[i] + gap; }
var sc = tot > 960 ? 960 / tot : 1, px = (W - tot * sc) / 2, py = y + 14;
for (var k = 0; k < info.stats.length; k++) {
var w0 = ws[k] * sc;
g.fillStyle = 'rgba(8,12,28,.72)'; g.strokeStyle = 'rgba(255,255,255,.55)'; g.lineWidth = 3;
g.beginPath();
if (g.roundRect) g.roundRect(px, py, w0, 68, 34); else g.rect(px, py, w0, 68);
g.fill(); g.stroke();
g.fillStyle = '#fff'; g.font = '800 ' + Math.round(38 * sc) + 'px system-ui, sans-serif';
g.fillText(info.stats[k], px + w0 / 2, py + 47);
px += w0 + gap * sc;
}
}
outline = 8;
g.fillStyle = '#fff'; g.font = '900 52px system-ui, sans-serif'; put(g, 'Can you beat it?', W / 2, H - 84);
g.fillStyle = 'rgba(255,255,255,.92)'; g.font = '700 34px system-ui, sans-serif'; outline = 6; put(g, 'Play free at webdevpuneet.com', W / 2, H - 34);
outline = 0;
return c;
}
function finish(c, cb) {
var dataUrl = '';
try { dataUrl = c.toDataURL('image/png'); } catch (e) {}
try { c.toBlob(function (b) { cb(b, dataUrl); }, 'image/png'); } catch (e) { cb(null, dataUrl); }
}
function makeCard(info, cb) {
var cv0 = bestCanvas();
if (cv0) { var bc = drawBig(info, cv0); if (bc) { finish(bc, cb); return; } }
var S = 1080, c = document.createElement('canvas');
c.width = S; c.height = S;
var g = c.getContext('2d');
var gr = g.createLinearGradient(0, 0, 0, S);
gr.addColorStop(0, '#eaf4ff'); gr.addColorStop(1, '#fff3ef');
g.fillStyle = gr; g.fillRect(0, 0, S, S);
var cv = bestCanvas();
var head = info.head || 'Can you beat me?';
var kind = /draw|tie/i.test(head) ? 'draw' : LOSS.test(head) && !WIN.test(head) ? 'loss' : 'win';
g.textAlign = 'center';
g.fillStyle = '#1e293b'; g.font = '800 60px system-ui, sans-serif';
var y = wrap(g, TITLE, S / 2, cv ? 120 : 200, 980, 68, 1);
g.fillStyle = kind === 'draw' ? '#475569' : kind === 'loss' ? '#3b63e0' : '#e8553a';
g.font = '800 ' + (head.length > 22 ? 76 : 100) + 'px system-ui, sans-serif';
y = wrap(g, head, S / 2, y + (head.length > 22 ? 70 : 90), 980, head.length > 22 ? 84 : 108, 2);
if (info.sub) { g.fillStyle = '#475569'; g.font = '700 38px system-ui, sans-serif'; y = wrap(g, info.sub, S / 2, y + 4, 940, 48, 2); }
if (info.stats.length) {
g.font = '800 36px system-ui, sans-serif';
var gap = 18, pad = 26, ws = [], tot = -gap;
for (var i = 0; i < info.stats.length; i++) { ws.push(g.measureText(info.stats[i]).width + pad * 2); tot += ws[i] + gap; }
var sc = tot > 1000 ? 1000 / tot : 1, px = (S - tot * sc) / 2, py = y + 14;
for (var k = 0; k < info.stats.length; k++) {
var w0 = ws[k] * sc;
g.fillStyle = '#ffffff'; g.strokeStyle = '#d5e2ee'; g.lineWidth = 3;
g.beginPath();
if (g.roundRect) g.roundRect(px, py, w0, 64, 32); else g.rect(px, py, w0, 64);
g.fill(); g.stroke();
g.fillStyle = '#1e293b'; g.font = '800 ' + Math.round(36 * sc) + 'px system-ui, sans-serif';
g.fillText(info.stats[k], px + w0 / 2, py + 44);
px += w0 + gap * sc;
}
y = py + 64 + 10;
}
if (cv) {
try {
var maxW = 960, maxH = Math.max(180, 930 - y - 30), r = cv.width / cv.height;
var w = maxW, h = w / r;
if (h > maxH) { h = maxH; w = h * r; }
var x = (S - w) / 2, yy = y + 20 + Math.max(0, (maxH - h) / 2);
g.save(); g.shadowColor = 'rgba(60,100,140,0.3)'; g.shadowBlur = 30; g.shadowOffsetY = 10;
g.fillStyle = '#fff'; g.fillRect(x - 8, yy - 8, w + 16, h + 16); g.restore();
g.drawImage(cv, x, yy, w, h);
} catch (e) {}
}
g.fillStyle = '#1e293b'; g.font = '800 46px system-ui, sans-serif';
g.fillText('Can you beat it?', S / 2, 1000);
g.fillStyle = '#64748b'; g.font = '700 34px system-ui, sans-serif';
g.fillText('Play free at webdevpuneet.com', S / 2, 1050);
var dataUrl = '';
try { dataUrl = c.toDataURL('image/png'); } catch (e) {}
try { c.toBlob(function (b) { cb(b, dataUrl); }, 'image/png'); } catch (e) { cb(null, dataUrl); }
}
function shareText(info) {
var bits = [];
if (info.head) bits.push(info.head);
if (info.stats.length) bits.push(info.stats.join(', '));
return TITLE + ': ' + (bits.length ? bits.join(' - ') + '. ' : '') + 'Can you beat it? ' + URL;
}
function msg(t) { q('msg').textContent = t; }
function openFallback() {
var t = encodeURIComponent(shareText(lastInfo));
q('wa').href = 'https://wa.me/?text=' + t;
q('x').href = 'https://twitter.com/intent/tweet?text=' + t;
q('fb').href = 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(URL) + '"e=' + t;
box.hidden = false;
}
function nativeShare() {
/* a user cancel does nothing; anything else (blocked in an iframe, no permission) shows Copy / Download / links */
try {
navigator.share({ files: [file], title: TITLE, text: shareText(lastInfo) }).catch(function (e) { if (!e || e.name !== 'AbortError') openFallback(); });
} catch (e) { openFallback(); }
}
function onShare() {
var info = readResult(); info.stats = readStats();
lastInfo = info;
msg('Share your result:');
makeCard(info, function (b, dataUrl) {
blob = b; file = null;
if (b) { try { file = new File([b], 'result.png', { type: 'image/png' }); } catch (e) {} }
if (dataUrl) q('dl').href = dataUrl;
q('dl').hidden = !dataUrl;
var canNative = !!(file && navigator.share && navigator.canShare && navigator.canShare({ files: [file] }));
var touch = !!(window.matchMedia && window.matchMedia('(pointer: coarse)').matches);
q('more').hidden = !canNative;
if (canNative && touch) nativeShare();
else openFallback();
});
}
/* Games often have document/window level touch and pointer handlers (some call preventDefault, which cancels the tap's
click on phones, others restart the game on touchstart). So: keep every pointer event on our controls away from the
game, and fire on touchend as well as click (guarded so one tap shares once). */
var busy = 0;
function trigger() { if (busy) return; busy = 1; setTimeout(function () { busy = 0; }, 700); onShare(); }
function guard(el, act) {
var evs = ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'touchstart', 'touchend', 'click'];
for (var i = 0; i < evs.length; i++) el.addEventListener(evs[i], function (e) { e.stopPropagation(); });
if (act) {
el.addEventListener('touchend', function (e) { if (e.cancelable) e.preventDefault(); act(); });
el.addEventListener('click', function () { act(); });
}
}
guard(pill, trigger);
guard(inl, trigger);
guard(box);
q('more').addEventListener('click', function () { if (file) nativeShare(); });
q('close').addEventListener('click', function () { box.hidden = true; });
document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && !box.hidden) box.hidden = true; });
q('copy').addEventListener('click', function () {
try {
navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]).then(
function () { msg('Image copied. Paste it into your chat or post.'); },
function () { msg('Could not copy here. Try Download image.'); }
);
} catch (e) { msg('Copying images is not supported here. Try Download image.'); }
});
})();
/* wdp-share:end */Extend with AI
Take This Snippet Further With AI
Paste this snippet's HTML, CSS and JS into an AI assistant like Claude and ask it to explain the stage system, the near-miss tracking, the dash invulnerability and the hazard collision rectangles. It is a good base to extend: ask for boss stages with patterns, a daily seeded run, unlockable car skins, a ghost replay of your best run, gamepad support, or a port to a React component with the game state in a ref. Treat it as a working prototype to question and rebuild.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build a complete synthwave dodging game on an HTML5 canvas in plain HTML, CSS and JavaScript, with no libraries and no image files.
Requirements:
- A hover-car at the bottom that moves left and right with acceleration and drag (arrow keys, A and D, on-screen buttons) or follows the finger when dragging on the canvas, and tilts with its speed.
- A dash (Space, Shift or a button) that launches the car sideways for 0.18 s with invulnerability, then a 1.6 s cooldown shown by a meter.
- Stages every 20 seconds, each with a banner and a short breather: falling blocks, then zig-zag blocks, then full-width walls with one gap, then lasers that show a blinking warning line before firing a full-height beam, then everything. Fall speed and spawn rate rise continuously and wall gaps narrow.
- Near-miss detection (smallest horizontal gap while a hazard passes the car's row) with a combo up to x5; stars worth 50 times the combo; shield, slow-motion (world only) and shrink power-ups.
- A synthwave look: a backdrop with a striped sun and mountains painted once to an off-screen canvas, a scrolling perspective grid, speed streaks, neon hazards with glow, additive particles, a crash explosion and screen shake.
- Score = 10 points per second + bonuses, best score in localStorage (with try/catch), Web Audio sound with mute, and pause on P, a button and tab switch.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.