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 trialAshley Keeling
11,476 PointsI don't know what I am missing for this to work
I am not sure what I ne to do for this to work
# 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
if direction not in CELLS:
hp-=5
else:
if move == "A":
x-=1
if move == "D":
x+=1
if move == "W":
y-=1
if move == "S":
y+=1
return x, y, hp
4 Answers
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHi Ashey, did you manage to complete the challenge? Luke is correct, however, sorry to disagree Luke with your last statement. The the test x+h < 0 or x+h > 9 already checks if they are close to the wall, unless I am missing something :)
def move(player, direction):
x, y, hp = player
h, v = direction # direction holds the horizontal and vertical moves
if x + h < 0 or x + h > 9: # test if x + the move hits a wall
hp -=5 # if yes then -5 hitpoints
else: # else make the move
x += h
if y + v < 0 or y + > 9: # test if y + the move hits a wall
hp -=5
else:
y += v
return x, y, hp
Luke Strama
6,928 PointsTo solve this, you'll have to unpack the direction tuple as well. I used x_move and y_move as variables. What you have to do is create an 'if' statement where if x becomes -1 or 10 by adding the direction (x_move) to it, the hp variable will decrease by 5 hp. You'll have to do the same for the y coordinate. Lastly, you'll have to create an else statement that if the x and y coordinates both stay within the correct range, the player moves to the proper coordinates.
Also, in your first 'if' statement, you mention the object CELLS which is not assigned anywhere so that will automatically make the program fail as well.
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsThis is a confusing challenge - here are some pointers that hopefully will help
# 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
h, v = direction # direction holds the horizontal and vertical moves
if x + h < 0 or x + h > 9: # test if x + the move hits a wall
hp -=5 # if yes then -5 hitpoints
else: # else make the move
x += h
# add in the code here to make the vertical move
return x, y, hp
Hope that helps
Ashley Keeling
11,476 Pointsthanks but I have tried it and it says :
Bummer! Hmm, didn't get the right movement results. Hitting a wall didn't hurt the player
do you now what I should do?
def move(player, direction):
x, y, hp = player
h, v = direction
if x + h < 0 or x + h > 9:
hp -=5
else:
x += h
y += v
return x, y, hp
Luke Strama
6,928 PointsYour current if statement only deals with the x coordinates. You still have to add the code for what happens when the Y coordinates are out of range. Also, there needs to be code included that does not move the character if they are already on the edge of the grid. So instead of x += h, you would have x = x.
Luke Strama
6,928 PointsLuke Strama
6,928 PointsStuart, sorry yes you're right! I had done it a slightly different way and had a bit of blind spot to your code.