2D breakout game using pure JavaScript
In this step-by-step tutorial we create an MDN Breakout game written entirely in pure JavaScript and rendered on HTML <canvas>
.
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 using the <canvas>
element to implement fundamental game mechanics like rendering and moving images, collision detection, control mechanisms, 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 Web games.
Lesson details
All the lessons — and the different versions of the MDN Breakout game we are building together — are available on GitHub:
- Create the Canvas and draw on it
- Move the ball
- Bounce off the walls
- Paddle and keyboard controls
- Game over
- Build the brick field
- Collision detection
- Track the score and win
- Mouse controls
- Finishing up
Starting with pure JavaScript is the best way to get a solid knowledge of web game development. After that, you can pick any framework you like and use it for your projects. Frameworks are just tools built with the JavaScript language; so even if you plan on working with them, it's good to learn about the language itself first to know what exactly is going on under the hood. Frameworks speed up development time and help take care of boring parts of the game, but if something is not working as expected, you can always try to debug that or just write your own solutions in pure JavaScript.
Note: This series of articles can be used as material for hands-on game development workshops. You can also make use of the Gamedev Canvas Content Kit based on this tutorial if you want to give a talk about game development in general.
If you are interested in using a game library for learning about 2D web game development, see this series' counterpart, 2D breakout game using Phaser.
Next steps
Ok, let's get started! Head to the first chapter Create the Canvas and draw on it.