Skip to content

Flat-Earther/SnakeGame-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

NEAT Snake Game

An implementation of the classic Snake game where artificial intelligence agents learn to play using the NEAT (NeuroEvolution of Augmenting Topologies) algorithm.

Overview

This project demonstrates neuroevolution by training neural networks to control a snake agent. Instead of programming the snake's behavior directly, the algorithm evolves neural networks over multiple generations, gradually improving their ability to:

  • Navigate towards food
  • Avoid walls and boundaries
  • Avoid colliding with their own body
  • Maximize the length of the snake

What is NEAT?

NEAT is a genetic algorithm that evolves artificial neural networks. Unlike traditional neural networks with fixed architecture, NEAT:

  • Starts with minimal networks
  • Gradually adds neurons and connections through mutations
  • Uses a speciation mechanism to protect innovative solutions
  • Evolves both the topology (structure) and weights of the network

This allows complex behaviors to emerge without manual network design.

Features

  • Adaptive AI: Neural networks that evolve to play Snake
  • Visualization: Real-time rendering of the best-performing snake
  • Fitness Evaluation: Multiple reward/penalty mechanisms:
    • Bonus for moving towards food
    • Penalties for moving away or standing still
    • Large reward for eating food
    • Penalties for hitting walls or body
    • Bonus for going straight towards adjacent food
  • Population-based Training: Multiple snakes train simultaneously each generation
  • Winner Playback: After training, watch the best snake play

Requirements

  • Python 3.x
  • pygame
  • neat-python

Installation

  1. Clone or download this project
  2. Install required dependencies:

pip install pygame neat-python

Running the Project

python main.py

The program will:

  1. Train the neural network population for 100 generations
  2. Display training statistics to the console
  3. After training completes, play the best-evolved snake agent

How It Works

Game State (Inputs)

The snake's neural network receives 8 inputs about its current state:

  1. fruit_straight: Is food directly ahead?
  2. fruit_forward: Is food in the forward direction?
  3. fruit_left: Is food to the left?
  4. fruit_right: Is food to the right?
  5. danger_forward: Is there danger ahead (wall or body)?
  6. danger_left: Is there danger to the left?
  7. danger_right: Is there danger to the right?
  8. body_length: Current snake length (normalized by 50)

Network Output

The neural network produces 3 outputs corresponding to:

  • Turn left (-1)
  • Go straight (0)
  • Turn right (1)

Fitness Function

Snakes are scored based on:

  • +0.1: Going straight
  • -0.2: Turning
  • +1.0: Moving closer to food
  • -0.2: Moving away from food
  • +80 - steps_taken: Eating food (bonus for eating quickly)
  • -30: Hitting wall or body
  • Max steps: 80 moves per food (prevents infinite loops)

Configuration

The NEAT algorithm is configured via config-feedforward.txt. Key parameters include:

  • Population size
  • Number of generations
  • Mutation rates
  • Activation functions
  • Network structure constraints

File Structure

Snake/ main.py # Main game and NEAT training logic config-feedforward.txt # NEAT configuration file README.md # This file

Classes

Snake

Represents the snake agent with:

  • Position and direction tracking
  • Body segments
  • Movement and growth mechanics
  • Collision detection

Fruit

Represents the food target with:

  • Random spawning
  • Collision detection with snake

Future Enhancements

  • Configurable difficulty levels
  • Save/load trained networks
  • Multiple game modes
  • Statistics visualization
  • Performance metrics tracking
  • Adjustable simulation speed

References

License

This project is provided as-is for educational purposes.