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):
        for gx in range(-1,2):
            adj = findAdjFnames(x + gx,y + gy)
            shouldSave = not os.path.isfile('./assets/grids/' + str(x + gx) + '_' + str(y + gy) + '.txt')
            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]):
        elif 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.

  • \$\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\$ Jun 5, 2018 at 12:00

1 Answer 1


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.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .