AI plays snake game using BFS (Breadth-First Search) algorithm.
Last updated May 2, 2026
44
Stars
18
Forks
0
Issues
0
Stars/day
Attention Score
9
Topics
Language breakdown
Python 100.0%
โธ Files
click to expand
README
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 (path1) to the apple. If path1 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 (path2) is checked. If accessible, the actual snake is then directed to follow path1.
- If either path1 or path2 is inaccessible, the actual snake is commanded to follow its tail.
.py files.
Contributors
- Hayder Kharrufa - Initial work
License
This project is licensed under the MIT License - see the LICENSE.md file for more details.
๐ More in this category