SpaceRace: a smart spawner for more fun!

Fernando Rosa
Apple Developer Academy | Federico II
9 min readApr 16, 2020

Nowadays, creating a 2D videogames for iOS systems it is easier than you think, thanks to the existing development systems. Making it graphically appealing is more complex, and usually it is necessary a visionary designer (we had the chance to meet Simona Zimona Correale…). By the way, with a little effort of imagination (maybe getting some ideas from other videogames) and a bit of fancy, you can attain a product able to kindle curiosity of the players even without a pixel guru. If you succeed adding a simple gaming mechanics and an intriguing story, you reached your goal: making a player download and play it at least once in his life.

Obviously, it is necessary a bigger effort if we want to define a game a success. Indeed, if relying on simple mechanisms based on pre-established schemes allows you to quickly create a product to be published in the shortest possible time, in the long run the idea of confronting a simple automaton that simply replicates pre-established behaviors, leads the user inexorably towards the chasm of boredom, whose next step is always the fateful “Delete App”. In order for a video game to be worthy of being played a second time, and then a third and so on, it must have other qualities, whose the sum of which should prompt the user to ask himself at the beginning of each level “What awaits me now?” .

And this is where our story begins, from the desire to make our game SpaceRace a game that deserves to be played more than once, where every time is different from the previous one. How to do it? Using a branch of Artificial Intelligence known as Machine Learning, made accessible in iOS systems thanks to CreateML and CoreML.

Machine Learning: learn and predict

Learning is a really complex process, which in our case is made relatively simple by the proper use of the most powerful computing unit in the world: the human mind. Sure that being able to replicate the learning method of us humans would have made it possible to make significant progress in their field of study, part of the scientists and researchers involved in Artificial Intelligence gave life to Machine Learning, at the end of the twentieth century.

The concept behind this discipline is very simple: allowing a machine to learn from observations in order to predict future actions. Like a child who can learn how painful it is to burn himself by touching a lit match flame, so a computer can learn to identify a possible pandemic based on data relating to cases of seasonal flu in certain countries of the world.

Of course we cannot and we do not want to deal in depth with such a complex topic as Machine Learning, but we think it is useful to say that there are 3 types of ML:

  1. Supervised Learning, to predict results;
  2. Unsupervised Learning, to find hidden structures in the data;
  3. Reinforcement Learning, to improve interactions.

The common concept between all these techniques is the need to have available observations, structured data, divided into training data and testing data, which allow the machine to build and validate a model that will be the base of its knowledge.

By studying how Apple has made it possible for developers to build models and use them to make their apps smart, we thought that ML could help us achieve our goal: to give SpaceRace a “mind” to make it more fun.

SpaceRace and SpaceCowboys

SpaceRace by Space Cowboys

What is SpaceRace? SpaceRace is a 2D video game of scientific disclosure, which aims to provide the user with information relating to the race of man in conquering space and at the same time entertaining him. It is the result of the hard work of the SpaceCowboys team, of which we have been part. It has captivating graphics, a structured story and a simple game mechanics, divided into Planets (worlds) and Missions (levels). In each level the user is engaged in a recovery mission and he has to recover pieces of probes drifting in space being careful to avoid spatial garbage and micro meteorites that can damage his suit and his jetpack.

SpaceRace game scene

As we said at the beginning of our story, a video game must present a game mechanics that pushes the user to face each level with enthusiasm. So we wondered how ML could help us make our game more exciting. To ensure that this research was not carried out blindly, we tried to identify which component of the game we could give intelligence to.

It was not difficult to understand that the fundamental element of the game, currently devoid of “intelligence” and limited to behave according to predetermined schemes, was the one that most needed an evolutionary leap: the spawner. Making it smart would have made our game more fun.

Spawn it!

In 2D video games (as in all video games) there is a term that stands for the activity of generating entities within the game itself: spawn. In SpaceRace the spawner is responsible for generating three types of objects within each level:

  1. Space junk, an object (malus) that upon impact with the player causes damage to the space suite or jet pack, causing respectively a loss of oxygen, of propellant or a temporary collision radar failure, depending on the size of the object itself and the impact coordinate;
  2. Repair kit, a set of tools (bonus) that allows the player to repair damage to the space suite or jet pack;
  3. Refills, oxygen or propellant containers used by the player to refill the oxygen or propellant tank respectively.

It is therefore easy to understand how SpaceRace’s gaming experience depends on the spawner.

So once we decided that the spawner needed a modification in order to make it intelligent, to allow it to decide independently where and how to generate objects, we tried to understand what type of ML and which technique could help us achieve our goal.

To do this, we took into consideration the dataset at our disposal, the fateful observations, from which we could learn, and then generate a model for our spawner, a model to be used to predict the future…

Predict the future

So we decided to build a crystal ball that would allow the spawner to knowthe future position of the player, with a certain precision.

To do this we have identified our ideal solution in Supervised Learning (SL), and in particular in regression.

SL is one of the three types of ML and it is based on the ability to learn a behavior model, starting from data already observed, which allows to make future predictions starting from new data.

Depending on the type of forecast you want, we have two types of SL:

  1. Classification, where discrete values are used and where the ultimate goal is to predict the class to which the new data set belongs;
  2. Regression, where continuous values are used and where the ultimate goal is to predict the (continuous) result starting from the new data set.

There was nothing more suitable than regression to predict the future position of the player, based on the current data of the player!

This observation enlightened us by showing us the way for the creation of the mind to be provided to our spawner. Therefore we identified useful information to create our data set:

PVA, Player Vertical Acceleration;

  1. PVS, Player Vertical Speed;
  2. PAVP, Player Actual Vertical Position;
  3. PFVP, Player Future Vertical Position.

Now, in order to apply the regression technique to build our model, we needed to collect the observations, i.e. the real data, which we would then use to build our function matrix (the set of independent variables PVA, PVS and PAVP ) and our vector of dependent variables (the PFVP values). To do this we added a class (JsonManager.swift) to the original SpaceRace project that would allow you to save, in a .json file, the recordings of the PVA, PVS, PAVP and PFVP values relating to the player during a game session. By inserting a function for recording this data in the GameScene,

<script src=’https://gitlab.com/Kronos_Rob/spacerace/snippets/1964423.js'></script>

we obtained a series of observations that can be represented in the following data set:

Subset of observations recorded during a game session

What to do with all this data?

Thanks to these observations we built our model. How? Trusting him …

Deep Thought Super Computer

The answer is… 42

Ok it’s not true, we didn’t rely on Deep Thought, also because we couldn’t have waited for all that computational time to finally get a simple 42.

We have relied on something more powerful that would allow us to obtain a reliable model for our spawner. We therefore created a project in Create ML using regression as a generation template.

As previously seen, regression is a type of SL that uses an algorithm (based on neural networks) that exploits a mathematical concept, regression analysis, which we could summarize in a simple way like this: given a number of independent variables (explanatory) and a number of associated dependent variables (outcome or target) try to find a relationship between these variables that allows to predict a result.

In the CreateML project we have provided our dataset of observations dividing it into training data, used by the ML to try to understand the relationship between the data, and testing data, used to verify the forecasts. At this point CreateML did the dirty work for us: processing the observations in the search for relationships between the data, making predictions of results and verifying them, and finally generating a knowledge model to be used with new data.

All that remained was to test our model in SpaceRace. To do this we imported the model generated by CreateML into our project, and thanks to CoreML we used it to modify the behavior of our spawner in the GameScene.

<script src=’https://gitlab.com/Kronos_Rob/spacerace/snippets/1964424.js'></script>

As you can see by analyzing the code, we relied on our model (test1_1) the choice of the position (verticalFuturePosition) where it would have generate space junk to hit the player.

Once the changes were done we did some game sessions and do you know what we found out?

It could work!

Frankenstein Junior

It worked! Simple but effective.

Game session from SpaceRace

By observing the collision radar on the lower left you can notice how the spawner comes to anticipate the player in his movements, transmitting to the player the feeling of having an intelligent entity in front of him.

Overall simple, this first approach with ML allowed us to obtain a knowledge-based behavior to be assigned to our spawner!

So going back to our initial consideration, is this the way to take in order to exploit ML and get a successful video game?

Roads… Where we’re going we don’t need roads

No, this is not the “road”. This is just one of many “roads” … Using CreateML and CoreML we can apply degrees of complexity that take into account further aspects, thus creating different behaviours such as:

  • compassionate spawner, who gives respite to a damaged player and generates more repair kits and less space junk;
  • charitable spawner, which generates refills until the player fills his tanks;
  • strategic spawner, which generates space junk by forcing the player into a disadvantageous position and which will lead him inexorably to defeat.

Aren’t you excited? We are! Considering therefore the innumerable possibilities provided by iOS, CreateML and CoreML, it also becomes useless to talk about roads, because as we like saying:

Back to the future

From Mean Machine with love

We would like to end this story by thanking the Space Cowboys Team, with whom we made SpaceRace during our experience at the Apple Developer Academy. We love you.

Mean Machine Team:

Massimo Maddaluno
Roberto Scarpati
Fernando Rosa

--

--