How I Made A Game
Goals
I have been aware of AI going back to when AI beat the world's best Go player. When TensorFlow and TensorFlow.js were released, I was very excited about getting the chance to work with neural networks. I needed a task so that I could train some AI to complete. I have a personal interest in video games, and figured training AI to beat a simple video game would be a fun way to work with TensorFlow.js. The goal was to write a program that I could easily use for both single player and training an AI. In order to train the AI I needed the following criteria:
- A Numeric Scoring System
- Ability to create multiple player objects at once
- Create ability for AI to be aware of suroundings
Ultimately I decided that a side scrolling 2D platformer would be a fun simple and quick game to develop. My primary objective is to work with neural networks, so I wanted to minimize the amount of dev time required. There are several advantages to this simple style of game. In a side scrolling game, the game camera progresses through the level at a fixed rate, so we don't need to worry about programming complex camera angles. We can also limit player movement on the horizontal axis, and only give them the ability to jump. As long as the player object gives the appearance of movement coupled with a scrolling background, the illusion of forward momentum can be achieved. I could also have multiple player objects all jumping at once, which is ultimately how I'm going to train the neural networks.
Implementation
At the time, I had just recently learned about a JavaScript toolset called p5.js, which works with WebGL to do animations in browser. With my background in JavaScript this seemed like the perfect toolset to use for the project. Since Tensorflow.js was available, I knew that I could do both the game and the training in browser. After familiarizing myself with the p5.js documentation, I set up a development plan and outlined the features that I would need.
The first decision I made was to use an object oriented style of programming. This allows me to quickly and easily generate as many player objects as I want for training AI. I'm also able to store each player object information separate from each other. I believe that it also helps with readability of the source code. We're able to abstract out much of the functionality, which makes it easier to maintain.
Next was to build the physics engine. The first step was to create the jump action for the player object. As I mention on my code example page, I ultimately made the physics engine as optimal as possible. We need to have a good size population for our training, so I needed to make the game as efficient as possible to free up resources for the neural network calculations.
One of the last goals I addressed was the scoring system. I wrote the code so that all of the scoring mechanics were part of a single function. This means that I can always come back at a later date and modify how I do the scoring. The score mechanism needs to guide the AI in the correct direction. If we simply count the number of jumps made, we'll just train AI that jump as many times as possible. That may or may not help in the goal of beating the game. I decided that the best method to start with is time spent alive. The farther the player object makes it, the better score they get. This optimizes the AI to try and play the game for as long as possible, essentially beating it.
Lessons Learned
After completing this project, I feel like I have gained a lot from the experience. Professionally I do a lot of work with jQuery, so being exposed to other JavaScript libraries has taught me a lot. I enjoyed getting a chance to do more work with Objects, and it was interesting to see how far I could push it. In the end I felt like I have a better appreciation for JavaScript and learned a few new tricks along the way.
I have built a few simple games for different college classes, but those are usually text based games such as hangman. This was my first experience with a physics based video game, and was a fun problem to solve. I would be interested in seeing what sort of professional applications I could find for p5.js, but I don't see many situations where this would be the best option.
I feel like in the end the game met all of the goals that I initially set. It turned out to be an excellent platform for working with neural networks. I also like having a playable game as part of my portfolio, because it encourages users to interact with the site more and see what else there is. This fits nicely in my portfolio of examples.