# Desert Combat (python)

Here’s my code, I’m stuck.

``````# while-loops repeat until the condition is false.

ordersGiven = 0
while ordersGiven < 5:
# Move down 10 meters.
hero.moveXY(10, -10)
# Order your ally to "Attack!" with hero.say
# They can only hear you if you are on the X.
hero.say("Attack!")
# Be sure to increment ordersGiven!
ordersGiven += 1
while True:
enemy = hero.findNearestEnemy()
# When you're done giving orders, join the attack.
if enemy:
hero.attack(enemy)

``````

The key to this level to to use relative coordinates. Rather than moving to an absolute position — `hero.moveXY(10, -10)` — you want to move to a position relative to where your hero currently is.

`hero.moveXY(hero.pos.x, hero.pos.y)` moves exactly to where your hero is (not at all); how can you modify it?

2 Likes

Yes, like Hydrobolic said, You don’t want to move to a specific position. In this case, your hero will move to (10,-10) and say “Attack” 5 times. You want to move 10 meters from your current position (hero.pos.x, hero.pos.y).

1 Like

I’ll try that, thanks!

1 Like

It still doesn’t work (I’ve been having a difficult time copying my code, so I’m sending a pic):

This is the link if you need it:
CodeCombat - Coding games to learn Python and JavaScript?

Right now you’re commanding your hero to move to exactly its own position, which of course does nothing. You want to move to a position relative to your hero.

Say there’s a point at `(30, 50)`. The point 10 units below it is `(30, 50 - 10)`, or `(30, 40)`. This can be generalized to any point, where the point 10 units below `(x, y)` is `(x, y - 10)`.

Your `hero` is at the point `(hero.pos.x, hero.pos.y)`, and you want to apply the above.

1 Like

You move to your hero’s position, which will always be the same unless you move. You need to move 10m below your current position (hero.pos.y - 10). You need to use the same x position though (hero.pos.x).

1 Like

Thanks guys, I got it.

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.