Workshop › Asteroids › Step 32 of 41
The rocks come in waves: wave 1 has 4, and every wave one more (3 + wave). spawnWave builds a wave at the left or
top edge, away from the ship. It replaces the start-up code for the rocks.
```js
let wave
function spawnWave() {
asteroids = []
for (let i = 0; i < 3 + wave; i++) {
// Start at the edges, away from the ship in the middle.
const edge = Math.random() < 0.5
const x = edge ? 0 : Math.random() * canvas.width
const y = edge ? Math.random() * canvas.height : 0
asteroids.push(makeAsteroid(x, y, 3))
}
}
wave = 1
spawnWave()
```edge is a coin toss: true puts the rock on the left edge at a random height, false on the top edge at a random
x.wave = 1 and spawnWave() replace the two old rock lines.let lives write let wave.function shoot() { write spawnWave, followed by an empty line.asteroids = [] and for lines with wave = 1 and spawnWave().Skills: Game states
← Blink while safe The next wave →