Workshop › 2048 › Step 22 of 26
Space (or Enter) goes on: after a game over it starts a new game, after a win it keeps playing.
```js
function next() {
if (state === 'over') newGame()
else if (state === 'won') {
state = 'playing'
keepPlaying = true
}
}
document.addEventListener('keydown', (event) => {
if (event.key === ' ' || event.key === 'Enter') next()
```next does the right thing for the state; while playing it does nothing.' ') or Enter, and then redraws as for every key.keydown listener write next, with an empty line after it.Skills: Keyboard and mouse input, Game states
← Win and lose screens Best score →