Python Python Collections (2016, retired 2019) Dungeon Game Movement

What does "player = get_moves(player, move)" in the code?

Hi, I wonder what "player = get_moves(player, move)" does in while loop? I don't understand this logic and need more clear explanation for keeping forward in coding. The variable "players" has (x, y) as we all know. I assume that this code "player = get_moves(player, move)" makes us get to the function get_moves() but is it this way to go to the function where we want to go? Please help me with this.

while True: clear_screen() valid_moves = get_moves(player) print("Welcome to he dungeon!") print("You are currently in room {}".format(player)) print("You can move {}".format(", ".join(valid_moves))) print("Enter Q to quit")

  move = input("> ").upper()
  if move == "Q":
     break
  if move in valid_moves:
     player = get_moves(player, move)
  else:
     print("\n Walls are hard! Take other ways instead! \n")
      continue

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 23,580 Points

Kenneth is showing how a game loop can be written using two important methods/functions, 'get_moves()' and 'move_player()'

valid_moves = get_moves(player) => returns all possible (valid) moves the player has at its current (x,y) location tuple.

new_player_location = move_player(current_player_location, move) => returns a NEW player location (x,y) after applying the supplied move to the current_player_location.

In your code above there was a slight transcription error, I put a comment on it

while True:
    clear_screen()
    valid_moves = get_moves(player)
    print("Welcome to he dungeon!")
    print("You are currently in room {}".format(player))
    print("You can move {}".format(", ".join(valid_moves)))
    print("Enter Q to quit")

    move = input("> ").upper()
    if move == "Q":
        break
    if move in valid_moves:
        # player = get_moves(player, move) # this needs to call move_player instead
        player = move_player(player, move) # we update the player location with the user selected move
    else:
        print("\n Walls are hard! Take other ways instead! \n")
        continue

Thank you for your explanation. Now I understand!