snake_pathfinding_ai
snake_pathfinding_ai copied to clipboard
AI plays snake game using BFS (Breadth-First Search) algorithm.
Snake Pathfinding AI
This AI-powered snake game uses the Breadth-First Search (BFS) algorithm to navigate the snake autonomously. Learn how this project was developed by watching this YouTube video (Arabic).

Setup and Execution
- Download and install Python 3.
- Install the required modules by executing the following command in your terminal:
pip install pygame
- Run
play.pyto start the game.
Code Overview
settings.py: This file houses game configurations and global variables such as width and height.snake.py: This is where the Snake and Square classes are defined.play.py: Contains the code required to run the game.
Functionality
- The snake uses the BFS algorithm to find the shortest path (path_1) to the apple. If path_1 is not accessible, the process moves to step 4.
- A virtual snake, identical to the actual one, is created and set to follow path_1.
- Once the virtual snake reaches the apple, the path between its head and tail (path_2) is checked. If accessible, the actual snake is then directed to follow path_1.
- If either path_1 or path_2 is inaccessible, the actual snake is commanded to follow its tail.
For more details, please read the comments in the .py files.
Contributors
- Hayder Kharrufa - Initial work
License
This project is licensed under the MIT License - see the LICENSE.md file for more details.