Hi, I’m having difficulties with the level, “Hit and Freeze”.
No matter what I do, I always start off dead.
I believe this is a glitch in the problem, but check my coding and tell me what you think.
# 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
# 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
# and save the result in the variable canAttack.
if enemy:
canAttack = inAttackRange(enemy)
# If the result stored in canAttack is True, then attack!
if canAttack == True:
hero.attack(enemy)
Hello and welcome to codecombat discourse! 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!
Please format your code correctly. Press the < / > button, and paste your code in there.
I formatted it correctly now. But no matter what I do, even if I comment all the code, I die at the start.
I copy the code here again.
# 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
# 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
# and save the result in the variable canAttack.
if enemy:
canAttack = inAttackRange(enemy)
# If the result stored in canAttack is True, then attack!
if canAttack == True:
hero.attack(enemy)
Hello, I’ve followed your directions. This is what I have now, but it has made no difference.
I still die from the very beginning.
# 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
# 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
# and save the result in the variable canAttack.
canAttack = inAttackRange(enemy)
# If the result stored in canAttack is True, then attack!
if canAttack:
hero.attack(enemy)