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 trialfahad lashari
7,693 PointsCan't really understand clearly enough what the question requires me to do? Any help?
I've done the solution that I implement into my own dungeon game. However it doesn't seem to work here. Any pointers would be appreciated.
kind regards,
Fahad
# 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
x1, y1 = direction
if x1 == 0:
hp-5
if x1 == 9:
hp-5
if y1 == 0:
hp-5
if y1 == 9:
hp-5
return x, y, hp
2 Answers
leonardbode
Courses Plus Student 4,011 PointsYou haven't changed the original x, y coordinates.
# using your code as an example where:
x1, y1 = direction
# change x, y values according to direction
x, y = x + x1, y + y1
Then
if x > 9:
hp -= 5
x -= 1
elif x < 0:
hp -= 5
x += 1
Do the same for y-value and return x, y, hp
# -= is called a decrement
x -= 5
# is the same as
x = x - 5
# += is called an increment
x += 5
# is the same as
x = x + 5
fahad lashari
7,693 PointsHi Leonard Bode, I know you've already answered my question. However I am still a bit hung up on the purpose of:
x -=1
and
x+=1
I would just like to know what role do they play in this task. I am asking this as it will help me understand the question a bit more.
leonardbode
Courses Plus Student 4,011 PointsNo problem, Fahad. If you look back to my answer you see that we change the values of x or y depending on direction.
def move(player, direction):
x, y, hp = player
x_move, y_move = direction
# here we change x, y according to direction
x, y = x + x_move, y + y_move
# then we check if x or y is some invalid integer
if x > 9:
# if it is an invalid integer, like 10 or -1, we remove 5 health points and return the player to a valid integer
# in this cases, x is greater than 9, ie 10. Therefore, we remove 5 hp and make x = 9
hp -= 5
x -= 1
elif x < 0:
hp -= 5
x += 1
elif y > 9:
hp -= 5
y -= 1
elif y < 0:
hp -= 5
y += 1
Then, if x or y is greater than 9 or less than 0, we subtract 5 from health because the player ran into a wall, and we return the player to a valid x, y coordinate.
if x > 9:
hp -= 5
x -= 1
Because we assume that if x > 9 then x must be equal to 10, the above code is the same as this:
if x == 10:
hp -= 5
x -= 1
# and the above code is the same as saying:
if x == 10:
hp -= 5
x = 9
So,
x -= 1
just returns x to a valid integer.
fahad lashari
7,693 Pointsfahad lashari
7,693 PointsHi Thanks for answering the question. Can you explain a bit more in terms of what the:
x -= 1
and
x += 1
do.
And also. Where did the 'x2' come from?
Kind regards,
Fahad
leonardbode
Courses Plus Student 4,011 Pointsleonardbode
Courses Plus Student 4,011 PointsSorry Fahad, x2 should be y1. I changed it in my code example.
fahad lashari
7,693 Pointsfahad lashari
7,693 PointsLeonard Bode
No worries at all. I figure it would be y1. I am just re-doing the challenge to understand everything that is happening.
Kind regards,
Fahad