View on GitHub

AI-snake-game

An AI agent learns to play Snake using Deep Q-Learning. Built with PyGame and PyTorch, it navigates the snake to eat food while avoiding collisions. Watch as the AI improves its gameplay through training!

AI-Trained Snake Game with Deep Q-Learning

This project implements an AI agent trained using Deep Q-Learning to play the classic Snake game. The agent learns to navigate the snake to eat food while avoiding collisions with itself and the game boundaries. The game environment is built using PyGame, and the neural network model is implemented with PyTorch.

Table of Contents

Overview

This project demonstrates how an AI agent can be trained to master the game of Snake using reinforcement learning, specifically Deep Q-Learning. By leveraging PyTorch for the neural network and PyGame for the game interface, this project provides a hands-on example of AI in gaming.

Gameplay

Gameplay

Game Components

Features

Installation

Follow these steps to set up and run the project on your local machine.

Prerequisites

Make sure you have the following installed:

Steps

  1. Clone the Repository:

    Clone this repository to your local machine:

    git clone https://github.com/your-username/AI-snake-game.git
    cd AI-snake-game
    
    
  2. Install Dependencies:

    pip install -r requirements.txt
    

Usage

To train the AI agent and watch it play Snake game:

python agent.py

The game will start, and you can observe the AI’s gameplay and training progress through the console and any plotted metrics.

Configuration

The training parameters and game settings can be adjusted in the agent.py and snake_game.py files. Key parameters include:

Training

The AI is trained using the Deep Q-Learning algorithm, where it learns by interacting with the game environment, storing experiences, and updating its policy based on rewards received.

Model Architecture

The model consists of a simple feedforward neural network with the following layers:

Results

The AI agent demonstrates the ability to play Snake autonomously, gradually improving its score as training progresses. However, certain challenges such as the snake circling in the same area persist.

Challenges

Credits

This project is inspired by OpenAI’s reinforcement learning techniques and incorporates ideas from various Deep Q-Learning resources.

And special thanks to FreeCodeCamp.org –> Watch the Video