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

Akinola Ogooluwa
Akinola Ogooluwa
1,255 Points

i do not understand the movement.py question. please help

Our game's player only has two attributes, x and y coordinates. Let's practice with a slightly different one, though. This one has x, y, and "hp", which stands for hit points.

Our move function takes this three-part tuple player and a direction tuple that's two parts, the x to move and the y (like (-1, 0) would move to the left but not up or down).

Finish the function so that if the player is being run into a wall, their hp is reduced by 5. Don't let them go past the wall. Consider the grid to be 0-9 in both directions. Don't worry about keeping their hp above 0 either.

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

    return x, y, hp

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The task is Finish the function so that if the player is being run into a wall, their hp is reduced by 5. Don't let them go past the wall. Consider the grid to be 0-9 in both directions. Don't worry about keeping their hp above 0 either.

Part 1: getting the new position. The code shows how to break out the players x-position, y-position, and hit points. The direction to move can be broken out from the direction is a similar way to get the x-move and y-move changes. To get the new player position, add the x-position and x-move, and also add the y-position and y-move.

Part 2: handling walls: A wall is "hit" if the players new position would move them passed the edge of the grid: a coordinate that is less than zero or greater than the largest grid value. In this case, reset the position back to the edge of the grid (0 or max_value), and deduct 5 from the current hp value.

Part 3: return the corrected player position and hp value (as shown in the provided code snippet.

Post back if you have any other questions. Good luck!!