The goal of this project is to understand and implement the pacman game using Javascript for the game mechanics and HTML5 Canvas to render the game map and its graphics.
| Technology | Purpose | Version / Type |
|---|---|---|
| HTML5 | Structure of the web page and Canvas element | HTML5 |
| CSS3 | Styling the page layout and canvas container | CSS3 |
| JavaScript (Vanilla) | Core game logic, rendering, and interactivity | ES6+ |
| HTML5 Canvas API | Drawing game elements (Pacman, ghosts, walls, food) | Built-in Browser API |
| PNG Image Assets | Sprites for Pacman (4 directions), ghosts, walls | Image Files |
| VS Code | Code editor used during development | IDE |
pacman-game/
│
├── image_assets/
│ ├── ghost_image # Ghost sprite images
│ │ ├── blueGhost.png
│ │ ├── orangeGhost.png
│ │ ├── pinkGhost.png
│ │ └── redGhost.png
│ │
│ ├── pacman_moves # Directional sprites for Pacman character
│ │ ├── pacmanDown.png
│ │ ├── pacmanUp.png
│ │ ├── pacmanLeft.png
│ │ └── pacmanRight.png
│ │
│ └── wall.png # Wall tile image used in the tilemap
│
├── index.html # Contains the <canvas> element and links JS/CSS
│
├── pacman.css # Stylesheet for page layout and canvas styling
│
├── pacman.js # Contains all game logic
│
├── LICENSE
└── README.md
- The game map is defined as a 2D array (the tileMap) with 21 columns × 23 rows. Each number in the array represents:
- 0 - Empty space (walkable path)
- 1 - Wall tile (blocks movement)
- 2 - Food pellet (small dot Pacman collects)
- 3 - Empty block (open space with no pellet)
- Each tile is drawn on the HTML5 Canvas at a fixed block size.
- Pacman is represented as an object with properties like x, y (position), direction, and speed.
- When a key/click is registered, Pacman's direction is updated.
- The game features four ghosts: Red, Pink, Blue, and Orange.
- The ghosts move in a random valid direction at each intersection (when they collide with a wall). This gives the appearance of patrolling enemies without requiring complex pathfinding. The ghosts are given different starting positions on the map to spread them across the board.
Two types of collision detection are implemented:
- Pacman vs. Walls: Before Pacman moves, the target tile is checked. If it is a wall tile (value = 1 in the tileMap), movement is blocked.
- Pacman vs. Ghosts: The distance between Pacman's center position and each ghost's center position is calculated. If the distance is below a threshold (roughly equal to half a tile size), a collision is triggered — the game resets.
- Pacman vs. Food: When Pacman moves over a tile containing food (value = 2), the tile is updated to 0 (empty) and the player's score increases.
-
Clone the repository
git clone https://github.com/swechchhapatel/pacman-game.git cd pathfinding-visualizer -
Open in browser
- Simply double-click
index.html - Or right-click → "Open with" → Choose your browser
- Simply double-click
-
Start Playing!
-
Install Live Server (VS Code extension or npm package)
npm install -g live-server
-
Navigate to project folder
cd pacman-game -
Start the server
live-server
-
Open in browser
- Automatically opens at
http://localhost:8080 - Auto-refreshes on file changes
- Automatically opens at
Contributions are welcome and appreciated!
If you'd like to improve this project, please follow these steps:
- Fork the repository
- Create a new branch
git checkout -b feature/your-feature-name
- Make changes and commit
git commit -m "Add: your message" - Push to your branch and open a Pull Request
Made with ❤️
