What do I do? How do I make my hero attack the enemies without killing himself by moving towards them?
You are trapped. Don’t move, it’ll be painful.
This function checks if the enemy is in your attack range.
def inAttackRange(enemy):
distance = hero.distanceTo(enemy)
# Almost all swords have attack range of 3.
if distance <= 3:
return True
else:
return False
pass
Attack ogres only when they’re within reach.
while True:
# Find the nearest enemy and store it in a variable.
enemy = hero.findNearestEnemy()
# Call inAttackRange(enemy), with the enemy as the argument
inAttackRange(enemy)
# and save the result in the variable canAttack.
canAttack = inAttackRange(enemy)
# If the result stored in canAttack is True, then attack!
if inAttackRange is True:
hero.attack(enemy)
pass
Hello and welcome to codecombat discourse @Michael_Joseph! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!
Can you please format your code as it is described below so we will be able to help you solve the level?