so i made some code but it is not working
while True:
item = hero.findNearestItem()
if item.x == hero.x and item.y > hero.y:
hero.moveUp(1)
it spost to turn when there is a orb above it but it is not working
so i made some code but it is not working
while True:
item = hero.findNearestItem()
if item.x == hero.x and item.y > hero.y:
hero.moveUp(1)
it spost to turn when there is a orb above it but it is not working
Your hero moves by multiples of 4 when the move function is used. The items are not always located at coordinates that can be reached by the hero. For instance, your hero may be at (9, 9) and an item may be at (11, 49). You would not be able to reach the exact coordinates of this item because the distance between your hero’s x-coordinate and the item’s x-coordinate would not be a multiple of 4. You can solve this by setting variables for the coordinates of the item, dividing them by 4, rounding them, multiplying them by 4 again, then putting those into your if-statement instead of item.x and item.y.
how do you round? (20 chars)
Put Math.round(), and then put the number you want to round in the parentheses. Example: Math.round(1.5).
by 2 not 4, each grid square is 2x2 I’m quite sure
1 cell is 2x2, 1 move step is 4m.
Yeah, I think I just misunderstood something in the reply above now that you say that :]
it is still not turning when it lines up with orb
What is the code that you have for performing that function?
while True:
item = hero.findNearestItem()
x = item.x / 4
y = item.y / 4
x2 = Math.round(x)
y2 = Math.round(y)
X = x2 * 4
Y = y2 * 4
if X == hero.x and Y > hero.y:
hero.moveUp(1)
I think I understand what is wrong. I forgot that the snail cannot reach every coordinate divisible by 4 because it does not start on coordinates divisible by 4. What you need to do is subtract you snail’s coordinates from the item coordinates at the beginning before you divide, round, and multiply. Then you need to add your snail’s coordinates to the item’s coordinates at the very end after you multiply the rounded numbers.
i am just wondering if you undersatand the problem the player is not turning when it lines up with the orb
I took the code you posted and made the changes I suggested and the player turned when an orb was above them, so it should work for your code. Here are the edits I made to the code you posted in case you want to compare it to your current code:
x = (item.x-hero.x) / 4
y = (item.y-hero.y) / 4
x2 = Math.round(x)
y2 = Math.round(y)
X = x2 * 4
Y = y2 * 4
X += hero.x
Y += hero.y
it works but it won’t move right
if Y == hero.y and Y < hero.y:
hero.moveRight(1)
this is the move right code
That is because the if statement will never be true. Your hero’s y coordinate cannot be both equal to and less than the variable Y. I would recommend replacing the and with an or, or making the condition if Y <= hero.y.
i did it again i am so dum when i was doing it alone it hapend so many times thanks
You’re welcome. Glad I could help.