0

I am following the tutorial here on how to develop a rogue-like game using Python and I ran across this method call

if self.game_map.tiles["walkable"][self.player.x + action.dx, self.player.y + action.dy]:

I don't understand what is happening with the two sets of brackets at the end of the call. Is this an if statement format, or a specific way to call a method? The class containing this method is...

class GameMap:
def __init__(self, width: int, height: int):
    self.width, self.height = width, height
    self.tiles = np.full((width, height), fill_value=tile_types.floor, order="F")

    self.tiles[30:33, 22] = tile_types.wall
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • The method being called is `__getitem__` -- the same thing called during a regular dictionary lookup. When you pass two comma-separated arguments, the thing that evaluates to is a tuple, so a tuple consisting of those arguments is what's looked up as a key. – Charles Duffy Jan 09 '22 at 01:47
  • In this case, you're calling `__getitem__` more than once -- once to look up `walkable` content, and then a second time to look up the destination location _within_ the `walkable` structure, presumably to check if _those specific coordinates_ are walkable. – Charles Duffy Jan 09 '22 at 01:49
  • BTW, the _point_ of writing a title for the question is so someone can know what the question is roughly about before they read it. When you use the word "this" in the title to refer to question contents, _that defeats the whole point of a title_ by forcing the reader to click through and read before they understand what your question is about. – Charles Duffy Jan 09 '22 at 01:51

0 Answers0