A modern C++ implementation of the classic Pac-Man game featuring intelligent ghost AI using different pathfinding algorithms. Built with SFML (Simple and Fast Multimedia Library) and advanced graph algorithms.
Project Video: [https://youtu.be/0M5LkG5rUYM)]
- Classic Pac-Man gameplay with smooth movement
- Food collection system with score tracking
- Multiple ghost types with unique AI behaviors
- Anti-camping mechanisms to prevent ghost stalling
- Dynamic difficulty adjustment based on remaining food
- BFS Ghost: Uses Breadth-First Search for direct pursuit with random movement elements
- A Ghost (A-Star Search): Intelligent pursuit of Pac-Man using heuristic-guided pathfinding
- Dijkstra Ghost: Uses Dijkstra's algorithm with weighted edges based on food positions for optimal pathfinding
- Graph-based maze representation
- Multiple pathfinding algorithms (BFS, DFS, Dijkstra)
- Object-oriented design with inheritance and polymorphism
- Strategy pattern for AI behavior
- SFML graphics with sprite animation
- C++17 - Core programming language
- SFML 2.5+ - Graphics, audio, and input handling
- Graph Theory - Maze representation and pathfinding
- Object-Oriented Programming - Clean architecture with inheritance
Before running this project, make sure you have:
- C++ compiler with C++17 support (GCC 7+, Clang 5+, or MSVC 2017+)
- SFML 2.5 or higher
- CMake 3.10+ (recommended for building)
vcpkg install sfmlbrew install sfmlsudo apt-get install libsfml-devgit clone https://github.com/yourusername/pacman-ai-game.git
cd pacman-ai-gamemkdir build
cd build
cmake ..
makeg++ -std=c++17 -o pacman *.cpp -lsfml-graphics -lsfml-window -lsfml-system./pacman- Arrow Keys or WASD - Move Pac-Man
- ESC - Pause/Exit game
- R - Restart game
- Collect all food items while avoiding the ghosts
- Each ghost has different behavior patterns:
- Red Ghost (BFS): Direct pursuer with occasional random moves
- Blue Ghost (DFS): Patrols between food items unpredictably
- Green Ghost (Dijkstra): Uses optimal pathfinding considering food positions
pacman-ai-game/
โโโ src/
โ โโโ ghost.h # Base ghost class
โ โโโ ghost.cpp
โ โโโ bfsGhost.h # BFS pathfinding ghost
โ โโโ bfsGhost.cpp
โ โโโ dfsGhost.h # DFS pathfinding ghost
โ โโโ dfsGhost.cpp
โ โโโ dijkstraGhost.h # Dijkstra pathfinding ghost
โ โโโ dijkstraGhost.cpp
โ โโโ pacman.h # Player class
โ โโโ pacman.cpp
โ โโโ Graph.h # Graph representation and algorithms
โ โโโ Graph.cpp
โ โโโ Food.h # Food item class
โ โโโ Food.cpp
โ โโโ main.cpp # Main game loop
โโโ Assets/
โ โโโ images/ # Sprite images
โ โโโ sounds/ # Audio files
โโโ CMakeLists.txt # CMake build configuration
โโโ README.md # This file
- Anti-camping system: Detects when ghosts are stuck and forces movement
- Polymorphic movement: Uses strategy pattern for different AI behaviors
- Shared movement logic: Common pathfinding execution and sprite animation
- Behavior: Direct pursuit of Pac-Man
- Features:
- 20% chance of random movement for unpredictability
- Guaranteed shortest path when chasing
- More aggressive when few food items remain
- Behavior: Intelligent pursuit of Pac-Man using heuristic-guided pathfinding
- Features:
- 20% chance of random movement for unpredictability
- Guaranteed optimal path with improved efficiency over BFS
- Uses Manhattan distance heuristic to prioritize promising directions
- More aggressive when few food items remain
- Faster pathfinding compared to pure BFS while maintaining shortest path guarantee
- Behavior: Optimal pathfinding with weighted edges
- Features:
- Considers food positions when calculating paths
- Uses weighted graph based on food density
- Most sophisticated AI with strategic positioning
- Inherit from the
ghostbase class - Override the
findPath()methods - Implement your pathfinding strategy
- Add to the game's ghost vector
Example:
class MyCustomGhost : public ghost {
protected:
std::vector<int> findPath(int ghostNodeId, int pacmanNodeId, Graph& g) override {
// Your custom pathfinding logic here
return customAlgorithm(ghostNodeId, pacmanNodeId, g);
}
};- Ghost speed: Adjust
speedmember in ghost constructors - Path recalculation frequency: Modify
moveCounterthreshold inmovement() - Anti-camping sensitivity: Change
stationaryCounterthreshold inisStuck()
Contributions are welcome! Here are some ways you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
- Classic Pac-Man game by Namco for inspiration
- SFML community for excellent documentation
- Graph algorithm implementations adapted from computer science literature
โญ Star this repository if it helped you learn about game AI or graph algorithms!