Workshop › Wolfenstein-style 3D Maze › Step 15 of 18
The exit E is a green wall. Walking up to it wins: everything stops and the time is shown. Space plays again.
```js
let state // 'playing' or 'won'
state = 'playing'
// The player escapes by walking up to the exit.
function nearExit() {
const row = MAP.findIndex((line) => line.includes('E'))
const col = MAP[row].indexOf('E')
return Math.hypot(player.x - (col + 0.5), player.y - (row + 0.5)) < 0.8
}
if (event.key === ' ' && state === 'won') reset()
if (state !== 'playing') return
if (nearExit()) {
state = 'won'
}
if (state === 'won') {
ctx.fillStyle = 'rgba(15, 23, 42, 0.75)'
ctx.fillRect(0, 0, canvas.width, H)
ctx.fillStyle = '#22c55e'
ctx.textAlign = 'center'
ctx.font = 'bold 28px sans-serif'
ctx.fillText('Escaped in ' + seconds(frames) + ' s', canvas.width / 2, H / 2)
ctx.fillStyle = 'white'
ctx.font = '16px sans-serif'
ctx.fillText('Press Space to play again', canvas.width / 2, H / 2 + 30)
}
```findIndex finds the row that contains E, indexOf its column.Math.hypot(a, b) is the straight-line distance √(a² + b²) from the player to the middle of the exit tile.update does nothing, so the time freezes.player, write state; in reset, set it to 'playing'.nearExit; in keydown, play again on Space.update, stop when not playing; at the end, win near the exit.draw, draw the winning screen.Skills: Game states