vendredi 14 août 2015

Random world generation in 2D array

I am trying to create a city grid generator that simply generates a "pyramid" at a random point (the higher the number, the more urban density). It was supposed to work by generating a point, then checking the surrounding area and filling it with single unit smaller values, however it only works about a third of the time.

Here's my generation method:

def radiate(city, x1, y1, x2, y2):
    x = False
    y = False
    if city[y2][x2] < city[y1][x1]:
        if x2 == x1 - 1 or x2 == x1+1 or x2 == x1:
            x = True
        if y2 == y1 - 1 or y2 == y1+1 or y2 == y1:
            y = True

        if x == True and y == True:
            return True
    return False

def gen_city(x, y, z):
    SIZE_X = x
    SIZE_Y = y
    SIZE_Z = z
    city = []
    for i in range(SIZE_Y):
        city.append([])
        for j in range(SIZE_X):
            city[i].append(1)

    #High Density Origin gen
    HDO = (random.randint(5, SIZE_X-5), random.randint(5, SIZE_Y - 5), SIZE_Z)
    city[HDO[1]][HDO[0]] = HDO[2]

    #Radiate
    radlist = [(HDO[0], HDO[1], HDO[2])]
    radnum = HDO[2]
    for k in radlist:
        radnum = k[2] - 1
        if radnum == 1: break
        for i in range(SIZE_Y):
            for j in range(SIZE_X):
                if radiate(city, k[0], k[1], j, i):
                    city[i][j] = radnum
                    radlist.append((i, j, city[i][j]))
#                    for c in city:
#                        print c
#                    raw_input()

    return city




Aucun commentaire:

Enregistrer un commentaire