Workshop › Space Invaders-style › Step 18 of 21
Now Space after GAME OVER starts a new game. newGame puts every starting value back — ship, wave, score, lives,
reload, state — and spawns the first wave. The game itself starts by calling it too.
```js
let ship
let lastShot
let wave
let score
let lives
let state // 'playing' or 'over'
function newGame() {
ship = { x: canvas.width / 2 - SHIP_W / 2, y: SHIP_Y, w: SHIP_W, h: SHIP_H }
wave = 1
score = 0
lives = 3
lastShot = -COOLDOWN
state = 'playing'
spawnWave()
}
if (event.key === ' ') {
if (state === 'over') newGame()
else shoot()
}
newGame()
```ship, lastShot, score, lives, state plain declarations and add wave.spawnWave, write newGame.newGame() instead of spawnWave().Skills: Game states, Functions
← A wave in a function Waves →