Practical overview
When it fits
Use it when you want a code-based example of genetic-algorithm training for a game, or need to save and replay the resulting Snake agents.
Who it is for
- Python learners exploring neural networks and genetic-algorithm training through a game.
- Developers who want to adjust training parameters and replay saved Snake-playing results.
Documented uses
- Train a neural network to play Snake.
- Save the best snake from each generation to a pickle file.
- Run saved snakes in the game and optionally skip steps during playback.
Requirements & limits
The public repository is licensed under MIT. Its documented workflow uses Python 3 and a terminal.
- The snake’s neural network receives 24 inputs based on observations in eight directions: food, body parts and boundaries.
- Training and replay are separate scripts; training writes a pickle file that the game script reads.
- The project includes a requirements file for installing its Python dependencies.
Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.
Install dependencies and train a Snake-playing model
Install the project dependencies, adjust training parameters, and save generation results to a pickle file.
Before you start
- Python 3 and a terminal
- The project repository, including its requirements file
Steps
Install dependencies with the command below.
python3 -m pip install -r requirements.txtAdjust the genetic-algorithm parameters in
input.py.Run the command below to save the best snake from each generation.
python3 Genetic_algo.py --output saved/test.pickle
Expected outcome
A pickle file containing the best snake from each generation is saved at the specified output path.
Guide limitations
- The instructions describe training and saving results; they do not specify recommended parameter values.
Replay saved snakes
Load and run snakes from a previously saved pickle file.
Before you start
- Python 3 and the installed project dependencies
- A saved pickle file containing snakes
Steps
Run the command below, substituting the path to the saved file.
python3 game.py --input saved/test.pickleTo skip steps during playback, add
--steps 50or another step value to the command.
Expected outcome
The game runs the snakes stored in the selected file, with an optional step-skipping argument.
Guide limitations
- The guide assumes a saved pickle file is available.
Sources & review
Source record details
- Source availability
- Active
- Sources checked
- 18 October 2026
- Original website title
- aliakbar09a/AI_plays_snake · Official homepage title · retained in its source language

