MDN self-hosted library · e01.org

2D breakout game using pure JavaScript

Games/Tutorials/2DbreakoutgamepureJavaScript/Initializethecanvas

no runnable cells guide upstream source

Games/Tutorials/2D_breakout_game_pure_JavaScript/Initialize_the_canvas

In this step-by-step tutorial, we create a simple mobile MDN Breakout game written in JavaScript, using pure JavaScript without any external libraries.

Every step has editable, live samples available to play with, so you can see what the intermediate stages should look like. You will learn the basics of implementing fundamental game mechanics like canvas manipulation, rendering and moving images, collision detection, control mechanisms, animations and tweens, and winning and losing states.

To get the most out of this series of articles, you should already have basic to intermediate JavaScript knowledge. After working through this tutorial, you should be able to build your own basic Web games.

Gameplay screen from the game MDN Breakout created with pure JavaScript where you can use your paddle to bounce the ball and destroy the brick field, with keeping the points and lives.

[!NOTE] This guide has a sister guide: 2D breakout game using Phaser. They follow essentially the same steps and arrive at visually identical results.

Lesson details

  1. Initialize the canvas
  2. Move the ball
  3. Bounce off the walls
  4. Player paddle and controls
  5. Game over
  6. Build the brick field
  7. Track the score and win
  8. Extra lives
  9. Animations and tweens
  10. Buttons
  11. Randomizing gameplay

Next steps

Ok, let us get started! Head to the first part of the series—Initialize the canvas.

Games/Tutorials/2D_breakout_game_pure_JavaScript/Initialize_the_canvas