Skip to content

Matt-Alaei/Snake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake

Project Description

This is a snake game developed using pygame. The classic snake game had only one consumable, apple. I've added two more consumables; ice, and chili. The ice and chili each have different effects. The ice, once consumed, will slow down the snake, and the chili will increase snake's speed. The apple will also increase the snake's speed, but at a lower rate. An AI agent has also been implemented using NEAT.

NEAT Specifications

Inputs of the NNs

The inputs of the neural netwerks (NNs) generated by NEAT are as follows:

  1. the location of the apple relative to the snake's head (6 different possible states)
  2. Whether the snake is moving towards the apple
  3. In what direction does danger (by danger here we mean if the snake moves in that direction, it will lose) lie (since the snake can either continue to go forward, or turn left or right, there are 3 distinct possiblities)
  4. The normalized distance between snake's head and the apple
  5. The normalized absolute difference between the apple's and snake's head x coordinate,
  6. The normalized absolute difference between the apple's and snake's head y coordinate.

In conclustion, there are 13 inputs for each NN.

Outputs of the NNs

Each NN determines whether the snake should go straight, turn right, or turn left. Meaning that each NN has 3 outputs.

Reward Scheme

The snake is rewarded for staying alive, but this is limited until the snake has reached a score of 15 (eaten 15 apples). This is implemented to inhibit the snake from learning to just stay alive and end up avoiding eating the apple. The model (snake) is also encouraged (rewarded) when moving towards the apple and eating an apple.

On the other hand, the model is punished when colliding with itself or the boundaries of the game (in other words, when losing). In addition, the snake is punished when it takes too long to eat an apple and also when it keeps rotating around itself.

The model has 3 outputs, each representing the 3 possible actions that the snake can take at each frame: continue going forward, turn left, turn right.

Installation and Play

  1. Clone the repository:
git clone https://github.com/Matt-Alaei/Snake
  1. Install the requirements:
pip install -r requirements.txt

And that's it! By running Snake.py you're all set up to play and see how the AI agent plays the game!!

Architecture

Snake/
├── Snake.py         # Game
├── neat config      # NEAT specification
├── icons/           # Icons used in game
├── sound effects/   # Sound effects used in game

Developement

An implementation of PPO is actively being developed (see PPO branch) and will be merged with main.

About

snake solved using NEAT

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages