Workshop › Snake › Step 20 of 28
Leaving the board ends the game. Before moving, we check whether the new head would be outside; if so we set
gameOver and stop moving.
```js
let gameOver = false
const hitWall = head.x < 0 || head.x >= COLS || head.y < 0 || head.y >= ROWS
if (hitWall) {
gameOver = true
return
}
if (!gameOver && time - last >= SPEED) {
```gameOver is a boolean: true or false.hitWall is true if the head is left of column 0, past the last column, above row 0 or below the last row (|| means "or").gameOver and return before the snake moves, so it stays inside the board.!gameOver (not game over).let score = 0 write let gameOver = false.update, under the const head line, write the hitWall check.loop, add !gameOver && at the start of the if condition.Skills: Collision detection, Game states
← No U-turns Crash into yourself →