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
Nathan English
Front End Web Development Techdegree Student 10,817 PointsDungeon game issue
import random
import os
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
CELLS = [(0,0),(1,0),(2,0,),(3,0),(4,0),
(0,1),(1,1),(2,1),(3,1),(4,1),
(0,2),(1,2),(2,2),(3,2),(4,2),
(0,3),(1,3),(2,3),(3,3),(4,3),
(0,4),(1,4),(2,4),(3,4),(4,4)]
def get_location():
return random.sample(CELLS,3)
def move_player(player,move):
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
def get_moves(player):
move = ["LEFT","RIGHT","UP","DOWN"]
x,y = player
if x == 0:
move.remove("LEFT")
if x == 4:
move.remove("RIGHT")
if y == 0:
move.remove("UP")
if y == 4:
move.remove("DOWN")
return move
def draw_map(player):
print(" _" * 5)
tile = "|{}"
for cell in CELLS:
x,y = cell
if x < 4:
line_end = ""
if cell == player:
output = tile.format("X")
else:
output = tile.format("_")
else:
line_end = "\n"
if cell == player:
output = tile.format("X|{")
else:
output = tile.format("_|")
print(output, end=line_end)
def game_loop():
monster,door, player = get_location()
while True:
draw_map(player)
valid_moves = get_moves(player)
print("You're Currently in room {}".format(player))
print("You can move {}".format(", ".join(valid_moves)))
print("Enter QUIT to quit")
monster,door,player = get_location
move = input("> ")
move = move.upper()
if move == 'QUIT':
break
if move in valid_moves:
player = move_player(player,move)
else:
input("\n ** Walls are hard! Don't run into them! **\n")
clear_screen()
clear_screen()
print("Welcome to the dungeon!")
clear_screen()
game_loop()
For some reason my game is randomizing the player movements. I really feel like it's a small error, but I can't find out what it is.
3 Answers
KRIS NIKOLAISEN
54,974 PointsAlso remove
monster,door,player = get_location
from here - this is your random movement
print("Enter QUIT to quit")
monster,door,player = get_location
and remove the bracket {
output = tile.format("X|{")
from here
else:
line_end = "\n"
if cell == player:
output = tile.format("X|{")
else:
output = tile.format("_|")
print(output, end=line_end)
KRIS NIKOLAISEN
54,974 PointsI copied your code and it doesn't run at all but I did notice there is a comma after the zero here (2,0):
CELLS = [(0,0),(1,0),(2,0,),(3,0),(4,0),
Nathan English
Front End Web Development Techdegree Student 10,817 PointsThank you, I was working on this pretty late and sometimes forget what time it is lol.