Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Build a Breakout Game with JavaScript

Breakout is the arcade classic: move a paddle, bounce a ball, and break every brick. It is one of the best beginner game projects because it teaches the canvas API, collision detection, and a requestAnimationFrame game l

Breakout is the arcade classic: move a paddle, bounce a ball, and break every brick. It is one of the best beginner game projects because it teaches the canvas API, collision detection, and a requestAnimationFrame game loop — the same loop that powers most browser games.

The setup is a canvas, a paddle at the bottom, a ball, and a grid of bricks. The ball has an x/y position and a velocity; every frame you move it, check the walls, check the paddle, check the bricks, and redraw everything:

const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
let ball = { x: 240, y: 300, dx: 3, dy: -3, r: 8 };
let paddle = { x: 200, y: 380, w: 80, h: 10 };
let bricks = [];

for (let row = 0; row < 4; row++) {
  for (let col = 0; col < 6; col++) {
    bricks.push({ x: 20 + col * 75, y: 20 + row * 30, w: 65, h: 20, alive: true });
  }
}

The game loop moves the ball, bounces it off the walls, and checks the paddle. The paddle bounce is the interesting part — where the ball hits the paddle changes its angle, which is what makes the game feel controllable rather than random:

function update() {
  ball.x += ball.dx;
  ball.y += ball.dy;

  if (ball.x < ball.r || ball.x > canvas.width - ball.r) ball.dx *= -1;
  if (ball.y < ball.r) ball.dy *= -1;

  if (
    ball.y + ball.r >= paddle.y &&
    ball.x >= paddle.x &&
    ball.x <= paddle.x + paddle.w
  ) {
    const hit = (ball.x - (paddle.x + paddle.w / 2)) / (paddle.w / 2);
    ball.dx = hit * 4;
    ball.dy = -Math.abs(ball.dy);
  }

  if (ball.y > canvas.height) {
    alert("Game over!");
    document.location.reload();
  }
}

Brick collision is a simple rectangle check. When the ball overlaps a live brick, the brick dies, the ball reverses vertically, and the score goes up:

bricks.forEach((brick) => {
  if (!brick.alive) return;
  if (
    ball.x > brick.x && ball.x < brick.x + brick.w &&
    ball.y > brick.y && ball.y < brick.y + brick.h
  ) {
    brick.alive = false;
    ball.dy *= -1;
    score += 10;
  }
});

The paddle follows the mouse or the arrow keys. Mouse control is two lines — listen for mousemove on the canvas and centre the paddle on the cursor, clamped to the canvas edges:

canvas.addEventListener("mousemove", (e) => {
  const rect = canvas.getBoundingClientRect();
  paddle.x = Math.min(
    Math.max(e.clientX - rect.left - paddle.w / 2, 0),
    canvas.width - paddle.w
  );
});

Ways to extend it: add levels with faster balls and new brick layouts, add power-ups that widen the paddle or add a second ball, colour bricks by row so harder rows are worth more points, or add a lives system instead of reloading on the first miss. Once you can write this loop, you can build Pong, Snake, and most other canvas games the same way.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.