Workshop › Pac-Man-style Maze Chase › Step 13 of 42
Turning into a side corridor waits for the next tile center, but turning back should not: when a ghost appears
ahead you must flee now. The trick: 3 frames into a step from tile 9 to 8 is the same place as 5 frames into a step from
tile 8 back to 9. So move to the tile you were heading to, flip dir, and set progress = frames - progress.
```js
const reverse = (dir) => [-dir[0], -dir[1]]
// Turning around in the middle of a tile: step into the next tile and walk back the rest of the way.
function turnAround(e) {
if (e.progress === 0 || same(e.dir, STOP)) {
e.dir = reverse(e.dir)
return
}
e.col = wrap(e.col + e.dir[0])
e.row += e.dir[1]
e.dir = reverse(e.dir)
e.progress = e.frames - e.progress
}
function steer(dir) {
player.want = dir
if (same(dir, reverse(player.dir)) && !same(dir, STOP)) turnAround(player)
}
```reverse flips both numbers of a direction.dir is enough.position gives
the same point before and after.steer turns around at once only when the new direction is exactly backwards.same, write reverse.steer, write the comment and turnAround.steer, under player.want = dir, write the new line.Skills: Keyboard and mouse input
← Glide between tiles Swipes on a phone →