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!
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

Joanna Dudley
4,727 PointsTrying to add clear() to dungeon game
I'm trying to add a clear() function to my game to stop my console from scrolling. However, what I'm doing doesn't seem to work. Does anyone have any insight into what I'm doing wrong?
import random
import os
CELLS = [(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)]
def clear():
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
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('_|'))
print('\n\n')
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']
# player = (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
# game loop
clear()
monster, door, player = get_locations()
print("Welcome to the dungeon")
while True:
moves = get_moves(player)
draw_map(player)
print("You're 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)
if move not in moves:
print("That's not a vaild move! Try again")
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 monster!")
break
1 Answer

Chris Freeman
Treehouse Moderator 68,390 PointsYou can put a clear()
as the last statement of the while
code block. That way it runs just before each iteration restarts.
There seems to be a bug in your if move in moves
code.
if move in moves:
# this runs if move in moves
player = move_player(player, move)
if move not in moves:
# this runs if move is Not in moves
print("That's not a vaild move! Try again")
else:
# this runs if move in moves (seems not what you want)
print("** Walls are hard, stop walking into them! **")
continue # Why is this continue here?
This could be simplified to:
if move in moves:
# this runs if move in moves
player = move_player(player, move)
else:
# this runs if move is Not in moves
print("That's not a vaild move! Try again")
Joanna Dudley
4,727 PointsJoanna Dudley
4,727 PointsThank you!