DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Pong Game with JavaScript

Pong is the first video game ever sold, and it is still the best first game to build: two paddles, one ball, and a score. It teaches the canvas API, collision detection, and a game loop — and it adds one thing Breakout does not: a simple AI opponent.

The ball moves with a velocity, and the loop is the same shape as every canvas game: move, collide, draw, repeat. The player paddle follows the mouse; the computer paddle follows the ball, but deliberately a little slower than the ball, so it can be beaten:

const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
let ball = { x: 300, y: 200, dx: 4, dy: 3, r: 8 };
let player = { y: 160, h: 80 };
let computer = { y: 160, h: 80 };
let score = { player: 0, computer: 0 };

canvas.addEventListener("mousemove", (e) => {
  const rect = canvas.getBoundingClientRect();
  player.y = Math.min(
    Math.max(e.clientY - rect.top - player.h / 2, 0),
    canvas.height - player.h
  );
});
Enter fullscreen mode Exit fullscreen mode

The AI is the interesting part. A perfect AI would track the ball exactly, so you add a speed cap and a small dead zone — the computer only moves when the ball is more than a few pixels away from its centre, and it moves at a fixed speed:

function updateComputer() {
  const center = computer.y + computer.h / 2;
  const speed = 3.5;
  if (ball.dx > 0) {
    if (center < ball.y - 6) computer.y += speed;
    if (center > ball.y + 6) computer.y -= speed;
  } else {
    if (center < canvas.height / 2 - 6) computer.y += speed / 2;
    if (center > canvas.height / 2 + 6) computer.y -= speed / 2;
  }
  computer.y = Math.max(0, Math.min(canvas.height - computer.h, computer.y));
}
Enter fullscreen mode Exit fullscreen mode

Ball-paddle collision changes the ball's angle based on where it hits the paddle, which is what makes the game skill-based rather than a coin flip. The ball speeds up slightly with every paddle hit, so rallies get tense:

function paddleBounce(paddleY) {
  const hit = (ball.y - (paddleY + 40)) / 40;
  const speed = Math.min(Math.hypot(ball.dx, ball.dy) * 1.05, 12);
  const angle = hit * (Math.PI / 4);
  ball.dx = Math.sign(ball.dx) * -1 * speed * Math.cos(angle);
  ball.dy = speed * Math.sin(angle);
}
Enter fullscreen mode Exit fullscreen mode

Scoring is a boundary check. If the ball passes the left edge, the computer scores; past the right edge, the player scores. The ball resets to the centre with a random direction, and the first to 7 wins:

function checkScore() {
  if (ball.x < 0) {
    score.computer++;
    resetBall(1);
  }
  if (ball.x > canvas.width) {
    score.player++;
    resetBall(-1);
  }
}

function resetBall(direction) {
  ball = { x: 300, y: 200, dx: 4 * direction, dy: 3 * (Math.random() > 0.5 ? 1 : -1), r: 8 };
}
Enter fullscreen mode Exit fullscreen mode

Ways to extend it: add a two-player mode where W/S keys control the left paddle, add a serve countdown, add a speed display, or add sound effects with the Web Audio API. The AI pattern here — move toward a target at a capped speed with a dead zone — is the same one used in enemy movement in platformers and tower defence games.

Top comments (0)