webdevpuneet.com

Pinball Game in JavaScript: Free HTML5 Canvas Pinball

Carnival Pinball Game · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

About this UI Snippet

Carnival Pinball: Canvas Pinball With Missions, Multiball, Perks and Four Modes

Screenshot of the Carnival Pinball Game snippet rendered live

Carnival Pinball is a complete pinball table in the browser, drawn on canvas with its own physics and no libraries, images or sound files. Charge the plunger, aim for the lit top lane, work the flippers and use the centre hole to start missions. For other arcade games in the library try Breakout Brick Game or Asteroids Blaster.

The table

Three pop bumpers, two slingshots, a drop target bank on the left, a second bank on the right, S-K-Y rollover lanes, a spinner, a centre hole, inlanes, outlanes and kickbacks. Flipping rotates the lit top lanes, so you can steer the one you still need under the ball.

Scoring, missions and multiball

Clearing the left bank raises the playfield multiplier up to 5, and lighting S-K-Y raises the end-of-ball bonus multiplier up to 6. The centre hole starts one of four timed missions. Clearing the right bank twice lights the lock for three-ball multiball with jackpots, and every third mission starts Carnival, a 30 second round where everything scores five times more. Shots in quick succession build combos, and an extra ball arrives at 150,000.

Perks and modes

Each finished mission offers three perks and you keep one: a post between the flippers, longer flippers, kickbacks, gentler gravity and more. Classic gives three balls, Blitz gives two minutes with endless balls, Zen never ends, and Daily seeds the perk offers, mission order and skill shots from the date.

How the physics works

The simulation runs in fixed steps of 1/600 of a second, so a fast ball cannot pass through a wall. Walls and flippers are line segments and bumpers are circles. A moving flipper passes the speed of its surface to the ball, which is why a tip shot goes further than one near the hinge.

Best scores are kept in localStorage where the browser allows it. Sandboxed previews block storage, so scores there last only until the page reloads.

What's included

Features

Fixed-step canvas physics with moving flippers and multiple balls
Classic, Blitz, Zen and Daily modes
Four timed missions plus a five-times Carnival round
Three-ball multiball with jackpots
Nine perks, offered three at a time
Skill shot, combos, ball save, kickbacks, extra ball and tilt
Synthesised effects and background tune with separate toggles
Keyboard and multi-touch controls, auto-pause and a bright layout for phones and desktops

Step by step

How to Use

  1. 1
    LaunchHold Space, or hold a finger on the table, and release. More charge means a faster ball. Hit the flashing top lane for the skill shot.
  2. 2
    FlipZ and / or the left and right arrows. Shift keys also work. On touch screens, tap the left or right half.
  3. 3
    Nudge with carePress the up arrow, X or the Nudge button to shove the table. Too many nudges in a row tilts it and ends the ball without bonus.
  4. 4
    Start missionsShoot the centre hole, then complete the shown goal before the timer ends to score 25,000 and pick a perk.
  5. 5
    Chase multiballLight all three right targets, twice, then shoot the centre hole.
  6. 6
    Pause or muteP or Escape pauses, M mutes everything, and the Music button turns off only the tune.

Real-world uses

Common Use Cases

A break game for your site
Add it to a 404, waiting or intermission page. It needs no server and no asset files.
Learning 2D collision physics
Read how circle against segment collision, restitution and a rotating flipper fit in a few functions.
Event or campaign mini game
Re-skin the colours and printed art for a launch or fair, and challenge visitors with the Daily mode.
Starting point for your own table
Move the bumpers, targets and walls by editing their coordinates to design a new layout.
Studying game feel
Compare lamps, particles, screen shake, floating scores and synthesised sound against the same table without them.

Got questions?

Frequently Asked Questions

Z or left arrow for the left flipper, / or right arrow for the right, Space to launch, up arrow or X to nudge, P to pause and M to mute. On touch screens tap each half of the table.

Hit all three right-side targets to complete the bank, do it a second time to light the lock, then shoot the centre hole.

The table tilted from too much nudging. Flippers return on the next ball.

The mission order, perk offers and skill lane come from the date. Ball movement still depends on your timing and has small random variation.

No. The collision and movement code is part of the snippet.

Copy & paste

Source Code

<div class="cp">
  <aside class="cp-panel">
    <h1>Carnival <span>Pinball</span></h1>
    <div class="cp-score">
      <small>Score</small>
      <strong id="score">0</strong>
    </div>
    <div class="cp-stats">
      <div><small id="ballsLabel">Ball</small><b id="balls">1</b></div>
      <div><small>Multiplier</small><b id="multi">x1</b></div>
      <div><small>Best</small><b id="best">0</b></div>
    </div>
    <div class="cp-card cp-mission">
      <small>Mission</small>
      <b id="missionName">Shoot the centre hole</b>
      <p id="missionText">Start a mission to earn a perk</p>
      <div class="cp-bar"><i id="missionBar"></i></div>
    </div>
    <div class="cp-card cp-perks"><small>Perks</small><p id="perkNames">None yet</p></div>
    <p class="cp-keys"><kbd>Z</kbd> <kbd>/</kbd> or <kbd>←</kbd> <kbd>→</kbd> flip · <kbd>Space</kbd> launch · <kbd>↑</kbd> nudge · <kbd>P</kbd> pause</p>
    <div class="cp-buttons">
      <button type="button" id="nudgeBtn">Nudge</button>
      <button type="button" id="pauseBtn">Pause</button>
      <button type="button" id="soundBtn">Sound on</button>
      <button type="button" id="musicBtn">Music on</button>
      <button type="button" id="menuBtn">Modes</button>
    </div>
    <p id="message" class="cp-sr" aria-live="polite"></p>
  </aside>

  <main class="cp-play" id="playArea">
    <canvas id="table" tabindex="0" aria-label="Pinball table"></canvas>

    <div class="cp-overlay" id="menu">
      <div class="cp-box">
        <h2>Carnival Pinball</h2>
        <p>Missions, multiball and a perk to pick after every mission.</p>
        <button type="button" class="cp-main" id="playBtn">Play</button>
        <div class="cp-modes">
          <button type="button" id="blitzBtn"><b>Blitz</b><span>2 minutes, endless balls</span></button>
          <button type="button" id="zenBtn"><b>Zen</b><span>Never game over</span></button>
          <button type="button" id="dailyBtn"><b>Daily</b><span>Same perks for everyone today</span></button>
        </div>
        <p class="cp-help">Keys: <b>Z</b> and <b>/</b> flip, <b>Space</b> launches. Touch: tap left or right side.</p>
        <p class="cp-help" id="bests"></p>
      </div>
    </div>

    <div class="cp-overlay" id="perkPick" hidden>
      <div class="cp-box">
        <h2>Pick a perk</h2>
        <div class="cp-perklist" id="perkList"></div>
      </div>
    </div>

    <div class="cp-overlay" id="pauseBox" hidden>
      <div class="cp-box">
        <h2>Paused</h2>
        <button type="button" class="cp-main" id="resumeBtn">Resume</button>
        <button type="button" class="cp-link" id="quitBtn">Quit to modes</button>
      </div>
    </div>

    <div class="cp-overlay" id="gameOver" hidden>
      <div class="cp-box">
        <h2 class="cp-title" id="overTitle">Game over</h2>
        <p class="cp-final" id="overScore">0</p>
        <p id="overNote"></p>
        <button type="button" class="cp-main" id="againBtn">Play again</button>
        <button type="button" class="cp-link" id="modesBtn">Change mode</button>
      </div>
    </div>
  </main>
</div>

Extend with AI

Take This Snippet Further With AI

Start by asking an AI assistant to explain the fixed time step and how the flipper surface velocity is added in the bounce function, because those two decide how the table feels. Good next changes are a second table layout chosen from the menu, a ramp that carries the ball over the playfield, or a saved list of top five scores with initials.

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 pinball game in plain HTML, CSS and JavaScript on canvas, with no libraries, images or audio files.

Requirements:
- A 420x760 logical table scaled to fit, bright carnival palette, side panel with score, ball, multiplier, best, mission, perks and buttons; compact top bar on phones.
- Fixed 1/600 s physics: gravity, segment walls with a curved top, circle bumpers, and two rotating tapered flippers that pass surface velocity to the ball. Support several balls with ball-to-ball collision.
- Charged plunger with a one-way gate, three pop bumpers, two slingshots, left drop target bank (raises playfield multiplier), right target bank (lights multiball lock), three rollover lanes that rotate with the flippers (bonus multiplier), spinner, centre hole, kickbacks and drain.
- Skill shot, ball save, combos, end-of-ball bonus, extra ball, nudge with tilt.
- Centre hole starts four timed missions; each win offers a choice of three perks from nine; every third mission starts a 30 second five-times multiball round.
- Modes: Classic (3 balls), Blitz (120 s), Zen (endless), Daily (date-seeded choices). Best score per mode in localStorage inside try/catch.
- Web Audio effects and a simple looping tune with mute and music toggles, particles, floating scores, screen shake, attract-mode autoplay behind the menu.
- Keyboard and multi-touch input, pause and auto-pause, respect prefers-reduced-motion.

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.