An interactive educational tool that visualizes the A* search algorithm in real-time using Python and Pygame.
This application provides an intuitive, visual way to understand how the A* (A-Star) algorithm explores a grid-based environment to find the optimal path between two points. Watch as the algorithm systematically evaluates nodes, navigates around obstacles, and discovers the shortest route using heuristics.
- Interactive Grid System: Click and drag to create custom maze layouts
- Visual Algorithm Execution: Real-time visualization of the pathfinding process
- Customizable Environment: Place start points, end points, and walls anywhere on the grid
- Path Highlighting: Clear visualization of the discovered shortest path
- 🟠 Orange: Start node
- 🔵 Turquoise: End/Target node
- ⬛ Black: Walls/Obstacles
- 🟢 Green: Open nodes (being considered)
- 🔴 Red: Closed nodes (already evaluated)
- 🟣 Purple: Final shortest path
- Set Start: Define the starting position (First Left Click)
- Set End: Define the destination point (Second Left Click)
- Draw Walls: Create obstacles (Subsequent Left Clicks + Drag)
- Run A*: Execute the pathfinding algorithm (
SPACEBAR) - Reset: Clear the entire grid and start fresh (
C) - Right-Click: Remove any placed element
- Python 3.7 or higher
- pip (Python package installer)
-
Clone or download this repository
git clone https://github.com/harshil2424/A-star-pathfinding.git cd A-star-pathfinding -
Install required dependencies
pip install pygame
-
Run the application
python main.py
- Launch the application - Run
python main.py - Place the start node - Left click on any grid cell
- Place the end node - Left click on another grid cell
- Draw obstacles - Left click and drag to draw walls
- Run the algorithm - Press
SPACEBAR - Reset if needed - Press
C
| Action | Control |
|---|---|
| Place nodes/walls | Left Click |
| Remove items | Right Click |
| Drag to draw walls | Click + Drag |
| Key | Action |
|---|---|
SPACEBAR |
Run A* algorithm |
C |
Clear/Reset the grid |
A* is a smart graph search algorithm that finds the shortest path by combining actual cost from start ($g(n)$) and estimated cost to goal ($h(n)$).
Formula:
-
$g(n)$ : Exact cost from start to node$n$ . -
$h(n)$ : Heuristic estimated cost from$n$ to goal (Manhattan Distance). -
$f(n)$ : Total estimated cost of path through$n$ .
- Green nodes: Nodes in the open set (candidates for exploration).
- Red nodes: Nodes in the closed set (already evaluated).
- Purple path: The reconstructed cheapest path.
A-star-pathfinding/
│
├── main.py # Main entry point
├── settings.py # Constants and configuration
├── node.py # Node class definition
├── algorithm.py # A* algorithm logic
├── README.md # Project documentation
└── requirements.txt # Dependencies
- Adjustable heuristics
- Diagonal movement
- Variable terrain weights