webdevpuneet.com

Dodge the Blocks Game: Neon Stages, Lasers and Dash

Dodge the Falling Blocks Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

About this UI Snippet

Dodge the Blocks: Stage-Based Difficulty, Near-Miss Combos and a Synthwave Look

Screenshot of the Dodge the Falling Blocks Game snippet rendered live

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

Five stage types introduced every 20 seconds: blocks, zig-zag blocks, walls with a gap, telegraphed lasers, then everything
Continuously rising fall speed and spawn rate, narrowing wall gaps, and a breather with a banner at each stage
Acceleration-based steering with tilt, drag-to-move for touch, and an invulnerable dash with a cooldown meter
Near-miss detection with a combo multiplier up to x5
Stars plus shield, slow-motion and shrink power-ups
A synthwave backdrop painted once, a scrolling perspective grid, speed streaks and neon hazards
Additive particles for engine sparks, dash trails and the crash, plus screen shake
On-screen ◀ DASH ▶ buttons, keyboard and drag controls, pause and mute
Web Audio sound effects and a best score in localStorage with try/catch

Step by step

How to Use

  1. 1
    Start 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.
  2. 2
    Survive 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.
  3. 3
    Dash 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.
  4. 4
    Watch 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.
  5. 5
    Skim 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.
  6. 6
    Grab 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

Learning difficulty design
Introducing one hazard per stage with a breather in between is a clear example of teaching players new mechanics gradually.
A one-thumb mobile game template
Drag-to-move, a big DASH button and simple rules make it comfortable on a phone and easy to adapt.
A synthwave canvas style
The striped sun, perspective grid and neon outlines show how to get a strong retro look from a few gradients and lines.
An interactive 404 page or loading screen
A quick survival game works as a distraction while something loads, like the Space Blaster Arcade Game.
A base for an endless dodger
Hazard types are small objects with their own update and collision rectangles, so new patterns slot into spawn() easily.

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>

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:

text
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.