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 trial

Python Python Collections (Retired) Dungeon Game Building the Game: Part 1

Niall Maher
Niall Maher
16,985 Points

What's wrong? It runs but player won't move :/

Running the first stage version of the app and I am not receiving errors but seems to be stuck in the same coordinates... Here is my code

import random

CELLS = [(0, 0), (0, 1), (0, 2),
         (1, 0), (1, 1), (1, 2),
         (2, 0), (2, 1), (2, 2)]


def get_locations():
  monster = random.choice(CELLS)
  door = random.choice(CELLS)
  start = random.choice(CELLS)

  if monster == door or monster == start or door == start:
    return get_locations()
  return monster, door, start

def move_player(player, move):
  x,y = player

  if move =="LEFT":
    y -= 1
  elif move =="RIGHT":
    y += 1
  elif move == "UP":
    x -= 1
  elif move == "DOWN":
    x += 1  

  return player


def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  # player is = (x,y)

  if player[1] == 0:
    moves.remove("LEFT")
  if player[1] == 2:
    moves.remove("RIGHT")
  if player[0] == 0:
    moves.remove("UP")  
  if player[0] == 2:
    moves.remove("DOWN") 

  return moves


monster, door, player = get_locations()

while True:
  moves = get_moves(player)

  print("Welcome to the dungeon!")
  print("You're currently in room {}".format(player))  # fill in with player position
  print("You can move {}".format(moves))  # fill in with available moves
  print("Enter QUIT to quit")

  move = input("> ")
  move = move.upper()

  if move == 'QUIT':
    break


  if move in moves:
    player = move_player(player, move)
  else:
    print("That's a wall...")
    continue

  if player == door:
    print("You escaped!!!")
    break
  elif player == monster:
    print("You made a tasty treat for the dungeon monster")
    break

3 Answers

in your move_player function, you used: return player, but you should have used return x, y

u used player variable many times but what does it contain ? is it array of numbers or what ?

yes , Mr.Michael Lefkowitz said the right answer I tried to write it with soe changes in the move_player function only ,it is running def move_player(player, move): x,y = player

if move =="LEFT": y -= 1 elif move =="RIGHT": y += 1 elif move == "UP": x -= 1 elif move == "DOWN": x += 1
player=(x,y) # I changed this only ..good program and programmer return player