0
\$\begingroup\$

I have begun to create a procedurally generated RPG using Python, and I am having trouble with rendering the terrain.

What is happening:

I generate some terrain from some files, or create/save new terrain, and display it on the screen as a 2D B&W heightmap; black = low elevation, white = high elevation. The data are stored in .txt files, formatted as a series of arrays populated with floats.

When the game loads, the terrain is displayed properly, however, once you begin to move around, the terrain begins to act funky.

  1. If you move far from where you start, the terrain becomes gridlike.
  2. If you move horizontally, the terrain becomes somewhat vertically offset.
  3. If you move vertically, the terrain does not update.
  4. Even if you DO move horizontally before/after moving vertically, the terrain will not update vertically, only horizontally.

What I just said, but with images/video.

EDIT: I think the problem occurs in this block of code, but I can't be sure. This is taken from grid_generator.py. It returns a merged grid from the 3x3 area around a given point, where merge, loadGrid, and findAdjFnames are custom functions within grid_generator.py.

def threeBYthree(x,y):
    g = []
    for gy in range(-1,2):
        g.append([])
        for gx in range(-1,2):
            adj = findAdjFnames(x + gx,y + gy)
            print(adj)
            shouldSave = not os.path.isfile('./assets/grids/' + str(x + gx) + '_' + str(y + gy) + '.txt')
            print(shouldSave)
            g[gy + 1].append(loadGrid(str(x + gx) + '_' + str(x + gy) + '.txt', adjF = adj, andSave = shouldSave))

    #merge upper four
    g3a = merge(g[0][0],g[0][1],g[1][0],g[1][1])

    #merge bottom-left two
    g3b = []
    for y in range(len(g[2][0])):
        g3b.append(g[2][0][y] + g[2][1][y][1:])

    #merge top-right two
    g3c = []
    for y in range(len(g[0][2])+len(g[1][2])):
        if y < len(g[0][2]):
            g3c.append(g[0][2][y])
        elif y == len(g[0][2]):
            pass
        else:
            g3c.append(g[1][2][y-len(g[0][2])])

    #merge all
    g3d = merge(g3a,g3c,g3b,g[2][2])

    return g3d

The problem may occur somewhere else, but this bit is what I most recently worked on before the code stopped working.

Pastebin files:

Terrain functions: grid_generator.py

Main game loop: main_test.py

Terrain file for coordinate (0,0): 0_0.txt

NOTE: irrelevant, and some necessary, functions have been removed for privacy and security reasons. If you feel that this includes information required for a fix, please comment.

\$\endgroup\$
  • \$\begingroup\$ I think it would be better if you were to provide only the relevant parts of the code, and write them here. Links to code are usually discouraged because they may become dead in the future, rendering this question useless. The same could be say for images, since you can post images in your question, I don't see the reason to post them somewhere else. \$\endgroup\$ – TomTsagk Jun 5 '18 at 12:00
0
\$\begingroup\$

The solution has been found!

Within the threeByThree function, on a line I would have never guessed was incorrect:

g[gy + 1].append(loadGrid(str(x + gx) + '_' + str(x + gy) + '.txt', adjF = adj, andSave = shouldSave))

should have been:

g[gy + 1].append(loadGrid(str(x + gx) + '_' + str(y + gy) + '.txt', adjF = adj, andSave = shouldSave))

The problem with my function was that I was using an x variable where a y should have been.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.