qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K
create a pinball game in the browser
- User
create a pinball game in the browser
- 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.htmlfile 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
Spacekey - Flipper controlled with
LeftandRightarrow keys - Walls to keep the ball in play
- Respawn the ball if it falls off the bottom
🛠️ How to Run
- Save the code above as
index.html - Open it in a browser (Chrome/Firefox/etc)
- Use:
Left Arrowto rotate the flipperRight Arrowto rotate the other waySpaceto 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?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.