Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Sanamdeep Singh
seal-mask
.a{fill-rule:evenodd;}techdegree
Sanamdeep Singh
Python Web Development Techdegree Student 1,741 Points

UnboundLocalError in Dungeon Game

When I try to run the code, it's giving me Unbound Local Error, not sure why. Traceback (most recent call last):
File "dungeon.game.py", line 104, in <module>
game_loop()
File "dungeon.game.py", line 81, in game_loop
draw_map(player)
File "dungeon.game.py", line 74, in draw_map
print(output, end = line_end)
UnboundLocalError: local variable 'output' referenced before assignment

1 Answer

Sanamdeep Singh
seal-mask
.a{fill-rule:evenodd;}techdegree
Sanamdeep Singh
Python Web Development Techdegree Student 1,741 Points
import os
import random

# draw grid
#pick random location for player
# for player
#monster
#draw player in the grid
#take input for movment
#move player, unless incalid move(past edges of grid)
#check for win/loss
#clear screen and redraw grid

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 clear_screen():
    os.system('cls'if os.name == 'nt'else 'clear')


def get_locations():
    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):
    moves = ["LEFT","RIGHT","UP","DOWN"]
    x,y = player
    if x == 0:
        moves.remove("LEFT")
    if x == 4:
        moves.remove("RIGHT")
    if y == 0:
        moves.remove("UP")
    if y == 4:
        moves.remove("DOWN")
    return moves


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:
                out = 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_locations()

    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")

        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!")
input("Press return to start!")
clear_screen()
game_loop()