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 trialPaulo Costa Jr
6,347 PointsCan i get some help here please?
so... i'm stuck here since yesterday and i can't figure it out what to put inside the if block... i guess i should sum x and y from player argument.... can you help me out?
# 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)
import operator
limit = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1), (8, 1),
(0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2),
(0, 3), (1, 3), (2, 3), (3, 3), (4, 3), (5, 3), (6, 3), (7, 3), (8, 3),
(0, 4), (1, 4), (2, 4), (3, 4), (4, 4), (5, 4), (6, 4), (7, 4), (8, 4),
(0, 5), (1, 5), (2, 5), (3, 5), (4, 5), (5, 5), (6, 5), (7, 5), (8, 5),
(0, 6), (1, 6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7, 6), (8, 6),
(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7), (8, 7),
(0, 8), (1, 8), (2, 8), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8), (8, 8)]
def move(player, direction):
x, y, hp = player
if player in limit:
tuple(map(operator.add, player, direction))
if player not in limit:
print("it's not a valid command")
hp -= 5
return x, y, hp
3 Answers
leonardbode
Courses Plus Student 4,011 PointsYou haven't changed the original x, y coordinates.
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
Here is my solution as an example:
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
If you have any other questions I will update my answer, if you do not have any other questions:
Remember to upvote and to choose the best answer so that your question receives a checkmark in forums.
Kind regards,
Leo
Paulo Costa Jr
6,347 PointsThank you very much, it helped me a lot !
abdulkadir yıldız
2,711 Pointsright answer is here
def move(player, direction):
x, y, hp = player
a,b = direction
x += a
y += b
if x not in range(10):
x -= a
y -= b
hp -= 5
elif y not in range(10):
x -= a
y -= b
hp -= 5
return x, y, hp
Andrew Kircher
16,612 Pointsdef move(player, direction):
x, y, hp = player
dx, dy= direction
if dx !=0:
x+=dx
if x not in range(10):
hp-=5
x-=dx
if dy !=0:
y+=dy
if y not in range(10):
hp-=5
y-=dy
return x, y, hp
Paulo Costa Jr
6,347 PointsPaulo Costa Jr
6,347 PointsI couldn't update the question, so i'm posting the challenge here...
Challenge Task 1 of 1
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.