Every project / Games

🧱 Brick Breaker

Bounce the ball, smash every brick. Aim with where it hits the paddle.

Brick Breaker running
Language
HTML, CSS & JavaScript
Level
Intermediate
Category
Games
▶ Try it & remix

What you'll learn

  • requestAnimationFrame
  • Collision detection
  • Velocity & angles
  • Mouse + keyboard input
  • Objects & arrays

How it works

  • requestAnimationFrame runs update() then draw() about 60 times a second.
  • The ball has a position (x, y) and a velocity (dx, dy). Hitting a wall flips dx or dy.
  • Where the ball lands on the paddle sets the bounce angle (up to 60°), and every paddle hit speeds it up a little.
  • Bricks are objects in an array with an alive flag. A hit sets alive to false and bounces the ball.

Knobs to turn

These settings are near the top of the code. Remix it, change one, and watch what happens. The preview updates as you type.

SettingStarts atFileWhat it does
ROWS5game.jsrows of bricks
COLS8game.jsbricks in each row
BRICK_W50game.jsbrick width in pixels
BRICK_H16game.jsbrick height in pixels
GAP6game.jsspace between bricks
TOP50game.jsspace above the first row
START_SPEED4game.jshow fast the ball starts

Files

FileLines
index.html14the page: what shows up
game.js175what it does
style.css25how it looks

Challenges

Each one is a bit harder than the last. Teachers: these make good assignment instructions.

  1. Give each row of bricks a different point value.
  2. Make some bricks take two hits (change color after the first).
  3. Add a power-up that drops from a brick and makes the paddle wider.
  4. Build level 2 with a different brick pattern once level 1 is cleared.
  5. Add a second ball that splits off when you hit a special brick.