SpacePort Universe Play-To-Earn by Machine Learning

AI learns to play SpacePort by Python FastAI and OpenCV

**Rug pull!**
The game is no longer alive, but the skills to build AI is still worth to learn.

Cryptocurrency is subject to high market risk. The Content is for informational purposes only, you should not construe any such information or other material as legal, tax, investment, financial, or other advice.

SpacePort is a revolutionary retro style arcade game where players can PLAY TO EARN. Game is completely free to play for all users! If you hold $SPC, you can earn 2% per day by playing SpacePort. For more detail, please see the whitepaper here.

The game is simple by moving your spaceship to shot the enemies for scores. If enemies escaped, you will lose some scores. Also, don’t get hit by them, you only have 3 life.

In this article, we will build an AI to play this game by looking at the game and do real-time reactions. We need is OpenCV for processing the images, FastAI for easy machine learning, and some keyboard control and listener libraries.

Prepare for training data

First, we have to prepare the training set by recording how you play.

I only use WASD to move my spaceship and Space for laser. So, there are 6 cases of response throughout the game, (1) W for forward,(2) press A to move leftward, (3) S for backward, (4) D for rightward, (5) Space for laser, and (6) without pressing any key. Here is the function checking the pressed keys.

Then, we capture the images and player responses during the game and save them.

This image explained the code how we simplified the images. The original images are too complicated for training the neural network. Then, resize to 224-by-224 which is the input size of ResNet-18.

Next, we save all images to the corresponding directory. You can record more games to get more images. The more training data we have, the more clever AI create!

|-- W
| |-- W1.png
| |-- W2.png
| `-- ...
|-- A
| |-- A1.png
| |-- A2.png
| `-- ...
`-- ...


After we prepared the training data, it’s time to use FastAI to train our model. A tutorial can be found here. FastAI is so simple that things are done under the hood and happen automatically in just a 1-liner code.

This is my training log output. I recorded 4 games and captured more than 110,000 images. It took 6 hours for my RTX2070 to train 10 epoches.

Total Images:117364
epoch train_loss valid_loss error_rate time
0 0.899820 0.869228 0.307686 41:13
epoch train_loss valid_loss error_rate time
0 0.558516 0.509685 0.181578 33:43
1 0.441059 0.434110 0.147452 41:00
2 0.361761 0.359694 0.121805 35:56
3 0.263690 0.278684 0.094964 35:28
4 0.173640 0.242842 0.078263 34:22
5 0.125888 0.207312 0.066207 34:04
6 0.073190 0.204656 0.056408 34:10
7 0.035775 0.200382 0.050486 34:17
8 0.013322 0.224554 0.048441 34:35
9 0.002859 0.228516 0.048228 34:07


Finally, here is the result for the model trained with just 4 games! It looks stupid but it is doing something right. It aims at the enemies and uses lasers when too many enemies.

To improve the performance, when we look into the dataset, it is very imbalanced. I trained again with a more balanced dataset by reducing “Nothing” randomly to 20000 images and here is the improvement.

║ Action ║ Count ║
║ A ║ ~20020 ║
║ s ║ ~4775 ║
║ D ║ ~17650 ║
║ W ║ ~2500 ║
║ Nothing ║ ~65000 ║
║ Space ║ ~550 ║

We can simply further improve the model by adding more data to it.

To be Continued

You can find the full code for this project here in my github. Next time we’ll go over the game with another machine learning approach.

Do you have any thoughts? I’d love to hear from you.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store