Workshop › Breakout › Step 15 of 39
Draw the bricks
What we are doing
We build the row at the start and draw every brick that is still alive, with another kind of loop: for ... of.
```js
for (const brick of bricks) {
if (!brick.alive) continue
ctx.fillStyle = '#ef4444'
ctx.fillRect(brick.x, brick.y, BRICK_W, BRICK_H)
}
buildBricks()
```
What it means
for (const brick of bricks) runs the body once per brick, calling the current one brick.
! means "not"; continue skips to the next brick. So dead bricks are not drawn.
- The bricks are drawn right after the background, so the paddle and ball are drawn on top.
buildBricks() at the bottom builds the row once, before the loop starts.
Your turn
- In
draw, under the background's fillRect line, leave an empty line and write the for loop.
- Write
buildBricks() just above resetBall() at the bottom.
Skills: Loops, Drawing on a canvas
← A row of bricks A whole wall: a loop inside a loop →
All steps
- The field and the paddle
- Draw again and again
- Keep a number inside limits
- Follow the mouse
- A canvas shown at another size
- A ball with a velocity
- Draw a round ball
- Move the ball
- Bounce off the side walls
- Bounce off the ceiling
- The open floor
- Bounce off the paddle
- Aim with the paddle
- A row of bricks
- Draw the bricks
- A whole wall: a loop inside a loop
- A color for every row
- Circle meets box
- Break a brick
- Hitting the side of a brick
- Wait on the paddle
- Ride along with the paddle
- Launch the ball
- Click or press Space
- Remember which keys are held
- Steer with the arrow keys
- Three lives
- Lives and a hint on screen
- Game over
- Keep score
- Show the score
- Win the game
- One function for a new game
- Play again
- A burst of particles
- Break into sparks
- Move, fall and disappear
- Fade out
- Build it yourself: tough bricks