Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHanna Han
4,105 PointsWhat 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
Treehouse Moderator 28,720 PointsKenneth 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
Hanna Han
4,105 PointsHanna Han
4,105 PointsThank you for your explanation. Now I understand!