如何识别特定数组坐标周围的索引值?

时间:2016-08-31 20:36:24

标签: python arrays python-2.7 multidimensional-array

如果我有一个代表世界的二维数组,那就像是:

. . . . . . . . . . . . . . .
. P . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . t . . . . . . . . .
. . . . . t . . . . . . . . .
. . . . . . t . . . . . . . .
. . . . . . . t . . . . . . .
. . . . . . . t t . . . . . .
. . . . . m . . . . . . . . .
. . . . . . m m . . . . . . .
. . . . . . . . m m . . . . .
. . . . . . . . . . m . . . .
. . . . . . . . . . m . . . .
. . . . . . . . . . . m . . .
. . . . . . . . . . . . . . .

m代表mountainst代表treesP代表Player

现在,假设我们有一个Player类来跟踪数组中的userpos或用户位置,当前为(1, 1)

class Player(object):
    def __init__(self, x, y, array):
        self.x = x
        self.y = y
        self.userpos = array[x][y] 

如果我在Player类中编写一个处理基本运动控制的方法来编辑数组内P值的位置,我该如何跟踪8 P值周围的瓷砖?例如,我们可以看到当前player userpos周围的所有切片都是.

我想限制玩家移动到m牌,因为他们是山脉。使用userpos变量很容易识别当前磁贴,因为P值仅添加到地图的字符串版本,而不是实际数组。然而,问题来自于试图将玩家移动到之前的位置。我希望能够将播放器返回到最近的empty.磁贴,但不知道如何确定播放器应该移动到的数组内部的索引。

是否有更简单的方法可以做到这一点,或者跟踪播放器周围的瓷砖是最简单的方法?

3 个答案:

答案 0 :(得分:0)

由于你得到了x和y以及原始数组,你应该可以使用以下代码迭代这些点:

tiles_to_move = []
for i in range(x-1, x+2):
    for j in range(y-1, y+2):
         if i != x or j != y:
             if array[i][j] != 'm': # depends on how you manage your array
                 tiles_to_move.append([i, j])

并使用tiles_to_move中存储的切片确定您的下一个可选动作。

答案 1 :(得分:0)

我认为一个更好的解决方案是有一个方法试图将玩家移动到所需的方向并让该方法在玩家类之外,因为玩家类不应该知道地图(代码未经测试):

def try_to_move_player(direction,map,player):
    desired_x = player.x
    desired_y = player.y

    if direction == "Left":
        desired_x -= 1
    elif direction == "Right":
        desired_x += 1
    #etc

    #Prevent player from moving off map left or right
    if desired_x < 0 or desired_x > len(map): #<-- map length might not be right
        return False
    #do the same for going above or below

    #Prevent the player from moving into mountains
    if map[desired_x][desired_y] == "m":
        print "Tried to walk into a mountain"
        return False

    #After all incorrect moving is taken care of
    player.x = desired_x
    player.y = desired_y
    return True

class Player(object):
    def __init__(self, x, y, array):
        self.x = x
        self.y = y
        self.userpos = array[x][y] 

#Try to move the player left
moved_successfully = try_to_move_player("Left",map,player)
if moved_successfully:
    print "The player was moved successfully w/ an updated position"
else:
    print "The player was not moved correctly"

答案 2 :(得分:0)

您可以向Player类添加一个方法,该方法将垂直和/或水平移动给定数量的步骤。它可以通过计算Player对象的新坐标并检查该索引处的矩阵以确保它不是一座山来实现这一目标(我还假设你不会这样做#39} ; t希望你的Player对象走进树林。

class Player(object):

    def __init__(self, x, y, array):
        self.x = x
        self.y = y
        self.userpos = array[x][y]

        # Make a list of the different items that can block the player
        self.obstacles = ['m', 't']

    def move(self xOffset, yOffset, array):
        newX = self.x + xOffset
        newY = self.y + yOffset

        # The following two conditionals make sure that the position
        # that the player is trying to move to is within the actual
        # bounds of the array
        if 0 <= newX < len(array):
            if 0 <= newY < len(array[newX]):

                # Check if the position that the player is trying to
                # move to is not filled by an obstacle
                if array[newX][newY] not in self.obstacles:
                    self.x = newX
                    self.y = newY