Skip to content

AI paying Snake Game using Reinforcement Learning - Python

Notifications You must be signed in to change notification settings

Abhijith14/SnakeGame-ML

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Snake Game - Reinforcement Learning

A simple snake game automated using Machine Learning - Reinforcement Learning

demo



📕 Installation

🕷️ Create an environment

Whatever you prefer (e.g. conda or venv)

mkdir myproject
$ cd myproject
$ python3 -m venv venv

🕷️ Activate it

Windows:

venv\Scripts\activate

Mac / Linux:

. venv/bin/activate

🕷️ Install PyTorch and dependencies

For Installation of PyTorch see official website.

You also need PyGame, Matplotlib, IPython:

pip install pygame, matplotlib, ipython

If you get an error for numpy, install numpy separately:

pip install numpy

👨‍💻 Usage

Run

python agent.py

This will start training the agent. It takes upto 80 - 100 gameplays for the model to be trained good.
If you want to change the number of gameplays, edit this section in function get_action, class Agent in agent.py:

def get_action(self, state):
    self.epsilon = {{ Enter your number of gameplays for training }} - self.n_games

You can find the model file inside model folder.
Also while training, you can find the training graph with the number of gameplays vs Scores graph

⚙️ Customize

Have a look at game.py. You can change the game variables according to your own use case.
Change the colours of the game elements over here.

# rgb colors
WHITE = (255, 255, 255)
RED = (200,0,0)
BLUE1 = (0, 0, 255)
BLUE2 = (0, 100, 255)
BLACK = (0,0,0)

Default BLOCK_SIZE (Size of a block in the game) and SPEED (speed of snake) is set to 20.

BLOCK_SIZE = 20
SPEED = 20

In the init function of class SnakeGame; the parameters w and h are the width and height of your game window.

def __init__(self, w=640, h=480):
        self.w = w
        self.h = h

🛠️ Built With

❤️ Authors

  • Abhijith Udayakumar - Design & Development - Abhijith14


🚨 Forking this repo (please read!)

yes, with attribution.

I value keeping my work open source, but as you all know, plagiarism is bad. It's always disheartening whenever I find that someone has copied my work without giving me credit. I spent a non-trivial amount of effort building and designing this project, and I am proud of it! All I ask of you all is to not claim this effort as your own.

TL;DR

Yes, you can fork this repo. Please give me proper credit by linking back to Abhijith14/SnakeGame-ML. Thanks!

About

AI paying Snake Game using Reinforcement Learning - Python

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages