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

Cory Sovilla
Cory Sovilla
11,087 Points

Dungeon Game Modification

So I didn't change much just the look of the game. It annoyed my that my screen was constantly filled with previous information so I cleared it.

I went back to Letter Game and looked at the code to clear the screen and incorporated it into Dungeon Game.

My only delima I can't seem to figure out is how to NOT clear the screen if they run into a wall (Left,Right,Up or Down). My guess would be to modify the move command somehow.

Below is all the code for the game, I also added an ASCII T-Rex that kills you if you step on the tile he is on.

import random
import os

CELLS = [(0,0), (0,1), (0,2),
         (1,0), (1,1), (1,2),
         (2,0), (2,1), (2,2)]

def get_locations():
    # monster = random
    monster = random.choice(CELLS)
     # door = random
    door = random.choice(CELLS)
    # start = random
    start = random.choice(CELLS) # Player

    # if monster, door, or start are the same, do it again
    if monster == door or monster == start or door == start:
        return get_locations()

    # return monster, door, start
    return monster, door, start


def move_player(player, move):
    # player = (x, y)
    x, y = player

    if move == 'LEFT':
        y -=1
    elif move == 'RIGHT':
        y +=1
    elif move == 'UP':
        x -=1
    elif move == 'DOWN':
        x +=1

    return x,y

    # Get the player's current location
    # If move is LEFT, y - 1
    # If move is RIHGT, y + 1
    # If move is UP, x - 1
    # If move is DOWN, x + 1

def get_moves(player):
    moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
    # player = (x, y)

    # if player's y is 0, remove LEFT
    if player[1] == 0:
        moves.remove('LEFT')

    # if player's y is 2, remove RIGHT    
    if player[1] == 2:
        moves.remove('RIGHT')

    # if player's x is 0, remove UP    
    if player[0] == 0:
        moves.remove('UP')

    # if player's x is 2, remove DOWN    
    if player[0] == 2:
        moves.remove('DOWN')
    return moves

def draw_map(player):
    print(' _ _ _')
    tile = '|{}'
    for idx, cell in enumerate(CELLS):
        if idx in [0,1,3,4,6,7]:
            if cell == player:
                print(tile.format('X'), end='')
            else:
                print(tile.format('_'), end='')
        else:
            if cell == player:
                print(tile.format('X|'))
            else:
                print(tile.format('_|'))


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

monster, door, player = get_locations()
print("Welcome to the dungeon!")

while True:

    moves = get_moves(player)
    clear()

    print("You're currently in room {}".format(player)) # fill in with player position

    draw_map(player)

    print('')
    print("You can move {}".format(moves)) # fill in with available moves 
    print("Enter QUIT to quit")

    move = input("> ")
    move = move.upper()

    if move == 'QUIT':
        break

    if move in moves:
        player = move_player(player, move)
    else:
        print("** Walls are hard, stop walkng into them! **")
        moves()
        continue

    if player == door:
        clear()
        print("You've escaped!")
        break
    elif player == monster:
        clear()
        print("You were eaten by a T-Rex!")
        print('')

        print("                                              ____")
        print("   ___                                      .-~. /_\"-._")
        print("  `-._~-.                                  / /_ \"~o\  :Y")
        print("      \  \                                / : \~x.  ` ')")
        print("       ]  Y                              /  |  Y< ~-.__j")
        print("      /   !                        _.--~T : l  l<  /.-~")
        print("     /   /                 ____.--~ .   ` l /~\ \<|Y")
        print("    /   /             .-~~\"        /| .    ',-~\ \L|")
        print("   /   /             /     .^   \ Y~Y \.^>/l_   \"--'")
        print("  /   Y           .-\"(  .  l__  j_j l_/ /~_.-~    .")
        print(" Y    l          /    \  )    ~~~.\" / `/\"~ / \.__/l_")
        print(" |     \     _.-\"      ~-{__     l  :  l._Z~-.___.--~")
        print(" |      ~---~           /   ~~\"---\_  ' __[>")
        print(" l  .                _.^   ___     _>-y~")
        print("  \  \     .      .-~   .-~   ~>--\"  /")
        print("   \  ~---\"            /     ./  _.-'")
        print("    \"-.,_____.,_  _.--~\     _.-~")
        print("                ~~     (   _}  ")
        print("                        `. ~)")
        print("                         (  \\")
        print("                         /,`--'~\--'")

        print('')
        break

    # If it's a good move, change player's position
    # If it's a bad move, don't change anything
    # If the new player position is the door, they win!
    # If the new player position is the monster, they lose!
    # Otherwise, continue

Any help with this would be greatly appreciated. :D

Happy Coding!

Kenneth Love