lundi 4 janvier 2016

Generating random squares on a matrix (Python)

Recently in python, I've been working on a game with procedural generation. For now, I've been developing a function that would generate random levels that consist of squares of 3's (which represent an empty space, as opposed to 5's) on the matrix. Here's what I have:

def generateLevel():
    levellist = [5]
    while len(levellist) < 40:
            levellist.append(5)
    levelmatrix = [levellist]
    while len(levelmatrix) < 60:
            levelmatrix.append(levellist)
    if randint(1,5) == 1:
            roomsNo = randint(1,5)
    else:
            roomsNo = randint(3,5)
            #x start, y start x len y len
    rooms = [[randint(1,42),randint(3,32),randint(3,9)]]
    temp = 0
    while temp < roomsNo:
            rooms.append([randint(1,42),randint(3,32),randint(3,9)])
            temp += 1
    print(rooms)
    temp = 0
    width = 0
    height = 0
    roomscoords = [[]]
    while temp < len(rooms):
            print("new room")
            width = 0
            while width < 60:
                    height = 0
                    while height < 40:
                            if width >= rooms[temp][0] and width < (rooms[temp][0] + rooms[temp][2]) and height >= rooms[temp][1] and height < (rooms[temp][1] + rooms[temp][2]):
                                    print("clear space",temp,width,height) #figure 1
                                    levelmatrix[width][height] = 3
                            #print(temp,width,height)
                            height += 1
                    width += 1
            temp += 1
    return levelmatrix

When my program displays the matrix, It come up with these randomly generated bars across the x-axis, even though the results from the printout on figure 1 would suggest that it would generate my squares. Any help would be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire