1

I'm trying to improve my python skills by making a rouge like game with pygame. I started by trying to make a simple level generator but I am running in to an issue where more values in my map matrix are bring filled in than should be.

The following code is my attempt at adding all the adjacent elements to a list to later randomly select one to be added to the map.

def generate(self):
    print(f'generating a new map (difficulty: {self.diff})')

    next_room = (random.randint(0, self.size-1), random.randint(0, self.size-1))
    print(next_room)

    room_options = []

    room_options.append(next_room)

    # left
    if next_room[0] != 0:
        room_options.append( (next_room[0]-1, next_room[1]) )
    # right
    if next_room[0] < self.size-1:
        room_options.append( (next_room[0]+1, next_room[1]) )
    # top
    if next_room[1] != 0:
        room_options.append( (next_room[0], next_room[1]-1) )
    # botttom
    if next_room[1] < self.size-1:
        room_options.append( (next_room[0], next_room[1]+1) )
    # top left
    if next_room[0] != 0 and next_room[1] != 0:
        room_options.append( (next_room[0]-1, next_room[1]-1) )
    # top right
    if next_room[0] != 0 and next_room[1] != self.size-1:
        room_options.append( (next_room[0]-1, next_room[1]+1) )
    # bottom left
    if next_room[0] != self.size-1 and next_room[1] != 0:
        room_options.append( (next_room[0]+1, next_room[1]-1) )
    # bottom right
    if next_room[0] != self.size-1 and next_room[1] != self.size-1:
        room_options.append( (next_room[0]+1, next_room[1]+1) )

    for coords in room_options:
        print(coords)
        x = coords[0]
        y = coords[1]
        self.map[x][y] = "O"

When I print out all the coordinates I get values that make sense, for example

(7, 2) (6, 2) (8, 2) (7, 1) (7, 3) (6, 1) (6, 3) (8, 1) (8, 3)

But when I try to change the default values at this position from '#' to 'O' to visualize it using numpy.matrix(self.map) I get something that looks like this

[['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']
 ['#' 'O' 'O' 'O' '#' '#' '#' '#' '#' '#']]

Update: I've been trying to diagnose this issue but still haven't figure out what exactly is wrong. I do however have a slightly better idea of what is going on. every time it writes something to a position it overwrites the entire column. I just have absolutely no idea why. Here is more debugging output to show this.

(6, 1)[0]
[['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(6, 1)[0]' '#' '#' '#' '#' '#' '#' '#' '#']]
(5, 1)[1]
[['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(5, 1)[1]' '#' '#' '#' '#' '#' '#' '#' '#']]
(7, 1)[2]
[['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['#' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']]
(6, 0)[3]
[['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '#' '#' '#' '#' '#' '#' '#' '#']]
(6, 2)[4]
[['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(6, 0)[3]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']]
(5, 0)[5]
[['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(6, 2)[4]' '#' '#' '#' '#' '#' '#' '#']]
(5, 2)[6]
[['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(5, 0)[5]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']]
(7, 0)[7]
[['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(5, 2)[6]' '#' '#' '#' '#' '#' '#' '#']]
(7, 2)[8]
[['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']
 ['(7, 0)[7]' '(7, 1)[2]' '(7, 2)[8]' '#' '#' '#' '#' '#' '#' '#']]
Zelkins
  • 723
  • 1
  • 5
  • 22
  • 1
    All the board or map is initialized with ``#``? And the problem is that, given the coordinates, let's say ``(7,1)`` you are uncannily getting the entiry column replaced instead of one cell? Is that the actual issue? – Tomás Denis Reyes Sánchez Mar 29 '20 at 19:07

1 Answers1

1

The error was the constructor of the Level class, not its' generate() method.

I originally initialized self.map with self.map = [["#"] * size] * size which made each row a copy of itself. I should have used self.map = [["#"] * size for _ in range(size)]

Thanks to this question I just found for the solution.

Zelkins
  • 723
  • 1
  • 5
  • 22