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

Python

my Dungeon Game

import random
import getch
import os

X_Y = []
x = list(range(10))
y = list(range(10))
for i in x:
    for j in y:
        X_Y.append((i, j))

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')
    return


def locations(rooms=()):
    if len(rooms) == 3:
        return rooms
    if len(rooms) > 3:
        return locations()
    new_room = random.choice(X_Y)
    if new_room not in rooms:
        rooms = (new_room,) + rooms[:]
    return locations(rooms)


def build_grid():
    grid = {}
    for room in X_Y:
        grid[room] = 'empty'

    monster, door, player_location = locations(X_Y)
    grid[monster] = 'monster'
    grid[door] = 'door'
    grid[player_location] = 'player'
    return grid

def print_grid(grid):
    clear()
    print("\tWelcome to the Dungeon. \nFind the door while avoiding the monster. \n\tUse: \n\t\t 'i' to move up\n\n'j' to move left,\t\t'l' to move right \n\n\t\t 'k' to move down\n  \nYour character is shown by the '~'. \nEmpty spaces show where you have been. \nEnter 'q' at any time to quit.\n")
    # player_trail = []
    for i in range(0, len(X_Y), 10):
        temp = '|'
        for j in range(10):
            room = grid[ X_Y[i + j] ]
            if room == 'player':
                temp = temp + ' ~ '
            elif room == 'done':
                temp = temp + '   '
            else:
                temp = temp + ' # '
        temp = temp + '|'
        print(temp)
    print('')
    return


def read_key():
    arrowKeys = {
        'i' : 'up',
        'j' : 'left',
        'k' : 'down',
        'l' : 'right',
        '8' : 'up',
        '5' : 'down',
        '4' : 'left',
        '6' : 'right',
        'q' : 'quit'
        }
    key_press = getch.getch().lower()
    if key_press not in arrowKeys:
        print("I'm sorry Dave. I can't do that. \nTry using 'i, j, k, l or 8, 4, 5, 6'.")
        return read_key()
    return arrowKeys[key_press]


def move(direction, grid):
    player_location = ''
    rooms = grid.keys()
    for room in rooms:
        if grid[room] == 'player':
            player_location = room
            break

    if player_location:
        if direction == 'up':
            new_player_location = (player_location[0] - 1, player_location[1])
        elif direction == 'down':
            new_player_location = (player_location[0] + 1, player_location[1])
        elif direction == 'left':
            new_player_location = (player_location[0], player_location[1] - 1)
        elif direction == 'right':
            new_player_location = (player_location[0], player_location[1] + 1)

        if 0 <= new_player_location[0] < 10 and 0 <= new_player_location[1] < 10:
            if grid[new_player_location] == 'monster':
                return 'monster'
            if grid[new_player_location] == 'door':
                return 'door'
            else:
                grid[player_location] = 'done'
                grid[new_player_location] = 'player'

    return grid



def main():
    grid = build_grid()
    print_grid(grid)

    while True:
        direction = read_key()
        if direction == 'quit':
            return

        grid = move(direction, grid)
        if grid == 'monster':
            print('Eeek! The monster ate you.')
            break
        elif grid == 'door':
            print('Sucess! You found the door.')
            break
        else:
            print_grid(grid)

    print('Would you like to play again? Y/n')
    again = getch.getch().lower()
    if again == 'y':
        return main()

    return


if __name__ == '__main__':
    main()

@kennethlove

Nathan Tallack
Nathan Tallack
22,164 Points

Nice work! Love the large dungeon. Getch looks wonderfully simple, thanks for showing that module. :)

If you are looking for more to do, move your monster around randomly with each turn, revealing it to the player if it is in an adjacent square so they can get frightened and run away from it. :)

Very cool, man. I love seeing how you did some of this stuff. The way you made the coordinates is really clever, and I like the idea of a dictionary for the key movements. Thanks a lot for sharing!