Introduction

There are many algorithms for generating game levels procedurally. Some are more automated than others.

In this series, we’re looking at a random walker algorithm that generates a valid path between random entrance and exit locations. The ideas presented here draw great inspiration from Spelunky’s level generator.

This algorithm is a hybrid between automation and hand-crafted levels as it uses randomly selected rooms or chunks to fill out entire maps. You have to design the base rooms by hand, and the algorithm can place and randomize them. Doing so gives you control over the look and difficulty of each section, which is an advantage over many approaches to level generation. Managing difficulty is often a challenge when dealing with procedural content generation (PCG).

Tutorial objectives

You are going to learn to:

Here is how the algorithm works in short. You give it a grid_size and pre-defined room assets of the same size (room_size). Then, the algorithm:

  1. Generates a valid (unobstructed) path starting at the top of the map and ending at the bottom.
  2. Places the outer walls based on grid_size so that the player can’t go out of bounds.
  3. Takes the valid path and places rooms based on the given valid path parameters.
  4. Fills the remaining grid positions with random room assets. Some of these rooms can lead to dead ends, which is fine as we already generated a valid path for the player.

We’ll go over these steps in detail in the following lessons, but first, let’s prepare the room assets.

Notes on Procedural Generation demos project

This chapter associated files and folders, from Procedural Generation demos project, are:

Note that the demo implements a basic character, enemies and other minor improvements over the tutorial from this chapter. You are encouraged to check out the differences between the code in this tutorial and the demo.

References

This tutorial is based on work from: