Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dungeon Game Hit points

tyler borg
tyler borg
9,429 Points

I'M SO LOST. PLZ HELP! (hit points)

I'm stuck on the hit points challenge and have looked through all the community questions but I still don't understand what's going on...All the movements and variables are really confusing me and I can't understand how this works. Can someone outline step by step what's going on here? Would be much appreciated...thx.

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
    xdir, ydir = direction
    if xdir or ydir > 9:
        hp -= 5
    if xdir or ydir < 0:
        hp -= 5
    return x, y, hp

3 Answers

diogorferreira
diogorferreira
19,363 Points

xdir and ydir will always be either -1, 0, -1 - it's the direction in which the player is moving. So it would never be more than 9 and the if statement would never be executed. What you need to compare is the player's x and y positions and check if they're trying to move out of bounds. E.g when the x is 0, xdir cannot be -1 otherwise the character would lose 5HP.
This is the example I am referring to > move((0, 1, 10), (-1, 0)) => (0, 1, 5).

tyler borg
tyler borg
9,429 Points

I'm still confused...I think I'm confused on just how many variables are at play...you're saying that xdir is a separate variable from x?

tyler borg
tyler borg
9,429 Points

i think im starting to understand it a little...this is what i have now even though its still not right.

def move(player, direction):
    x, y, hp = player
    xdir, ydir = direction
    if x + xdir <= 0:
        hp -= 5
    if y + ydir <= 0:
        hp -= 5
    if x + xdir >= 9:
        hp -= 5
    if y + ydir >= 9:
        hp -= 5
    else:
        x + xdir
        y + ydir

    return x, y, hp
tyler borg
tyler borg
9,429 Points

ok...i figured it out. I just had to add an = sign to the +