Brick Breaker
We will develop a game called Brick Breaker based on a tutorial on MDN website. The original tutorial does not use classes, but we will design and build the game the Object-Oriented way. This chapter is where we cover Object-Oriented Programming in JavaScript. It also introduces the HTML Canvas element, which is used to build web-based animations and games.
Getting Started
Go to the brick-breaker-game repository and click “Use this template” → “Create a new repository” to create your own copy.
Clone your repository to your computer:
git clone <your-repo-url> brick-breaker-game
cd brick-breaker-game
code .
The template is pre-configured with Vite (just like the ToDo App project from the previous chapter). Install dependencies and start the dev server:
pnpm install
pnpm dev
You should see a blank page when you open the dev server in your browser.
If you get stuck at any point, you can view the complete solution on the master branch of the original repository.
Learning Outcomes
- Use the HTML Canvas element and animation loops to render and animate 2D graphics
- Model game objects as classes, and use inheritance to share and specialize behavior across related objects
- Implement core game mechanics such as movement, boundary constraints, and collision detection
- Build a complete, playable game by integrating rendering, input handling, scoring, and win/lose conditions
Sections
- Drawing on the Canvas
- Animating the Ball
- Modeling Game Objects with Classes
- Using the Block Class
- Extending Block with a Sprite Class
- Using the Sprite Class
- Creating a Bouncing Ball
- Refining the Ball Bounce Logic
- Building the Paddle
- Clamping the Paddle to the Canvas
- Collision Detection Between Ball and Paddle
- Detecting Game Over
- Creating the Brick Class
- Creating a Brick Grid
- Tracking and Displaying the Score
- Adding a Win Condition
- Practice Questions