Skip to content

sotnich/pygame-intro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple PyGame game with Reinforcement Learning

Simple runner game with where a player can jump over two types of obstacles: flies and snails. If player hits the obstacle the game ends. The goal of the game is to run as long as possible. The game allows you either to play it yourself or turn AI so that the game plays itself.

screenshot

How to run

Game is written in pygame framework and has two option to run:

  1. With human interaction (you must press space to jump)
cd ./src
python start_human.py 
  1. With RL trained agent (AI jumps on its own)
cd ./src
python start_training_qtable.py

The training will take a few minutes. After the training is complete, the result Q-table will be saved in the directory .\qtables. If you run the game again it will work with the saved Q-table without additional training.

Before execute any of the commands above you must install python requirements:

pip install -r requirements.txt

How it was created

The purpose of the creation of the game is to practise the pygame framework and practise Reinforcement Learning a bit.

  1. As the basis I took this example from Clear Code YouTube channel
  2. Made some refactoring
  3. Then added custom GYM environment. You could read more about creating your own GYM environment here
  4. And then implemented Q-learning algorithm which allows training RL agent

Custom GYM environment

Every GYM environment for Reinforcement Learning contains two important spaces: the observation space and the action space.

For the Action Space I took a space with two distinct values: { 0 - not to jump, 1 - jump}

For the Observation Space for simplicity I took a space with l1*l2*l3*l4 distinct values where:

  • l1 means all possible positions of the player in space. As the player doesn't move horizontally, this value simply means the height [0, 200].
  • l2 means all possible gravity coefficients of the player [-20; 20].
  • l3 means which type is the closest obstacle to the player {0 - no obstacle, 1 - snail, 2 - fly}.
  • l4 means all possible positions of the closest obstacle, just its horizontal coordinate [-100, 200]. We ignore obstacles that are far from the player (with x coordinate more than 200)

Observation Space

About

Simple PyGame game with Reinforcement Learning

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages