Owen Penn
Owen Penn

Reputation: 163

Using 2D Arrays to create a level

I have a 9x9 2D Array. I want to create a very Binding of Isaac-esque floor system. I want to use the 2D array to make a map of rooms in the 9x9 array. I've looked all over the internet but I cannot find an algorithm, or some kind of helpful code to help me do this. The problems are: Using the 2D Array to make a floor of rooms that connect - lets say something like this, where the 1's are rooms and 0's are nothing...

[['0','0','0','0','0','0','0','0','0'],
 ['0','0','0','0','1','0','0','0','0'],
 ['0','0','1','1','1','1','1','0','0'],
 ['0','0','1','0','1','0','0','1','0'],
 ['0','2','1','1','1','1','1','1','0'],
 ['0','0','0','0','1','0','1','0','0'],
 ['0','0','0','0','1','0','1','1','0'],
 ['0','0','0','0','1','0','0','0','0'],
 ['0','0','0','0','0','0','0','0','0']]

Then for each room, the algorithm would have to decide whether the room had 4 doors - 4 rooms connecting to it - or lets say 2 doors, one above and one below, so that I don't get any rooms that, say, have 4 doors but only one room connecting to it.

And lastly, the starting room would have to be in the center, and one of the outer rooms would have to be selected to be the end room, which would be different style of room (shown as a 2 on the above code). If possible this room should only be connected to one other room to be harder to find.

Upvotes: 0

Views: 717

Answers (1)

JonKleehammer
JonKleehammer

Reputation: 51

I think what has made this hard is that python isn't used very much for games. That being said, there are still lots of guides you can find if your searches have the right phrasing. At the very least in other languages you'll find a lot of information by looking up "procedural dungeon generation python" even though python was in the search.

Question 1) Using the 2D array

I think to really implement this you'll need to create a class for your rooms to store data. Figuring out how many doors a room has would be pretty easy, but you'll need to save where that information is somewhere if you want to use it later on down the line.

Starting simply I'm going to assume all rooms are 1x1 in size,

To determine the doors attached to this room using your 2d array you would want to iterate through each room with a nested for loop and check their adjacent neighbors.

Really simple pseudo-code

# your 2d array is called map in this example
room_array = []
for i in range(len(map)):
    new_room_row = []

    for j in range(len(map[i])):
        new_room = Room()

        # when our room is initialized all doors are false
        # checking each direction if it's not nothing
        if map[i-1][j] != 0
            new_room.up_door = true
        if map[i+1][j] != 0
            new_room.down_door = true
        if map[i][j-1] != 0
            new_room.left_door = true
        if map[i][j+1] != 0
            new_room.right_door = true

      new_room_row.push(new_room)

  room_array.push(new_room_row)

So we start by creating a "room_array" which will be the next step in developing the dungeon. The room array is a 2d map that will be the same size as the map array, just with more detail. We iterate through the map array and create a new room that represents each element of your map array. Along the way we're checking the adjacent elements to see if we need a door to open up in that direction for that room. Once you have all the rooms you'll just need to assign them sprite/art/walls etc based on where the doors are. (Collision might be kind of hard to do in pygame, I don't have much experience using pygame but I've done a lot of work in Unity)

Question 2) Room Restrictions and Map Generation

Ideally, your original 2D map should ensure that there is a room with only 1 opening if that's what you want in the final product. Start with a good foundation then add on to it. I don't know how you generated the example 2D array you had, but there are lots of ways of doing similar things.

This really comes down to your algorithm. There are a lot of ways to do this, but I would simply ensure that when generating the original 2D array "map" there is at least 1 room that has 1 neighbor. Example Ways of doing this:

  • At the beginning make a 2 somewhere on the map and then don't allow any neighboring rooms except from a random direction. Then connect the start to the end
  • At the end of generation, check for rooms with only 1 opening by iterating through the array and set one of them to be the end room. If no room was generated with only 1 opening try to place a room that would only have 1 opening.

When making a game there are a million different ways to do something, I hope I gave you some ideas that help you move forward.

Upvotes: 1

Related Questions