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

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

A little assistance with def move :)

This is kinda getting my lol seems like I would work. Lemme know your opinions!!



# 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
    dx, dy = direction
    if (dx + x) > 9 or (dy + y) > 9 or (dx + x) < 0 or (dy + y) < 0:
        hp -= 5
        if x > 9:
            x = 9
        if y > 9:
            y = 9
        if x < 0:
            x = 0
        if y < 0:
            y = 0

    return x, y, hp

1 Answer

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Hi Jamal Rodgers,

That is certainly one way to do it ... although I do notice you've not made allowance for a legitimate player move; one where a wall collision does not occur. You might want to pop that into the move function :smile: You could do it just by taking an else onto the bottom as your if condition only deals with wall hit moves, meaning if the condition doesn't pick it up then it is legit.
If it's a legit move you'll just need to modify x and y with the direction modifiers passed in - the ones you put in dx and dy ... so they can get returned if player makes a move that doesn't involve wall headbanging.

Another approach to the problem if you're interested. Crazily over commented as I was trying to explain each step to someone else.

Nice work! and happy coding,
Dave :dizzy:

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

OMG lol thanks for that, I completely forgot about VALID moves xD Will add that in!

Thanks a lot!