Skip to content

bebopkenny/NeuralSnake

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Snake Game AI

An AI-powered Snake game built using Python, Pygame, and PyTorch. The AI agent learns to play the Snake game using Q-Learning with a Deep Neural Network.


Demo Video

Watch a quick demo of the AI Snake Game in action:
🔗 Click here to watch the video


Tech Stack

  • Python
  • Pygame
  • PyTorch
  • Conda (for environment management)

Features

  • AI agent learns to play the Snake game using Reinforcement Learning.
  • Real-time gameplay with dynamic UI updates.
  • The agent improves its performance over time using Q-Learning.
  • Graphical Plot to visualize the agent's progress (score and mean score).
  • Fully customizable codebase with detailed comments for learning purposes.

Screenshots

Game Interface

Snake Game Screenshot

Training Graph

Training Graph Screenshot


Installation Guide

1. Prerequisites

Ensure you have the following installed on your machine:

  • Python 3.7+
  • Conda

2️. Clone the Repository

git clone https://github.com/bebopkenny/Snake-AI-Game.git
cd Snake-AI-Game

3️. Create a Conda Environment

conda create -n pygame_env python=3.7
conda activate pygame_env

4. Install Dependencies

pip install -r requirements.txt

Usage

To Play the Game:

python game.py

To Train the AI:

python agent.py

How It Works

The AI agent is trained using Q-Learning, a type of reinforcement learning where the agent learns to maximize rewards by taking specific actions.

AI Logic:

  • State Representation: The agent observes the game state, including the position of the snake's head, food location, and possible collisions.
  • Action Space: The agent can move in three directions: straight, left, or right.
  • Reward System:
    • Positive reward (+10) for eating food.
    • Negative reward (-10) for hitting a wall or itself.

Deep Neural Network:

The agent uses a Linear Neural Network built with PyTorch to predict the best possible action based on the current state.


Graphical Plot

A real-time graph is displayed during training to visualize the agent's progress:

  • Number of Games vs. Score
  • Mean Score over Time

The graph is generated using Matplotlib.


Contact


Future Improvements

  • Add more complex reward structures to improve the agent's decision-making.
  • Optimize the neural network architecture for faster training.
  • Implement additional gameplay features such as obstacles and different game modes.

License

This project is open-source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages