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

can someone explain this problem, I have been trying to picture this problem in my mind but I can not.

if you can answer step by step will be more than awesome. Thank you

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
    xx,yy =direction
    if 9 > x > 0 == -1
        x = x-1
    if 0 >= x > 9     
    return x, y, hp

1 Answer

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

I don't like to give away the answer, but to break it down:

def move(player, direction):
    # unpack the player and move
    x, y, hp = player
    mx, my = direction

    # apply the move to the player
    x = x + mx
    y = y + my

    # test if the player has gone outside the bounds, all four sides
    # if so, set them at the bound and reduce the hit points
    if x > 9:
        x = 9
        hp = hp - 5

    if y > 9:
        y = 9
        hp = hp - 5

    if x < 0:
        x = 0
        hp = hp - 5

    if y < 0:
        y = 0
        hp = hp - 5

    # Return the player
    return x, y, hp

Thank you, it helps me a lot. I can see now the logic behind and the correlation between the place of the player and the movement.