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

Dungeon game Python

Hi guys I follow the video and the code is same with teacher, however, once I execute it , and move the player, the program will copy the previous dungeon automaticly, Below is my code. I try many times but cannot figure it out

#draw grid
# pick the random location for player
#pick the random location for door
#pick the random location for monster
#draw player in the grid
#take input movement
#move player , unless invalid move (pass edge of grid)
#check win or loss
#redraw the grid 
import os
import random
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():
    os.system('cls' if os.name=='nt' else 'clear')
def get_location():
    return random.sample(CELLS,3)


def move_player(player,move):
    x,y=player
    if move=='LEFT':
        x=x-1
    if move =='RIGHT':
        x=x+1
    if move =='UP':
        y=y-1
    if move == 'DOWN':
        y=y+1
    return x,y 
    #get the player location
    #if move left x-1
    #if move right x+1
    #if move up y+1
    #if move down y-1

    return player

def get_moves(player):
    moves=['RIGHT','LEFT','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')
    #if player's y==0,they can't moveup
    #if player's y==4. they can't move down
    #if player's x==0, they can't move left
    # if player's x==4, they can't move right
    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:
                output=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_location()
    while True:
        draw_map(player)
        valid_move=get_moves(player)

        print('You are currently in room {}'.format(player)) #fill the player position
        print('You can move {}'.format(','.join(valid_move))) # valid move
        print('Enter Q/q to quit the game')

        move=input('>').upper()
        if move=='Q':
            break
        if move not in ['RIGHT','LEFT','DOWN','UP']:
          input('Please enter valid direction')
          continue
        if move in valid_move:
            player = move_player(player,move) 
        else:
            input('\n ** Walls are hard, do not run into them **\n')
        clear()

    # good move? change the position
    #bad move? Change nothing
    #on the door ? they win
    # on the monster? they lose
    #otherwise loop back around

clear()
print('Welcome to dungeon!')
input('Press return to start!')
clear()
game_loop()

It seems to be working for me. You don't have any code that provides any win/lose conditions(e.g. finding the door or landing on the monster) So all you will be able to do right now is wander around the dungeon and exit with Q. The map is predefined at the beginning. The player location also appeared to be randomized every time the game was launched. Can you provide more information what you mean by it "copies the previous dungeon"?

For example, once you hit enter, and in the program , the dungeon will show, and you input the valid move, like RIGHT ,LEFT,UP ,and DOWN. The player will move in the dungeon,but the one you scroll your mouse, you will find another dungeon up on current dungeon. Once you input a move, it will generate a new dungeon

1 Answer

Ah I see. You are saying that if someone inputs a wrong move then it redraws the map after you press enter so you have two maps. This is because you need to add the clear() function after input('Please enter valid direction'). This will clear the screen and then redraw the map.

It works, Thank you very much. Already, give the best answer