The error looks like this:
Traceback (most recent call last):
File "main.py", line 136, in <module>
curses.wrapper(main)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/curses/__init__.py", line 105, in wrapper
return func(stdscr, *args, **kwds)
File "main.py", line 130, in main
mainWindow.addstr(0, 0, buildView(windowBorder))
File "main.py", line 69, in buildView
newRow += worldRows[charPos[1]][charPos[0]]
IndexError: string index out of range
This program is supposed to take the player position, and print (using curses) the map and the player movement.
If the world DSL looks like this:
world = """
............X.....................................
.......X.......X.......X.........X...........X....
....X......X...X...X...........X.......C.X...X....
.X......X....b...X...X..X...X..X.....X........X...
...X.....................e.............X....X.....
........h..X......X...............................
..............X......X.........X........X...X.....
..X...X.....X....X......X.........................
............X...X......X.......X....d......X......
....X...X.......X...X.....X...........X......X....
.....X....X.X.....X.....X.....X...X...............
..X.X......X.X..a.......................X........
...X...X.........X..X.....g.................X.....
..X.X...X..X....X.........X........f..........X..
..................X....................X..........
"""
and the player's x, y position at start is a tuple (17, 6). The chunk of code constructing the viewport is as follows:
def buildView(border):
playerView = """"""
worldRows = [row for row in world.splitlines() if row]
counterY = -1
rows = border.splitlines()
for i, row in enumerate(rows):
newRow = ""
if i == 0 or i == len(rows):
pass
elif i == 1 or i == len(rows) - 1:
newRow = row
newRow += "\n"
else:
counterX = -6
for j, col in enumerate([char for char in row]):
if col != '#':
newRow += col
else:
charPos = (playerPos[0] + counterX,
playerPos[1] + counterY)
if charPos[0] < 0 or charPos[1] < 0 or charPos[
0] > 49 or charPos[1] > 14:
newRow += " "
else:
newRow += worldRows[charPos[1]][charPos[0]]
counterX += 1
newRow += "\n"
playerView += newRow
counterY += 1
temp1 = playerView.splitlines()
temp2 = [s for s in temp1[2]]
temp2[7] = "@"
newtemp = ""
for i in temp2:
newtemp += i
temp2 = newtemp
temp1[2] = temp2
thingy = """"""
for i in temp1:
thingy += i
thingy += "\n"
playerView = thingy
return playerView
When the program starts, it looks like this:
|=============|
|.X....X......|
|.X...X@.....X|
|.....X...X...|
|=============|
when the player types either w, a, s, or d, the player's xy is updated according to the direction they are moving, but after moving twenty six spaces to the right (player's x coord is += 26), I get the above error. Does anyone see something Im missing?
what I expect to see is this:
|=============|
|......X......|
|.X....@.X....|
|.............|
|=============|
and the player's xy coords should be (9, 43). The max xy coord of the world is (49, 14).