Workshop › Breakout › Step 29 of 39
Game over
What we are doing
When the game is lost, the ball disappears and a big Game Over appears in the middle.
```js
if (state === 'serve' || state === 'playing') {
ctx.fillStyle = '#f8fafc'
ctx.beginPath()
ctx.arc(ball.x, ball.y, BALL_R, 0, Math.PI * 2)
ctx.fill()
}
if (state === 'lost') {
ctx.font = 'bold 36px sans-serif'
ctx.fillText('Game Over', canvas.width / 2, 250)
}
```
What it means
- The ball's four lines move inside an
if: it is drawn only while serving or playing.
- When lost, a bold 36-pixel
Game Over is drawn in the middle (text is still centered).
Your turn
- In
draw, wrap the ball's four lines in the if (indent them by two spaces).
- Under the launch hint line, write the
if (state === 'lost') block.
Skills: Game states
← Lives and a hint on screen Keep score →
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