If I had a 2-D array, representing a world, that looked something like:
. . . . . . . . . . . . . . .
. P . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . t . . . . . . . . .
. . . . . t . . . . . . . . .
. . . . . . t . . . . . . . .
. . . . . . . t . . . . . . .
. . . . . . . t t . . . . . .
. . . . . m . . . . . . . . .
. . . . . . m m . . . . . . .
. . . . . . . . m m . . . . .
. . . . . . . . . . m . . . .
. . . . . . . . . . m . . . .
. . . . . . . . . . . m . . .
. . . . . . . . . . . . . . .
m stands for mountains, t for trees and P for Player.
Now, let's say we have a Player class that tracks the userpos or user position in an array, which is currently (1, 1):
class Player(object):
def __init__(self, x, y, array):
self.x = x
self.y = y
self.userpos = array[x][y]
If I were to write a method inside of the Player class that deals with basic movement controls to edit the location of the P value inside of the array, how could I go about tracking the 8 tiles around the P value? For example, we can see all of the tiles around the current player userpos is all .'s.
I want to restrict the player from moving onto m tiles, being that they are mountains. It is easy to identify the current tile with the userpos variable, since the P value is only added to a string version of the map, and not the actual array. However, the issue comes from trying to move the player to a previous position. I want to be able to return the player to the nearest empty or . tile, but do not know how to decide what index inside of the array that the player should be moved to.
Is there an easier way of doing this, or would tracking the tiles around the player be the easiest way?