SONIC-GAN

TOAD-GAN: Procedural Generator of 2d Platform Game Levels

Table of Contents

Introduction

This project focuses on leveraging Generative Adversarial Networks (GANs) to advance Procedural Content Generation (PCG) for 2D platform games. Specifically, it examines the application of the TOAD-GAN model, a deep learning-based approach designed to automate the generation of game levels. By utilizing TOAD-GAN, the project aims to provide a greater variety of levels and improve the replayability of platform games.

Our task is to adapt the TOAD-GAN model slightly to create a predefined workflow that will help other users apply it for level generation. For this purpose, we have chosen Sonic the Hedgehog as the test game, as it is more complex than Super Mario Bros or Mario Kart in terms of game difficulty and will present a more challenging task for the model.

Context

Creating 2D game levels for platformers like Sonic the Hedgehog is often a tedious and repetitive task for game developers. Levels must be designed with the right balance of difficulty, layout, and visual aesthetics. While developers can create these levels manually, the process can be time-consuming and prone to limitations. With the growing interest in procedural content generation, AI is now offering an innovative way to automate this process.

State of the art

Procedural Content Generation (PCG) has become an integral part of modern game development, enabling the automated creation of game levels, assets, and mechanics. Traditionally, rule-based approaches have dominated PCG, where developers manually define algorithms to generate content. While effective, these methods often lack the flexibility and creativity required to produce diverse and unpredictable content. To address these limitations, advancements in machine learning and deep learning have paved the way for more sophisticated PCG techniques.

Deep Learning in PCG

The integration of deep learning into PCG has transformed the landscape of content generation. Neural networks, particularly Convolutional Neural Networks (CNNs) and Recurrent Neural Networks (RNNs), have been widely used to analyze and generate game content. These models excel at learning patterns and structures from data, enabling them to mimic human-designed levels. However, their reliance on predefined datasets and supervised learning has highlighted a need for more generative and unsupervised methods.

Generative Adversarial Networks (GANs)

Introduced by Ian Goodfellow et al. in 2014, GANs represent a breakthrough in generative modeling. GANs consist of two neural networks, a generator and a discriminator, that compete with each other to produce realistic outputs. This architecture has been widely adopted in PCG to generate high-quality game levels. GANs are particularly effective in creating novel and diverse content while maintaining structural coherence. Applications of GANs in PCG include generating levels for games like Super Mario Bros., Doom, and The Legend of Zelda. For instance, MarioGAN has been used to generate levels for Super Mario Bros., showcasing the potential of GANs in replicating and expanding upon classic game designs. Despite their success, traditional GAN models often struggle with issues such as mode collapse, where the generator produces limited variations of content, limiting the diversity of generated levels.

image

Fig 1: Example of levels generated by TOAD-GAN model

TOAD-GAN

TOAD-GAN (Topology-Aware GAN for 2D Games) builds upon the foundation of traditional GANs by introducing topology-awareness into the generation process. Developed specifically for 2D platformers, TOAD-GAN addresses the need for maintaining structural integrity in generated levels. Unlike other GAN models, TOAD-GAN ensures that the levels are not only visually appealing but also functional and playable, a critical aspect of game design. TOAD-GAN uses a patch-based approach to generate levels, allowing it to create large, continuous game worlds while preserving local structures. This makes it particularly suitable for games with complex topologies, such as Sonic and Castlevania. By training on datasets of existing game levels, TOAD-GAN learns to replicate the design principles of the original games while introducing novel variations.

Contribution

This project takes Toad GAN and adapts it specifically for Sonic the Hedgehog 2D platform game levels. We have modified the GAN architecture to generate levels with the same style and difficulty as the original Sonic levels. Additionally, we’ve incorporated reinforcement learning to evaluate and improve the generated levels based on player feedback, enabling the model to fine-tune itself over time.

Resources

Datasets:

Main Libraries:

Hardware:

Techniques and Algorithms:

Team:

Work Done

Research and Preparation

To lay the groundwork for the project, we undertook an exhaustive process of researching and curating sprite resources and references relevant to Sonic the Hedgehog. Given Sonic’s unique and visually rich aesthetic, it was essential to identify sprites that accurately captured the game’s distinct level elements, including platforms, hazards, and background assets.

Sprites were sourced from publicly available repositories and fan communities dedicated to Sonic games like spriter-resource and vgmaps. These resources included tilesets, character animations, and environmental features. To adhere to project deadlines and simplify the physical logic of levels, we opted to exclude complex structures like semi-curved ground, loops, and dynamic motions for rings and enemies. Instead, static sprites were used for these elements, as their absence minimally impacts gameplay or difficulty during testing.

Below are examples of sprites used in this project:

image

Fig 2: Sonic sprite sheet

image

Fig 3 : Background sprite sheet

image

Fig 4 : Ring sprite sheet

image

Fig 5: Goal post sprite

image

Fig 6: Enemy sprite

image

Fig 7: Trap sprite

Many of the collected sprites required modification to fit the specific requirements of the TOAD-GAN model.

This process was critical to ensuring the compatibility of the sprites with the training and generation pipeline.

Dataset Collection and Preparation

The dataset forms the backbone of any machine learning project.

For this project we used an AI generated ASCII Level of the sonic level. An explanation of each token is provided in our project tokens.py:

image

Fig 8: Input level 1-2 for sonic (AI generated)

- Simplification and Refinement:

Logical rules were implemented to maintain game physics and level integrity, such as:

  1. Spikes (traps) must be placed on a flat ground block.
  2. Decorative elements (flowers, trees) are ground dependent.
  3. Enemies can be in the air or on the ground but not in water.
  4. Goal posts must always sit atop ground blocks.

Since the GAN-based approach doesn’t require large dataset to be functional, we haven’t spent that much time designing the level, knowing that we could come back to this step after the adaptation of the TOAD GAN will be made.

The levels we’ve manually made are of course more complex than Mario levels, but don’t carry the whole complexity of Sonic’s game. It’s more like a level snippet.

Model Adaptation

To apply TOAD-GAN effectively to Sonic, the model’s architecture and logic were tailored to match Sonic’s distinct level design features. Thus, it’s important to understand how the neural network behind the TOAD-GAN is constructed, how does the TOAD-GAN handle input data, how the graphical rendering of the level is designed and the overall process of the TOAD-GAN to generate those levels.

As already mentioned earlier, the GAN is a combination of two models: the Generator and the Discriminator. The generator as its noun refers to, will generate the output of based on input data. The discriminator is here to penalize the generator when he is getting out of rails.

Both are constructed using convolutional blocks with a predefined structure. They simply add as many modules as mentioned in the command line when we are calling the model (in this case a module is a convolutional block). Then, to directly impact the model architecture, we can modify that base configuration of the convolutional block.

For the TOAD-GAN model, the base convolutional block is the association of a Conv2D layer, with a BatchNorm2D and a LeakyReLu layer at the end. And we can add more layers, from different types based on our initial problem. That architecture allows us to easily modify the model and adjust it to fit our needs.

The TOAD-GAN model takes as input ASCII encoded level of the game we want to generate levels from. That level will just be converted into tensor then group those tensors based on the TOKEN_GROUPS dictionary defined in the tokens.py file. Now we pass the result into our models and the results which are group token tensor will be converted back into tensor and finally into ascii. Finally, the graphical engine takes the ascii level and by using the sprite data we have, it will generate the image corresponding to the level.

Now, we know how TOAD-GAN works, and how to adapt TOAD-GAN to our game, SONIC. For that, we focused on the graphic engine, and the model hyper parameters.

The graphic engine will generate the image of the level and apply the logical physics of the game, and the model hyper parameters will help the model to fit our needs by directly impacting the way it’s learning.

• Topology Awareness – Logic Physics implementation:

The level_image_gen was adjusted to emphasize logic, verticality, and interconnected paths. Simplified sprites and logical rules ensured that gameplay elements aligned with Sonic’s physics and aesthetic requirements.

• Token System Integration:

The newly curated and modified sprites were integrated into the model’s pipeline, replacing placeholder assets with elements true to Sonic’s universe. This allowed the model to learn the specific style and design principles of the original levels.

To do that we used a hierarchy like the one we could have in the Mario game:

image

Fig 9: Sonic Token Hierarchy

All these tokens’ groups are defined in token.py. It’ll seem obvious but to explain key groups, they include GROUND_TOKENS for standard ground blocks, SPECIAL_GROUND_TOKENS for unique terrain features, SKY_TOKENS for empty spaces, ENEMY_TOKENS for enemy types, SPECIAL_TOKENS for gameplay-critical items like rings, Sonic starting and ending position and item box, and EXTRA_SPECIAL_TOKENS for decorative elements like tree and flowers.

• Hyperparameter Adjustments:

We iteratively tuned TOAD-GAN’s hyperparameters to improve output quality. Key modifications included:

After implementing those adjustments, we’ve been able to create using the GAN levels looking like this:

image

And we previously had levels looking like:

image

image

image

Level Generation

Once the dataset and model were prepared, we began the level generation phase. This involved running iterative experiments to produce a diverse set of level designs while monitoring and fine-tuning the model’s performance.

Model parameters

During the iterative part, we tried to manipulate the hyperparameters of the model as the TOAD-GAN is not specific to one game, a bad output should be related to the hyperparameters (at least that’s the conclusion we came up with) and it worked well as it improved our result over iterations. An explanation of all hyperparameters can be found in our project README

Evaluation by Reinforcement Learning

To further refine the generated levels, we implemented Reinforcement Learning (RL) techniques to evaluate the levels based on player feedback. The idea behind this is to let the AI “play” through the generated levels and reward it for creating levels that provide a balanced challenge. If a generated level is too easy or too hard, the model can adjust its parameters to improve the level’s quality.

We get more into details here.

Conclusion

This project marks the first step in automating the process of 2D platform game level creation using AI. The success of Toad GAN in generating levels for Sonic shows that we are on the right path. With continuous improvements, this method could be expanded to generate levels for other games and genres, enhancing the game development process in the future.

Stay tuned for more updates and feel free to contribute to this exciting journey!


Follow the project on GitHub: Project Repository

Feel free to check out the code, contribute to improvements, and experiment with generating new levels for Sonic!

References


Thanks for reading, and happy level designing!