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 (2016, retired 2019) Dungeon Game Hit points

Henry Lin
Henry Lin
11,636 Points

I don't understand where I did wrong for the movement challenge!

I have commented my code, but i don't really get it where I did wrong.

movement.py
# EXAMPLES:
# move((1, 1, 10), (-1, 0)) => (0, 1, 10)
# move((0, 1, 10), (-1, 0)) => (0, 1, 5)
# move((0, 9, 5), (0, 1)) => (0, 9, 0)

def move(player, direction):
    x, y, hp = player
    h,v = direction # unpacking direction coordinates to horizontal and vertical

    # if vertical equals zero which means we need move player horizontally along with the x axis
    if v == 0 and h != 0:
        x += h
        # check if the player hit the wall after move
        if x > 9 or x < 0:
            hp -= 5 #oops hit the wall, hp lost

    # if horizontal equals zero which means we need move player vertically along with the y axis
    if h == 0 and v != 0:
        y += v
        if y > 9 or y < 0:
            hp -= 5 #oops hit the wall, hp lost
    return x, y, hp

1 Answer

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hello,

in your code you check the movement direction, then add to the existing value, then check if the player hit a wall and reduce the hp. Therefore the player can move past the wall. I would approach the challenge by first checking if the player is moving into the wall and reduce the hp, returning the original x and y values along with the reduced hit point in each case, if after checking, no wall is hit, then adjust the coordinates.

def move(player, direction):
    x, y, hp = player
    x_move, y_move = direction

    if x_move == 1 and x == 9:
        hp -= 5
    elif x_move == -1 and x == 0:
        hp -= 5
    elif y_move == 1 and y == 9:
        hp -= 5
    elif y_move == -1 and y == 0:
        hp -= 5
    else:
        x += x_move
        y += y_move

    return x, y, hp
Henry Lin
Henry Lin
11,636 Points

Hi James, I have figured it out. I didn't see the condition of "DON'T LET THEY PAST OVER THE WALL". Thank you for help Happing coding :)