Workshop › Pinball › Step 16 of 42
Three bumpers
What we are doing
Bumpers are the round red posts that throw the ball around. Each is a circle { x, y, r }; we list three and draw
them.
```js
const BUMPERS = [
{ x: 100, y: 160, r: 22 },
{ x: 200, y: 120, r: 22 },
{ x: 280, y: 280, r: 22 },
]
BUMPERS.forEach((b) => {
ctx.fillStyle = '#e11d48'
ctx.beginPath()
ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2)
ctx.fill()
})
```
What it means
BUMPERS is a list of objects, one per bumper: centre and radius.
BUMPERS.forEach((b) => { ... }) runs the function once for each bumper, with the bumper as b.
- Each is drawn as a filled red circle, before the ball so the ball shows on top.
Your turn
- Under the
WALLS list write BUMPERS.
- In
draw, above ctx.fillStyle = '#e7e5e4' (the ball), write the forEach block.
Skills: Arrays and objects, Drawing on a canvas
← The next ball The ball meets a bumper →
All steps
- Paint the table
- Draw one wall
- A list of walls
- The game loop
- A ball in the lane
- Draw the ball
- Move by the velocity
- Launch with Space
- Gravity
- Does the ball touch a wall?
- Push the ball out
- Bounce off the wall
- Small steps and a speed limit
- Ready or playing
- The next ball
- Three bumpers
- The ball meets a bumper
- A bumper kicks
- A score and a fresh start
- Points for every hit
- Remember which bumper was hit
- Light it up
- Lit for a moment, points once
- Two flippers
- Where is the tip?
- Flippers stop the ball
- Which keys are held?
- Which way to turn
- Flip!
- A moving wall
- The flipper throws
- Three balls
- Balls left and game over
- Play again
- Remember the best score
- Launch on letting go
- Pull the spring
- Show the spring
- Two thumbs
- Pull with a finger
- Tap to play again
- Build it yourself: slingshots