Of course! Here is a very simple, single-file implementation of Pong using HTML and JavaScript. It's designed to be easy to understand and requires no external libraries.
What this code does:
- Creates a classic Pong setup with a ball and two paddles.
- The left paddle is controlled by your mouse movement.
- The right paddle is a simple computer AI that follows the ball.
- The ball bounces off the walls and paddles.
- If the ball goes past a paddle, it resets to the center.
- To keep it simple, there is no scoring system.
Just copy this code into an .html file and open it in your web browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Pong</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #111;
}
canvas {
background-color: #000;
border: 2px solid #fff;
}
</style>
</head>
<body>
<canvas id="pongCanvas" width="800" height="400"></canvas>
<script>
// --- 1. SETUP ---
const canvas = document.getElementById('pongCanvas');
const context = canvas.getContext('2d');
// Ball object
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 10,
speedX: 5,
speedY: 5,
color: 'white'
};
// Player paddle (left)
const player = {
x: 10,
y: (canvas.height - 100) / 2,
width: 10,
height: 100,
color: 'white'
};
// Computer paddle (right)
const computer = {
x: canvas.width - 20,
y: (canvas.height - 100) / 2,
width: 10,
height: 100,
color: 'white'
};
// Center line
const net = {
x: (canvas.width - 2) / 2,
y: 0,
width: 2,
height: 10,
color: 'white'
};
// --- 2. DRAWING FUNCTIONS ---
// Draw a rectangle (for paddles)
function drawRect(x, y, w, h, color) {
context.fillStyle = color;
context.fillRect(x, y, w, h);
}
// Draw a circle (for the ball)
function drawCircle(x, y, r, color) {
context.fillStyle = color;
context.beginPath();
context.arc(x, y, r, 0, Math.PI * 2, false);
context.closePath();
context.fill();
}
// Draw the net
function drawNet() {
for (let i = 0; i <= canvas.height; i += 15) {
drawRect(net.x, net.y + i, net.width, net.height, net.color);
}
}
// Draw everything on the canvas
function render() {
// Clear the canvas with a black rectangle
drawRect(0, 0, canvas.width, canvas.height, 'black');
// Draw the net
drawNet();
// Draw the paddles
drawRect(player.x, player.y, player.width, player.height, player.color);
drawRect(computer.x, computer.y, computer.width, computer.height, computer.color);
// Draw the ball
drawCircle(ball.x, ball.y, ball.radius, ball.color);
}
// --- 3. GAME LOGIC ---
// Player movement control
canvas.addEventListener('mousemove', movePaddle);
function movePaddle(event) {
let rect = canvas.getBoundingClientRect();
player.y = event.clientY - rect.top - player.height / 2;
}
// Collision detection
function collision(b, p) { // b for ball, p for paddle
p.top = p.y;
p.bottom = p.y + p.height;
p.left = p.x;
p.right = p.x + p.width;
b.top = b.y - b.radius;
b.bottom = b.y + b.radius;
b.left = b.x - b.radius;
b.right = b.x + b.radius;
return b.right > p.left && b.top < p.bottom && b.left < p.right && b.bottom > p.top;
}
// Reset the ball to the center
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.speedX = -ball.speedX; // Change direction for the other player
}
// Update function: move the ball, check for collisions, etc.
function update() {
// Move the ball
ball.x += ball.speedX;
ball.y += ball.speedY;
// Simple AI for the computer paddle
// Makes the paddle follow the ball's y position
computer.y += (ball.y - (computer.y + computer.height / 2)) * 0.1;
// Wall collision (top and bottom)
if (ball.y + ball.radius > canvas.height || ball.y - ball.radius < 0) {
ball.speedY = -ball.speedY;
}
// Paddle collision
let paddle = (ball.x < canvas.width / 2) ? player : computer;
if (collision(ball, paddle)) {
ball.speedX = -ball.speedX;
}
// If ball goes out of bounds (left or right)
if (ball.x - ball.radius < 0 || ball.x + ball.radius > canvas.width) {
resetBall();
}
}
// --- 4. GAME LOOP ---
function gameLoop() {
update(); // Update game state
render(); // Draw the new state
}
// Run the game loop 60 times per second
const framePerSecond = 60;
setInterval(gameLoop, 1000 / framePerSecond);
</script>
</body>
</html>
How to Play:
- Save the code above as an HTML file (e.g.,
pong.html).
- Open the file in any modern web browser (like Chrome, Firefox, or Edge).
- Move your mouse up and down inside the black game area to control the left paddle.