Creating a Simple Pinball Game in the Browser
Here's a basic pinball game you can create using HTML5 Canvas and JavaScript. This implementation includes a flipper, ball, and some bumpers.
HTML (index.html)
<!DOCTYPE html>
<html>
<head>
<title>Simple Pinball Game</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #222;
}
canvas {
border: 2px solid #444;
background-color: #111;
}
#score {
position: absolute;
top: 20px;
color: white;
font-family: Arial, sans-serif;
font-size: 24px;
}
</style>
</head>
<body>
<div id="score">Score: 0</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script src="game.js"></script>
</body>
</html>
JavaScript (game.js)
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const gravity = 0.2;
const friction = 0.99;
const ballRadius = 10;
const flipperWidth = 100;
const flipperHeight = 20;
const bumperRadius = 20;
const bumperBounce = 0.7;
let score = 0;
let gameOver = false;
// Ball object
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
dx: 0,
dy: 0,
radius: ballRadius,
color: '#fff'
};
// Flippers
const leftFlipper = {
x: 100,
y: canvas.height - 50,
width: flipperWidth,
height: flipperHeight,
angle: 0,
color: '#f00',
active: false
};
const rightFlipper = {
x: canvas.width - 100 - flipperWidth,
y: canvas.height - 50,
width: flipperWidth,
height: flipperHeight,
angle: 0,
color: '#0f0',
active: false
};
// Bumpers
const bumpers = [
{ x: 200, y: 200, radius: bumperRadius, color: '#ff0', active: false },
{ x: 600, y: 200, radius: bumperRadius, color: '#0ff', active: false },
{ x: 400, y: 300, radius: bumperRadius, color: '#f0f', active: false },
{ x: 300, y: 400, radius: bumperRadius, color: '#ff0', active: false },
{ x: 500, y: 400, radius: bumperRadius, color: '#0ff', active: false }
];
// Draw functions
function drawBall() {
ctx.beginPath();
ctx.arc(ball.x, ball.y, ball.radius, 0, Math.PI * 2);
ctx.fillStyle = ball.color;
ctx.fill();
ctx.closePath();
}
function drawFlipper(flipper) {
ctx.save();
ctx.translate(flipper.x, flipper.y);
ctx.rotate(flipper.angle);
ctx.fillStyle = flipper.color;
ctx.fillRect(-flipper.width / 2, -flipper.height / 2, flipper.width, flipper.height);
ctx.restore();
}
function drawBumper(bumper) {
ctx.beginPath();
ctx.arc(bumper.x, bumper.y, bumper.radius, 0, Math.PI * 2);
ctx.fillStyle = bumper.color;
ctx.fill();
ctx.closePath();
}
// Collision detection
function checkFlipperCollision(flipper) {
const flipperLeft = flipper.x - flipper.width / 2;
const flipperRight = flipper.x + flipper.width / 2;
const flipperTop = flipper.y - flipper.height / 2;
const flipperBottom = flipper.y + flipper.height / 2;
// Simple AABB collision detection
if (ball.x > flipperLeft && ball.x < flipperRight &&
ball.y > flipperTop && ball.y < flipperBottom) {
// Calculate normal vector
const normalX = (ball.x - flipper.x) / flipper.width;
const normalY = (ball.y - flipper.y) / flipper.height;
// Calculate dot product
const dot = ball.dx * normalX + ball.dy * normalY;
// Reflect velocity
ball.dx = ball.dx - 2 * dot * normalX;
ball.dy = ball.dy - 2 * dot * normalY;
// Add some force from the flipper
if (flipper.active) {
ball.dx += normalX * 2;
ball.dy += normalY * 2;
}
return true;
}
return false;
}
function checkBumperCollision(bumper) {
const dx = ball.x - bumper.x;
const dy = ball.y - bumper.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < ball.radius + bumper.radius) {
// Calculate normal vector
const normalX = dx / distance;
const normalY = dy / distance;
// Calculate dot product
const dot = ball.dx * normalX + ball.dy * normalY;
// Reflect velocity with some energy loss
ball.dx = -dot * normalX * bumperBounce;
ball.dy = -dot * normalY * bumperBounce;
// Add score
score += 100;
scoreElement.textContent = `Score: ${score}`;
bumper.active = true;
return true;
}
return false;
}
function checkWallCollision() {
// Left wall
if (ball.x - ball.radius < 0) {
ball.x = ball.radius;
ball.dx = -ball.dx * 0.8;
}
// Right wall
if (ball.x + ball.radius > canvas.width) {
ball.x = canvas.width - ball.radius;
ball.dx = -ball.dx * 0.8;
}
// Top wall
if (ball.y - ball.radius < 0) {
ball.y = ball.radius;
ball.dy = -ball.dy * 0.8;
}
// Bottom (game over)
if (ball.y + ball.radius > canvas.height) {
gameOver = true;
}
}
// Update game state
function update() {
if (gameOver) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '48px Arial';
ctx.textAlign = 'center';
ctx.fillText('GAME OVER', canvas.width / 2, canvas.height / 2);
return;
}
// Apply gravity
ball.dy += gravity;
// Apply friction
ball.dx *= friction;
ball.dy *= friction;
// Update ball position
ball.x += ball.dx;
ball.y += ball.dy;
// Check collisions
checkWallCollision();
checkFlipperCollision(leftFlipper);
checkFlipperCollision(rightFlipper);
for (const bumper of bumpers) {
if (checkBumperCollision(bumper)) {
// Add some visual feedback
bumper.radius = 25;
setTimeout(() => {
bumper.radius = bumperRadius;
bumper.active = false;
}, 100);
}
}
}
// Draw game
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw bumpers
for (const bumper of bumpers) {
drawBumper(bumper);
}
// Draw flippers
drawFlipper(leftFlipper);
drawFlipper(rightFlipper);
// Draw ball
drawBall();
}
// Game loop
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
// Event listeners
document.addEventListener('keydown', (e) => {
if (e.key === 'a' || e.key === 'ArrowLeft') {
leftFlipper.active = true;
leftFlipper.angle = -Math.PI / 4;
}
if (e.key === 'd' || e.key === 'ArrowRight') {
rightFlipper.active = true;
rightFlipper.angle = Math.PI / 4;
}
});
document.addEventListener('keyup', (e) => {
if (e.key === 'a' || e.key === 'ArrowLeft') {
leftFlipper.active = false;
leftFlipper.angle = 0;
}
if (e.key === 'd' || e.key === 'ArrowRight') {
rightFlipper.active = false;
rightFlipper.angle = 0;
}
if (e.key === ' ' && !gameOver) {
// Launch the ball
ball.dx = 0;
ball.dy = -5;
}
if (e.key === 'r' && gameOver) {
// Reset game
gameOver = false;
score = 0;
scoreElement.textContent = `Score: ${score}`;
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.dx = 0;
ball.dy = 0;
}
});
// Start the game
gameLoop();
How to Play:
- Press SPACE to launch the ball
- Use A or LEFT ARROW to activate the left flipper
- Use D or RIGHT ARROW to activate the right flipper
- Hit the bumpers to score points
- If the ball falls out the bottom, the game is over
- Press R to restart after game over
Features to Add for a More Complete Game:
- More complex physics (better collision detection)
- Sound effects
- Multiple balls
- High score tracking
- More obstacles and targets
- Animations and particle effects
- Better graphics and styling
You can save these files (index.html and game.js) in the same directory and open the HTML file in a browser to play the game.