OpenViSTAI · Agents · Blockchain · Crypto
Menu
AI Models

AI Plays Snake

AI Plays Snake is a Python project that trains a neural network to control a Snake game using a genetic algorithm, then saves and replays the resulting snakes.

Independent OpenViST project profile, based on linked sources.

AI Plays Snake — OpenViST project profile in AI Models.
OpenViST project illustration. See the linked sources for product details.
Understand the project

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.
Before you choose

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.

Source-based practical guide

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

  1. Install dependencies with the command below.

    Documented command
    python3 -m pip install -r requirements.txt
  2. Adjust the genetic-algorithm parameters in input.py.

  3. Run the command below to save the best snake from each generation.

    Documented command
    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.
Source-based practical guide

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

  1. Run the command below, substituting the path to the saved file.

    Documented command
    python3 game.py --input saved/test.pickle
  2. To skip steps during playback, add --steps 50 or 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.
Evidence and transparency

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