Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Giorgos Voutouri


Snake Game with Reinforcement Learning

This project is a demonstration of training a reinforcement learning agent to play the classic game "Snake". The agent learns to navigate the game board, avoiding obstacles and collecting rewards, using a deep Q-network algorithm.

Getting Started


Python 3


  1. Clone the repository { git clone }
  2. Access the repository { cd AI_agent_Snake_Game }
  3. Install the dependencies { pip install torch numpy pygame matplotlib IPython }
  4. Start the game { python3 }

How to play

Use the arrow keys to control the snake's movement. The objective is to collect the red dots while avoiding collision with the wall or the snake's own body.

Reinforcement learning

The agent uses a deep Q-network algorithm to learn the optimal policy. The state of the game board is represented as an array of binary values, with 1 representing the snake's head, 2 representing the snake's body, and 3 representing the food. The neural network takes the state as input and outputs the Q-values for each possible action. The agent selects the action with the highest Q-value at each time step.

The agent receives a reward of +1 for collecting food and a reward of -1 for colliding with the wall or the snake's own body. The training is done through experience replay, where the agent stores its experiences in a buffer and randomly samples them to update the neural network.


After training for 17,000 episodes, the agent was able to consistently achieve an overall score of 99 and an average score of 33.12. The following graph shows the learning progress over time:



Some parts from the below repository were used towards the code development of this project.


No description, website, or topics provided.






No releases published