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! Feedback greatly appreciated

Hey guys, here's my take on the dungeon game from Kenneth's collections module. I added a normal/hard mode, and an HP system. This was a really fun project, and taught me quite a bit! I'm extremely new to coding and I feel like this is still a bit sloppy, so any feedback or suggestions would be super helpful!

A few changes I'm already working on are: -A single play_again function(instead of the 2 currently) that lets you change the difficulty -General clean-up of some of the code

import random
import os


CELLS_NORMAL = [
    (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)
]

CELLS_HARD = [
    (0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0), (9, 0),
    (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1),
    (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2),
    (0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3), (9, 3),
    (0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4), (9, 4),
    (0, 5), (1, 5), (2, 5), (3, 5), (4, 5), (5, 5), (6, 5), (7, 5), (8, 5), (9, 5),
    (0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6), (8, 6), (9, 6),
    (0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7), (8, 7), (9, 7),
    (0, 8), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8), (8, 8), (9, 8),
    (0, 9), (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9), (9, 9),
]


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


def move_player(player, move):
    x, y = player
    if move == "UP":
        y -= 1
    if move == "DOWN":
        y += 1
    if move == "LEFT":
        x -= 1
    if move == "RIGHT":
        x += 1
    return x, y


def monster_encounter(hp):
    hp -= 1
    input("You ran into a monster! You managed to fight him off, but your HP is now {}.".format(hp))
    return hp


def game_loop_normal():
    player, monster, door = get_locations_normal()
    hp = 3
    while True:
        clear_screen()
        draw_map_normal(player)
        valid_moves = valid_moves_normal(player)
        print("You are playing Dungeon of Death on NORMAL mode\n"
              "Your current location is {}".format(player))
        print("You have {} HP".format(hp))
        print("Which direction would you like to move? You can go {}".format(", ".join(valid_moves)))
        print("<Type 'Q' to quit.>")
        move = input(">    ").upper()

        if move == "Q":
            clear_screen()
            print("Thanks for playing!")
            break
        if move in valid_moves:
            player = move_player(player, move)
            if player == monster:
                if hp > 1:
                    hp = monster_encounter(hp)
                    monster = random.choice(CELLS_NORMAL)
                    while monster == player or monster == door:
                        monster = random.choice(CELLS_NORMAL)
                else:
                    play_again_normal()
                    break
            if player == door:
                print("\n** CONGRATULATIONS! You found the escape door!")
                print("You've escaped death.....this time!")
                play_again = input("Would you like to try again? [Y/N]\n"
                                   ">   ").lower()
                if play_again == "y" or play_again == "yes":
                    game_loop_normal()
                else:
                    clear_screen()
                    input("Thanks for playing! I hope you had fun!")
                    break

        else:
            input("Sorry, that's not a valid direction! Press <ENTER> to try again.")
            continue


def get_locations_normal():
    return random.sample(CELLS_NORMAL, 3)


def valid_moves_normal(player):
    x, y = player
    moves = ["UP", "DOWN", "LEFT", "RIGHT"]
    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_normal(player):
    print(" _" * 5)
    for cell in CELLS_NORMAL:
        x, y = cell
        if player == cell:
            if x == 0:
                print("|X|", end="")
            elif x == 4:
                print("X|")
            else:
                print("X|", end="")     
        elif x == 0:
            print("|_|", end="")
        elif x == 4:
            print("_|")
        else:
            print("_|", end="")


def play_again_normal():
    print("\n** YOU RAN INTO THE MONSTER! Your health was too low to get away this time....You lose **")
    print("\nBetter luck next time!")
    play_again = input("Would you like to try again? [Y/N]\n"
                       ">   ").lower()
    if play_again == "y" or play_again == "yes":
        game_loop_normal()
    else:
        clear_screen()
        print("Thanks for playing! I hope you had fun!")


def game_loop_hard():
    player, monster1, monster2, monster3, door = get_locations_hard()
    hp = 3
    while True:
        clear_screen()
        draw_map_hard(player)
        valid_moves = valid_moves_hard(player)
        print("You are playing Dungeon of Death on HARD mode\n"
              "Your current location is {}".format(player))
        print("You have {} HP".format(hp))
        print("Which direction would you like to move? You can go {}".format(", ".join(valid_moves)))
        print("<Type 'Q' to quit.>")
        move = input(">    ").upper()

        if move == "Q":
            clear_screen()
            print("Thanks for playing!")
            break
        if move in valid_moves:
            player = move_player(player, move)
            if player == monster1:
                if hp > 1:
                    hp = monster_encounter(hp)
                    monster1 = random.choice(CELLS_HARD)
                    while monster1 == player or monster1 == door or monster1 == monster2 or monster1 == monster3:
                        monster1 = random.choice(CELLS_HARD)
                else:
                    play_again_hard()
                    break
            if player == monster2:
                if hp > 1:
                    hp = monster_encounter(hp)
                    monster2 = random.choice(CELLS_HARD)
                    while monster2 == player or monster2 == door or monster2 == monster1 or monster2 == monster3:
                        monster2 = random.choice(CELLS_HARD)
                else:
                    play_again_hard()
                    break
            if player == monster3:
                if hp > 1:
                    hp = monster_encounter(hp)
                    monster3 = random.choice(CELLS_HARD)
                    while monster3 == player or monster3 == door or monster3 == monster2 or monster3 == monster1:
                        monster3 = random.choice(CELLS_HARD)
                else:
                    play_again_hard()
                    break
            if player == door:
                print("\n** CONGRATULATIONS! You found the escape door!")
                print("You've escaped death.....this time!")
                play_again = input("Would you like to try again? [Y/N]\n"
                                   ">   ").lower()
                if play_again == "y" or play_again == "yes":
                    game_loop_normal()
                else:
                    clear_screen()
                    input("Thanks for playing! I hope you had fun!")
                    break

        else:
            input("Sorry, that's not a valid direction! Press <ENTER> to try again.")
            continue


def get_locations_hard():
    return random.sample(CELLS_HARD, 5)


def valid_moves_hard(player):
    x, y = player
    moves = ["UP", "DOWN", "LEFT", "RIGHT"]
    if x == 0:
        moves.remove("LEFT")
    if x == 9:
        moves.remove("RIGHT")
    if y == 0:
        moves.remove("UP")
    if y == 9:
        moves.remove("DOWN")
    return moves


def draw_map_hard(player):
    print(" _" * 10)
    for cell in CELLS_HARD:
        x, y = cell
        if player == cell:
            if x == 0:
                print("|X|", end="")
            elif x == 9:
                print("X|")
            else:
                print("X|", end="")           
        elif x == 0:
            print("|_|", end="")
        elif x == 9:
            print("_|")
        else:
            print("_|", end="")


def play_again_hard():
    print("\n** YOU RAN INTO THE MONSTER! Your health was too low to get away this time....You lose **")
    print("\nBetter luck next time!")
    play_again = input("Would you like to try again? [Y/N]\n"
                       ">   ").lower()
    if play_again == "y" or play_again == "yes":
        game_loop_hard()
    else:
        clear_screen()
        print("Thanks for playing! I hope you had fun!")


#Explain game in welcome screen
#Make Normal/Hard modes, normal uses less cells and has 1 monster, hard uses more cells, has more monsters
#Make HP system, player starts with X hp, and loses Y hp each monster encounter, dies at 0 HP
#Make scoreboard system to keep track of wins/losses for multiple plays
#possibly make some kind of randomized battle system for monster encounters?
clear_screen()
input("Welcome to the DUNGEON OF DEATH! Press <ENTER> to begin!")
clear_screen()
difficulty = input("Please choose [Normal/Hard]: \n"
                   "Normal - Smaller dungeon size, one monster\n"
                   "Hard - Large Dungeon, more monsters\n\n"
                   ">    ").lower()
while difficulty != "normal" and difficulty != "hard":
    difficulty = input("I didn't recognize that, please enter the difficulty you would like to play on! [Normal/Hard]\n"
                       ">    ").lower()
if difficulty == "normal":
    game_loop_normal()
else:
    game_loop_hard()

Thanks again to anyone who takes the time to read through it all!