Building Intelligent Games

rasras1 pts0 comments

Building Intelligent Games – Rasmus Rasmussen dot com

Skip to content

In the world of game development the term AI has been around for decades, though it was not referring to chatbots and neural networks but rather things like finite state machines and behavior trees. Both of those are fantastic tools for game development and everyone should use them, but this post is not about either. This is about using transformer networks as a way to dynamically control the level of gameplay difficulty. Instead of hard coded values, this is based on individual, (almost) real-time player behavior!

Instead of an old school difficulty slider that locks in specific game parameters to make the game harder or easier all round, a neural network can learn to react and adjust the difficulty moment to moment and without a slider anywhere in the game menus.

This is not theory, this is fairly straightforward to build and I have already done it. It is not a massive LLM we are creating after all, just a tiny model with minimal memory thanks to its transformer nature. Just enough to do this one thing. Data comes in, the network processes it all and chooses an action.

Building and Training the Model

To build a neural network like this, you start with player statistics, such as:

movement direction

position

recent kills

lives left

time since last death

total time played

number of enemies on-screen

Plus any other metrics that together paint a picture of the current state of the game. These data points become the inputs for the model. You can experiment with hidden layers and nodes but you really don’t need a lot.

Next a set of actions that this AI game master should be able to take are defined, and those become the outputs. The exact actions will depend on your project, but to use this demo as an example, output actions include:

spawning different enemy types and formations

increase or decrease the overall pressure (spawn rates and budget)

temporarily back off to give the player a moment to breathe

launching a massive wave of attack

If you’re building your own, you will need to define these actions ahead of time, and make sure your game code has all the necessary functions wired in to trigger when the neural network sends the signal to do so.

The hardest part is training this game master model, and getting enough data to do so. The demo game has a built-in data snapshot function that also takes a screenshot. To turn this into training data, I had to label it and evaluate every entry. It is not impossible to do, just tedious.

For this demo, I hand-labeled over 1000 entries and trained the AI on them with early stopping to avoid overfitting. I didn’t write it down, but I think it trained for around 20 epochs. The actual training took very little time.

Afterwards, the fully trained model can be converted to a format that is friendly to your game engine of choice. Godot does not natively support neural network formats like ONNX, but because the model is so small, converting the whole thing to a format that works is not a big deal.

Introducing Space Base Bomb Run

To demonstrate the idea, I put together a small tech demo. It’s a vertically scrolling shoot-em-up game where the waves of attacking enemies are controlled by exactly this kind of neural network gamemaster. Every 4 seconds, it takes a snapshot of what’s going on in the game, and takes an action accordingly. It can choose different enemies and attack patterns, as well as tweak the overall spawn rates and budget (higher budget = more elite enemies).

You can toggle visualization of the network on and off while playing (press v) to see it in action, though be warned that is does cover part of the screen if you do so.

I call the demo "Space Base Bomb Run" (SBBR) and I put the game, including the source code / Godot project on github, as well as all the training scripts and tools used to build and train the AI. If you’re curious, you can train your own game master AI model from scratch, using SBBR and the labeling and training tools included.

Do note that SBBR is a tech demo and not at all a polished game meant for release. That said, if you play it you will definitely notice that the game gets harder as you progress – and if you’re struggling to stay alive, the game will actually back off a bit, at least for a moment.

I think this way of using tiny transformer models is both exciting and fun – I like the emergence and adaptability of this approach and can easily see it adapted to other parts of a game than just the difficulty.

Leave a Reply Cancel reply<br>Your email address will not be published. Required fields are marked *<br>Comment *<br>Name *

Email *

Website

Rasmus has 20+ years building products in tech and gaming. He is an AI-enthusiast who specializes in fine-tuning, building agentic workflows, and creating open-source tools that make AI accessible.

game network model building neural training

Related Articles