HomeWebsitesProjectsContactAbout Me

Playing Coin Game W AI

Playing a simple pygame game with supervised learning and rl.

Made in 2025

This article was written in 2026, and the code was slightly touched up in 2026. However, I wrote the original code sometime between 2024 and 2025.

Demo

Background

Before I made this project I saw alot of cool RL videos on my yt feed. This inspired me to make my own RL project. I used a neural network to play Coin Game. Coin Game is a simple 2d game where you have to collect coins and avoid enemies, and green circles slow you down. To render the game I used pygame, and to train the neural networks I used pytorch.

Supervised Learning

One of the most classic machine learning techniques is supervised learning. In supervised learning, the model is given a dataset of inputs and outputs, and the model tries to find a function that maps the inputs to the outputs. In this project, I used supervised learning to train the model of the playthroughs of Coin Data, and try to replicate the player.

Env Structure/Reinforcement Learning

In reinforcement learning models have no correct choice they try to choose. Instead the environment/game gives a numerical reward for the model's choice, but the model does there is no one correct choice like in supervised learning. My environment rewards the model for collecting coins, and punishes the model for touching enemies, and it gives a slightly less harsh punishment for touching green circles. I also noticed that the model did not understand the border of the screen, and would keep trying to run at it. To stop this I made the enviorment punish the model for touching the border.

Download

envRecorder.py is the file you should run to record the playthroughs. Once you save a playthrough by clicking space, you can train the model using supervised.py. You can also train the model using reinforcement learning by running reinforcement.py.

Download Source Code