Workshop › Angry Birds-style Slingshot › Step 9 of 47
Everything that will move in this game (the bird, the blocks, the pigs) is a body: a box with a kind, a position,
a size and a velocity. They all live in one array, bodies, and draw paints each one.
```js
let bodies // { kind, x, y, w, h, vx, vy }
let bird // the bird in flight, or null
const body = (kind, x, y, w, h) => ({ kind, x, y, w, h, vx: 0, vy: 0 })
bodies = []
bird = null
for (const b of bodies) {
const m = MATERIALS[b.kind]
ctx.fillStyle = m.color
ctx.beginPath()
ctx.arc(b.x + b.w / 2, b.y + b.h / 2, b.w / 2, 0, Math.PI * 2)
ctx.fill()
}
```body(...) returns a new object; { kind, x } is short for { kind: kind, x: x }. vx and vy (the velocity)
start at 0.bird will point to the bird while it flies, null meaning "none".for (const b of bodies) repeats for each body. MATERIALS[b.kind] looks up the material by a name held in a
variable; the circle's center is the middle of the box, x + w / 2.let aim write let bodies and let bird; under let aim leave an empty line and write the body helper.reset, write bodies = [] and bird = null.draw, after an empty line, write the for loop.Skills: Arrays and objects, Game states
← Aim with the arrow keys Launch! →