This is a clone of the popular retro arcade game! Collect the dots to move on to the next level. Make sure not to get possessed by the ghosts!
- HMTL/CSS
- Canvas
- Vanilla JavaScript
- Object Oriented Programming
I think conceptually, the toughest part of the project was creating the board. At first, I started by drawing out the map manually with coordinates of where the walls should be.
// border
const radius = 10;
// TOP HALF
ctx.beginPath();
ctx.moveTo(20, 270);
// left middle part
ctx.arcTo(200, 270, 200, 260, radius);
ctx.arcTo(200, 200, 190, 200, radius);
ctx.arcTo(20, 200, 20, 190, radius);
However, that direction took too long, was very tedious and unreliable. Therefore, I went with a 2d array in order to create the grid.
- 0 => wall
- 1 => dot
- 2 => power pellet
- 3 => empty
- 4 => tunnel
- 5 => one way wall for ghosts
- 6 => wall
this.grid = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // 0
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], // 1
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0], // 2
[0, 2, 0, 3, 3, 0, 1, 0, 3, 3, 3, 0, 1, 0, 0, 1, 0, 3, 3, 3, 0, 1, 0, 3, 3, 0, 2, 0], // 3
[0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0], // 4
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], // 5
[0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0], // 6
[0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0], // 30
];
With the 2d array in place, iterating over the array generates the board. Additionally, the 2d array creates a quick lookup time for evaluating collision detection. All in all, the 2d array helped design better, more efficient code while being super flexible to any change necessary.
- Add pacman dying animation
- Add next level screen
- Add blinking to indicate power ending transition
- Add smarter AI for ghosts