【问题标题】:How to identify the values of indexes around a specific array coordinate?如何识别特定数组坐标周围的索引值?
【发布时间】:2016-08-31 20:36:23
【问题描述】:

如果我有一个代表世界的二维数组,它看起来像:

. . . . . . . . . . . . . . .
. 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 个图块987654334@ 值?例如,我们可以看到当前playeruserpos 周围的所有图块都是. 的。

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

有没有更简单的方法来做到这一点,或者跟踪玩家周围的瓷砖是最简单的方法?

【问题讨论】:

    标签: python arrays python-2.7 multidimensional-array


    【解决方案1】:

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

    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】:

      您可以向Player 类添加一个方法,该方法将垂直和/或水平移动给定数量的步骤。它可以通过计算 Player 对象的新坐标并检查该索引处的矩阵以确保它不是一座山来实现这一点(我还假设您不希望您的 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
      

      【讨论】:

        【解决方案3】:

        既然你得到了 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 中的图块来确定您的下一个可选动作。

        【讨论】:

        • 这里应该是or j != y
        • ...对不起,再想一想,可能是and j != y。您只想避免 x,y 位置。
        • 使用and 将避开所有i = x 或j = y 的点,即5 个点。请记住(~i==x or ~j==y) == (~(i==x and j==y))
        猜你喜欢
        • 1970-01-01
        • 2023-01-06
        • 2021-09-29
        • 1970-01-01
        • 2014-03-27
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 2017-12-03
        相关资源
        最近更新 更多