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.
Game is written in pygame framework and has two option to run:
- With human interaction (you must press space to jump)
cd ./src
python start_human.py
- 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
The purpose of the creation of the game is to practise the pygame framework and practise Reinforcement Learning a bit.
- As the basis I took this example from Clear Code YouTube channel
- Made some refactoring
- Then added custom GYM environment. You could read more about creating your own GYM environment here
- And then implemented Q-learning algorithm which allows training RL agent
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)