Skip to content
The Chat Archive
qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K

create a pinball game in the browser

  1. User

    create a pinball game in the browser

  2. Assistant

    Creating a pinball game in the browser can be done using HTML, CSS, and JavaScript, along with the Matter.js physics engine to handle collisions and physics.

    Below is a simple pinball game implementation that includes:

    • A ball
    • A flipper
    • A plunger
    • Basic gravity
    • Collision detection

    🕹️ Live Demo

    You can copy this code into an index.html file and open it in your browser.


    ✅ Step-by-step: Pinball Game with Matter.js

    1. HTML + JS Setup

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Pinball Game</title>
      <style>
        body {
          margin: 0;
          overflow: hidden;
          background: #111;
        }
        canvas {
          display: block;
        }
      </style>
    </head>
    <body>
      <script src="https://cdn.jsdelivr.net/npm/matter-js@0.19.0/build/matter.min.js"></script>
      <script>
        // Module aliases
        const Engine = Matter.Engine,
              Render = Matter.Render,
              World = Matter.World,
              Bodies = Matter.Bodies,
              Body = Matter.Body,
              Composite = Matter.Composite,
              Constraint = Matter.Constraint,
              Mouse = Matter.Mouse,
              MouseConstraint = Matter.MouseConstraint,
              Events = Matter.Events;
    
        // Create engine
        const engine = Engine.create();
        const world = engine.world;
    
        // Create renderer
        const render = Render.create({
          element: document.body,
          engine: engine,
          options: {
            width: 800,
            height: 600,
            wireframes: false,
            background: '#111'
          }
        });
    
        Render.run(render);
        Engine.run(engine);
    
        // Ground
        const ground = Bodies.rectangle(400, 580, 810, 60, {
          isStatic: true,
          render: { fillStyle: '#444' }
        });
    
        // Left wall
        const leftWall = Bodies.rectangle(5, 300, 10, 600, {
          isStatic: true,
          render: { fillStyle: '#444' }
        });
    
        // Right wall
        const rightWall = Bodies.rectangle(795, 300, 10, 600, {
          isStatic: true,
          render: { fillStyle: '#444' }
        });
    
        // Top wall
        const topWall = Bodies.rectangle(400, 5, 800, 10, {
          isStatic: true,
          render: { fillStyle: '#444' }
        });
    
        // Ball
        const ball = Bodies.circle(400, 500, 15, {
          restitution: 0.95,
          friction: 0.001,
          density: 0.002,
          render: { fillStyle: 'red' }
        });
    
        // Flipper
        const flipper = Bodies.rectangle(300, 550, 100, 20, {
          isStatic: true,
          angle: -0.2,
          render: { fillStyle: 'blue' }
        });
    
        const flipperConstraint = Constraint.create({
          bodyA: flipper,
          pointA: { x: -50, y: 0 },
          bodyB: null,
          stiffness: 0.9,
          length: 0,
          type: 'constraint'
        });
    
        // Add to world
        World.add(world, [ground, leftWall, rightWall, topWall, ball, flipper, flipperConstraint]);
    
        // Plunger
        const plungerGuide = Bodies.rectangle(600, 550, 10, 60, {
          isStatic: true,
          render: { fillStyle: '#666' }
        });
    
        const plunger = Bodies.rectangle(600, 570, 10, 20, {
          isStatic: false,
          collisionFilter: { group: -1 },
          render: { fillStyle: 'yellow' }
        });
    
        const plungerConstraint = Constraint.create({
          bodyA: plunger,
          pointA: { x: 0, y: -20 },
          bodyB: null,
          stiffness: 1,
          length: 0
        });
    
        World.add(world, [plungerGuide, plunger, plungerConstraint]);
    
        // Handle key input
        document.addEventListener('keydown', function(event) {
          if (event.code === 'Space') {
            // Launch plunger
            Body.setVelocity(plunger, { x: 0, y: -15 });
          }
          if (event.code === 'ArrowLeft') {
            Body.setAngle(flipper, flipper.angle - 0.2);
          }
          if (event.code === 'ArrowRight') {
            Body.setAngle(flipper, flipper.angle + 0.2);
          }
        });
    
        // Reset ball on bottom
        Events.on(engine, 'beforeUpdate', function() {
          if (ball.position.y > 600) {
            Body.setPosition(ball, { x: 400, y: 500 });
            Body.setVelocity(ball, { x: 0, y: 0 });
          }
        });
      </script>
    </body>
    </html>
    

    🧩 Features

    • Ball physics with gravity and bounce
    • Plunger to launch the ball using the Space key
    • Flipper controlled with Left and Right arrow keys
    • Walls to keep the ball in play
    • Respawn the ball if it falls off the bottom

    🛠️ How to Run

    1. Save the code above as index.html
    2. Open it in a browser (Chrome/Firefox/etc)
    3. Use:
      • Left Arrow to rotate the flipper
      • Right Arrow to rotate the other way
      • Space to launch the plunger

    🚀 Next Steps (Optional Enhancements)

    • Add bumpers and scoring
    • Add ramps or targets
    • Improve flipper physics with motors
    • Add sound effects
    • Add a start screen and score display

    Would you like me to expand this into a full-featured pinball game with scoring and bumpers?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.