Workshop › Pac-Man-style Maze Chase › Step 1 of 42
Eat every pellet in the maze while four ghosts hunt you. Smooth tile-to-tile movement, buffered turns, ghost AI with four personalities, scatter and chase modes, and power pellets that turn the hunters into the hunted.
A maze game lives on a grid of tiles. The easiest way to design one is to draw it as text: one string per row, one character per tile. We take the canvas and its 2D context, name the tile size, and write the maze.
```js
const canvas = document.getElementById('game')
const ctx = canvas.getContext('2d')
const TILE = 24
const TOP = 40 // room for the score and the lives
// # wall, - the ghost house, . pellet, o power pellet, P player start. Row 9 is a tunnel: its ends are open.
const MAZE = [
'###################',
'#........#........#',
'#o##.###.#.###.##o#',
'#.................#',
'#.##.#.#####.#.##.#',
'#....#...#...#....#',
'####.### # ###.####',
' #.# #.# ',
'####.# #---# #.####',
' . #---# . ',
'####.# ##### #.####',
' #.# #.# ',
'####.# ##### #.####',
'#........#........#',
'#.##.###.#.###.##.#',
'#o.#.....P.....#.o#',
'##.#.#.#####.#.#.##',
'#....#...#...#....#',
'#.######.#.######.#',
'#.................#',
'###################',
]
const ROWS = MAZE.length
const COLS = MAZE[0].length
```canvas is the drawing area on the page, ctx its 2D drawing tools.TILE is the size of one tile; TOP leaves room above the maze for the score.MAZE is an array of 21 strings of 19 characters: MAZE[row][col] is one tile.ROWS and COLS are read from the maze itself, so a different maze would just work.Write the code under the three comment lines. You may copy the MAZE rows; check every row has 19 characters.
Skills: Arrays and objects, Drawing on a canvas