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
Christopher Wommack
3,983 PointsDungeon_game.py
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 x, y
def get_moves(player):
moves = ('LEFT', 'RIGHT', 'UP', 'DOWN')
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
def draw_map(player):
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!!")
while True:
moves = get_moves(player)
print("You are currently in room{}".format(player))
print("You can move{}".format(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("** Walls are hard stop walking into them!**")
continue
if player == door:
print("You escaped")
break
elif player == monster:
print("You were eaten by the grue")
break
Welcome to The Dungeon!! Traceback (most recent call last): File "dungeon_game.py", line 67, in <module> moves = get_moves(player) File "dungeon_game.py", line 37, in get_moves moves.remove('LEFT') AttributeError: 'tuple' object has no attribute 'remove' The errors I am getting
4 Answers
john larson
16,594 PointsIt took some digging but I found it
moves = ('LEFT', 'RIGHT', 'UP', 'DOWN')
# should be
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
apparently you had that as a tuple and not a list, and Python didn't like that.
Christopher Wommack
3,983 PointsGame works but not seeing a map
john larson
16,594 PointsDo you have your updated code to post? I'd love to comb through it.
Christopher Wommack
3,983 Points<p>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 x, y
def get_moves(player):
moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
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
def draw_map(player):
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!!")
while True:
moves = get_moves(player)
print("You are currently in room{}".format(player))
print("You can move{}".format(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("** Walls are hard stop walking into them!**")
continue
if player == door:
print("You escaped")
break
elif player == monster:
print("You were eaten by the grue")
break
</p>
Kenneth Love
Treehouse Guest TeacherIt never shows the map because you never called the draw_map function. Probably before the move = input("> ") line, you want to put in draw_map(player).
Just curious, are you still doing the soon-to-be-retired version of Python Collections? If so, you should definitely check out the new version of it. The dungeon game in it is much better, as are the other games.
Christopher Wommack
3,983 PointsI am currently on object oriented python object-oriented python by the way I'm sorry for the mix-up it is the computerguess.py that I am also having difficulty with not the guessing game I will upload a new Community post for that piece of code
Christopher Wommack
3,983 PointsChristopher Wommack
3,983 PointsThanks for your help the program will let me play the game but doesn't give me a map Any ideas?
john larson
16,594 Pointsjohn larson
16,594 PointsHi Christopher, I'll take a look at it in a bit (got to do some stuff), if you haven't figured it out by then I'll take another look.