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
Ben Robinson
7,380 PointsDungeon Game bug
There's a problem I'm having with my dungeon-game.py code. I get the concepts and everything, but for some reason I can't move the player. Here's my code:
import random
CELLS = [(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2,)]
# Grid spaces
def get_locations(): # Starting points of the necessary entities
monster = random.choice(CELLS)
door = random.choice(CELLS)
player = random.choice(CELLS)
if monster == door or player == monster or door == player: # Restarts the starting points if any are the same
return get_locations()
return monster, door, player
def move_player(player, move): # move the player, duh.
# player = (x, y)
x, y = player
if move == 'LEFT':
x -= 1
if move == 'RIGHT':
x += 1
if move == 'UP':
y += 1
if move == 'DOWN':
y -= 1
return x, y
# Get player's current location
def get_moves(player): # display the moves the player can take
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
# player = (x, y)
if player[1] == 0:
moves.remove('LEFT')
elif player[1] == 2:
moves.remove('RIGHT')
elif player[0] == 2:
moves.remove('DOWN')
elif player[0] == 0:
moves.remove('UP')
return moves #like jagger lel
def draw_map(player): # Draws the map
print(' _ _ _')
tile = '|{}'
for idx, cell in enumerate(CELLS):
if idx in [0, 1, 3, 4, 6, 7]:
if cell == player:
print(tile.format('X'), end='')
else:
print(tile.format('_'), end='')
else:
if cell == player:
print(tile.format('X|'))
else:
print(tile.format('_|'))
monster, door, player = get_locations()
print("WELCOME to the dungeon!! :3")
while True:
moves = get_moves(player)
print("You're currently in room {}".format(player))
draw_map(player)
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, moves)
continue
else:
print("** You have been rejected by a wall. Go a different route, dummy. **")
continue
if player == door:
print('You made it out alive.... For now....')
break
elif player == monster:
print('You have made it into the belly of the mysterious monster. Oh, and you died.')
break
Oh, and don't mind the odd comments I put in there... I'm a strange person
1 Answer
Kenneth Love
Treehouse Guest TeacherWow, that took me way too long to find. :) You're passing moves to your move_player function instead of move.
Ben Robinson
7,380 PointsBen Robinson
7,380 PointsOHHHH okay! Thank you so much! :D