while True:
# Find coins and/or attack the enemy.
# Use flags and your special moves to win!
item = hero.findNearestItem()
enemy = hero.findNearestEnemy()
flag = hero.findFlag("green")
if item:
pos = item.pos
x = pos.x
y = pos.y
hero.moveXY(x, y)
elif enemy:
hero.attack(enemy)
What is wrong with my code? The CPU keeps winning
There might not be anything wrong with your code, it just might not beat a competing code based on the goals assigned for the level. The multiplayer levels are more about strategy than simply writing code.
Keep a look out for coins that are out of bounds that your hero tries to get. You can put a check before you move to it so it doesn’t cost you time. Also, the move() method is more effective at getting to the nearest coin at all times. The hero runs through the rest of the code after every step so if a closer item appears, he/she will change course right away. Another simple help, pick gear that makes you move faster.
I think the problem is that all you do is collect coins. A lot of the fights that I lost were because someone just targets the enemy whenever they spawn and only then do they collect the coins. I believe swapping the 2 would do the trick for you. Something like this:
if enemy:
#your code for attacking the enemy
elif item:
#your code for collecting coins