Workshop › Pac-Man-style Maze Chase › Step 19 of 42
The ghost will walk with the same advance as the player; it only needs its own choose function: chooseGhost.
At each tile center it lists the open directions except backwards and, for now, takes the first. It turns back
only at a dead end. ghostFrames makes it a little slower than the player (9 frames a tile).
```js
// Frames a ghost needs to cross one tile (the player always needs PLAYER_FRAMES): smaller is faster.
function ghostFrames(g) {
return 9
}
function chooseGhost(g) {
g.frames = ghostFrames(g)
// Ghosts never turn back on their own: only the open ways that are not backwards.
const options = Object.values(DIRECTIONS).filter((d) => canGo(g, d) && !same(d, reverse(g.dir)))
if (options.length === 0) {
g.dir = reverse(g.dir)
return
}
g.dir = options[0]
}
```Object.values(DIRECTIONS) is the list of the four directions, in the order up, left, down, right.filter keeps the ones that are open and not the reverse of the current direction.placeActors, write the comment and ghostFrames.choosePlayer, write chooseGhost.Skills: Game states, Arrays and objects
← A red ghost The ghost walks →