Here’s a minimal “Pong” clone in two flavors—pick whichever you like:
- JavaScript + HTML5 Canvas (no install—just open in your browser)
- Python + Pygame (if you have Python and Pygame installed)
1) JavaScript + HTML5 Canvas
Save the snippet below as pong.html, then open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Simple Pong</title>
<style>
body { margin:0; background:#000; overflow:hidden; }
canvas { display:block; margin:auto; background:#111; }
</style>
</head>
<body>
<canvas id="pong" width="800" height="400"></canvas>
<script>
const canvas = document.getElementById("pong"),
ctx = canvas.getContext("2d");
// Game objects
const paddleWidth = 10, paddleHeight = 80;
let leftPaddle = { x: 10, y: 160, dy: 0 },
rightPaddle= { x: canvas.width-20, y: 160, dy: 0 },
ball = { x: canvas.width/2, y: canvas.height/2,
r: 8, speed: 5, dx: 5, dy: 3 };
// Controls
document.addEventListener("keydown", e => {
if (e.key === "w") leftPaddle.dy = -6;
if (e.key === "s") leftPaddle.dy = 6;
if (e.key === "ArrowUp") rightPaddle.dy = -6;
if (e.key === "ArrowDown") rightPaddle.dy = 6;
});
document.addEventListener("keyup", e => {
if (e.key==="w"||e.key==="s") leftPaddle.dy = 0;
if (e.key==="ArrowUp"||e.key==="ArrowDown") rightPaddle.dy = 0;
});
// Draw functions
function drawRect(x,y,w,h,col){
ctx.fillStyle = col;
ctx.fillRect(x,y,w,h);
}
function drawCircle(x,y,r,col){
ctx.fillStyle = col;
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2);
ctx.fill();
}
function resetBall() {
ball.x = canvas.width/2; ball.y = canvas.height/2;
// reverse direction
ball.dx = -ball.dx;
ball.speed = 5;
}
// Update game state
function update() {
// Move paddles
[leftPaddle,rightPaddle].forEach(p => {
p.y += p.dy;
// keep on screen
p.y = Math.max(Math.min(p.y, canvas.height - paddleHeight), 0);
});
// Move ball
ball.x += ball.dx;
ball.y += ball.dy;
// Top & bottom collision
if (ball.y - ball.r < 0 || ball.y + ball.r > canvas.height) {
ball.dy = -ball.dy;
}
// Paddle collision
let paddle = (ball.x < canvas.width/2) ? leftPaddle : rightPaddle;
if (
ball.x - ball.r < paddle.x + paddleWidth &&
ball.x + ball.r > paddle.x &&
ball.y + ball.r > paddle.y &&
ball.y - ball.r < paddle.y + paddleHeight
) {
// bounce
ball.dx = -ball.dx;
// speed up a bit
ball.speed += 0.5;
ball.dx = (ball.dx > 0 ? 1 : -1)*ball.speed;
}
// Score?
if (ball.x < 0 || ball.x > canvas.width) {
resetBall();
}
}
// Render everything
function render() {
// clear
drawRect(0,0,canvas.width,canvas.height, "#111");
// draw net
for(let i=0; i<canvas.height; i+=30){
drawRect(canvas.width/2 - 1, i, 2, 20, "#555");
}
// draw paddles & ball
drawRect(leftPaddle.x, leftPaddle.y, paddleWidth, paddleHeight, "#fff");
drawRect(rightPaddle.x, rightPaddle.y, paddleWidth, paddleHeight, "#fff");
drawCircle(ball.x, ball.y, ball.r, "#0f0");
}
// Game loop
function loop() {
update();
render();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
Controls:
- Player 1 (left paddle): W / S
- Player 2 (right paddle): ↑ / ↓
2) Python + Pygame
Install Pygame first (e.g. pip install pygame), then save this as pong.py and run python pong.py.
import pygame, sys
# Pygame setup
pygame.init()
WIDTH, HEIGHT = 800, 400
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong")
# Colors
WHITE = (255,255,255)
BLACK = ( 0, 0, 0)
# Game objects
PADDLE_W, PADDLE_H = 10, 80
ball_radius = 8
# Positions & speeds
left = pygame.Rect(10, HEIGHT//2 - PADDLE_H//2, PADDLE_W, PADDLE_H)
right= pygame.Rect(WIDTH-20, HEIGHT//2-PADDLE_H//2, PADDLE_W, PADDLE_H)
ball = pygame.Rect(WIDTH//2, HEIGHT//2, ball_radius*2, ball_radius*2)
left_speed = right_speed = 0
ball_speed_x, ball_speed_y = 5, 3
clock = pygame.time.Clock()
def reset_ball():
global ball_speed_x, ball_speed_y
ball.center = (WIDTH//2, HEIGHT//2)
ball_speed_x *= -1
ball_speed_y = 3
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Keydown
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w: left_speed = -6
if event.key == pygame.K_s: left_speed = 6
if event.key == pygame.K_UP: right_speed = -6
if event.key == pygame.K_DOWN: right_speed = 6
# Keyup
if event.type == pygame.KEYUP:
if event.key in (pygame.K_w, pygame.K_s): left_speed = 0
if event.key in (pygame.K_UP, pygame.K_DOWN): right_speed = 0
# Move paddles
left.y += left_speed
right.y += right_speed
# Keep on screen
left.y = max(min(left.y, HEIGHT-PADDLE_H), 0)
right.y = max(min(right.y, HEIGHT-PADDLE_H), 0)
# Move ball
ball.x += ball_speed_x
ball.y += ball_speed_y
# Collisions
if ball.top <= 0 or ball.bottom >= HEIGHT:
ball_speed_y *= -1
if ball.colliderect(left) or ball.colliderect(right):
ball_speed_x *= -1
# Score / Reset
if ball.left <= 0 or ball.right >= WIDTH:
reset_ball()
# Draw
win.fill(BLACK)
# center line
for y in range(0, HEIGHT, 30):
pygame.draw.rect(win, WHITE, (WIDTH//2-1, y, 2, 20))
pygame.draw.rect(win, WHITE, left)
pygame.draw.rect(win, WHITE, right)
pygame.draw.ellipse(win, WHITE, ball)
pygame.display.flip()
clock.tick(60)
Feel free to tweak speeds, colors, or add a scoring display! Let me know if you want enhancements—AI-powered CPU paddle, sound effects, or a menu screen. 🎮