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 -- Any Feedback Welcome :)

Kenneth Love , feedback on my approach (specifically concern separation) would be greatly appreciated.

import random
input = raw_input # delete if using python3



#-- GAME SETUP --------------------------------->>>

SIZE = 10
def generate_board(size):
    x_coords = range(size)
    y_coords = x_coords

    board = []

    for x_coord in x_coords:
        for y_coord in y_coords:
            board.append((x_coord, y_coord))

    return board


BOARD = generate_board(SIZE) # should be a constant...


def get_location(num, board):

    locations = []

    for i in range(num):
        locations.append(random.choice(board))
        while i >= 1 and locations[i-1] == locations[i]:
            locations[i] = random.choice(board)

    return locations


PLAYER, MONSTER, DOOR = get_location(3, BOARD) # not constants...



#-- GAME MECHANICS --------------------------------->>>

def render_board(size, player, door):
    player_x, player_y = player
    #monster_x, monster_y = monster
    door_x, door_y = door

    board_arr = [['[ ]' for i in range(size)] for j in range(size)]
    board_arr[player_x][player_y] = '[X]'
    board_arr[door_x][door_y] = '[#]'

    for row in board_arr:
        print(''.join(row))


available_moves = list('LEFT RIGHT UP DOWN J L I K'.split())
def move_player(player, move):
    if move == 'LEFT' or move == 'J':
        player = (player[0], player[1] - 1)
    elif move == 'RIGHT' or move == 'L':
        player = (player[0], player[1] + 1)
    elif move == 'UP' or move == 'I':
        player = (player[0] - 1, player[1])
    elif move == 'DOWN' or move == 'K':
        player = (player[0] + 1, player[1])

    return player


def game_over(player):
    if player == DOOR:
        print("\n\nCONGRATULATIONS!!! You've won.\n")
        return True
    elif player == MONSTER:
        print("\n\nEEEEKK! ..you've been devoured by the MONSTER at position {}..".format(MONSTER))
        return True
    else:
        return False

available_commands = list('HELP QUIT WHEREAMI'.split()) + available_moves
command_descriptions = list('Get a list of available commands. | End gameplay. | Find out where you are on the board. | Move one unit left. | Move one unit right. | Move one unit up. | Move one unit down.'.split('|'))



#-- GAME HELPERS --------------------------------->>>

def show_help():
    print('\nHere is a list of available commands:')
    print('-'*25)
    command_dictionary = { k:v for (k,v) in zip(available_commands, command_descriptions) }
    print('QUIT: {QUIT}\nHELP: {HELP}\nWHEREAMI: {WHEREAMI}\nLEFT (J): {LEFT}\nRIGHT (L): {RIGHT}\nUP (I): {UP}\nDOWN (K): {DOWN}'.format(**command_dictionary))

def show_location():
    print("You are here, {}".format(PLAYER))



#-- ACTUAL GAME --------------------------------->>>

print('Welcome to the game. Your are here {}, and the door is here {}. GO!'.format(PLAYER, DOOR))
while True: # game loop
    print('\nChoose a direction')
    try:
        move = input('> ').upper()
        if move not in available_commands:
            raise Warning("Unknown command entered.")
    except:
        print("\n\n***Please enter a known command. Enter 'HELP' for details.***\n\n")

    if move == 'QUIT':
        break
    elif move == 'HELP':
        show_help()
    elif move == 'WHEREAMI':
        show_location()
    else:
        # Make the monster "faster"
        MONSTER = get_location(1, BOARD)
        PLAYER = move_player(PLAYER, move)
        # MONSTER = get_location(1)
        render_board(SIZE, PLAYER, DOOR)

    if game_over(PLAYER):
        break

2 Answers

Hi Emmanuel,

It seems good. I see in your code that there is a monster? I played it 3 times and didn't encounter anything.

I like the idea behind the game though.

Yeah that's an issue. If you throw a print statement for the MONSTER in, you see that there is a tuple representing it.

On a large board, it's just that collision is unlikely. I was trying to make the MONSTER move multiple times per round to increase collision probability, but felt that a collision check wod be necessary after every new position. What are you thoughts on that?

Yeah, I've played a few more times and still not encountered the monster, so the probability must be really low.

There's no harm in trying. Either that, or create multiple different monsters in the game so there is increased probability and also adds a bit of variation on what type of monster you encounter.

Also, at the start of them game you need to introduce the game a little bit more. Maybe put the 'Type HELP for assistance at any point during the game' and explain that you need to find your way to the end before getting eaten by the monster.

John Steer-Fowler, 'Roger that' on multiple monsters and intro. Thanks for the feedback!