Workshop › Asteroids › Step 12 of 41
Many bullets can fly at once, so they go in an array. shoot adds one at the ship's nose. Its velocity is the
direction times BULLET_SPEED, plus the ship's own velocity, like a ball thrown from a moving car.
```js
const BULLET_SPEED = 7
const BULLET_LIFE = 55 // frames
let bullets
function shoot() {
const dx = Math.cos(ship.angle)
const dy = Math.sin(ship.angle)
bullets.push({
x: ship.x + dx * SHIP_R,
y: ship.y + dy * SHIP_R,
vx: ship.vx + dx * BULLET_SPEED,
vy: ship.vy + dy * BULLET_SPEED,
life: BULLET_LIFE,
})
}
bullets = []
```dx, dy are the direction the ship faces, worked out once.bullets.push({ ... }) adds a bullet object to the end of the list: at the nose, with the ship's velocity plus the
bullet speed, and a life of 55 frames.bullets = [] at the bottom starts with an empty list.MAX_SPEED write the two bullet constants; under let ship write let bullets.keydown listener write shoot, followed by an empty line.resetShip(), write bullets = [].Skills: Arrays and objects, Velocity and gravity