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 Python Collections (Retired) Dungeon Game Building the Game: Part 2

Daire Ferguson
Daire Ferguson
1,592 Points

Map wont register DOWN movement

OK I have followed Kenneths instructions to the letter but my map wont register a down movement (and it seems an up movement wont register either).

I cant screenshot all the code and am not using workspaces to do the code either so im gonna throw it up the unliked way:

import random


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


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

    if monster == door or monster == start or door == start:
        return get_locations()

    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


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

    if player[1] == 0:
        moves.remove('LEFT')
    if player[1] == 2:
        moves.remove('RIGHT')
    if player[0] == 0:
        moves.remove('UP')
    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('_|'))


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


while True:
    moves = get_moves(player)

    print("You're currently in room {}".format(player))

    draw_map(player)

    print("You can move {}".format(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 walking into them! **")
        continue

    if player == door:
        print("You escaped!")
        break
    elif player == monster:
        print("You were eaten by the grue")
        break



    # if it a good move, change the players position
    # if it is a bad move, dont change the position
    # if the new player position is the door they win
    # if the new player position is the monster they lose
    # otherwise continue
Daire Ferguson
Daire Ferguson
1,592 Points

sorry for the shoddy upload on the code side

It's okay, I went ahead and fixed it for you. Be sure to check out the markdown cheatsheet when posting on how to properly format code.

1 Answer

Hey Daire,

Your code looks good, but you have one single typo that's breaking your down movement.

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

elif move == ' DOWN': this line is looking for a space before DOWN, instead of just DOWN. If you remove that space, it should work as intended.

Hope that helps!

Daire Ferguson
Daire Ferguson
1,592 Points

perfect thanks dude works like a charm now

Sure thing! Be sure to select best answer when your question is solved, it helps other users find solutions and denotes that you don't need any further help.