Using this code, my hero just stands there and not do anything. What is wrong with this?
# Use your new skill to choose what to do: hero.time
while True:
# If it's the first 10 seconds, attack.
if hero.time < 10:
if enemy:
enemy = hero.findNearestEnemy
hero.attack(enemy)
pass
# Else, if it's the first 35 seconds, collect coins.
elif hero.time < 35:
if coin:
coin = hero.findNearestItem
hero.moveXY(coin.pos.x, coin.pos.y)
pass
# After 35 seconds, attack again!
else:
hero.attack(enemy)
pass
Use your new skill to choose what to do: hero.time
while True:
# If it’s the first 10 seconds, attack.
if hero.time < 10:
if enemy:
enemy = hero.findNearestEnemy
hero.attack(enemy)
pass
# Else, if it’s the first 35 seconds, collect coins.
elif hero.time < 35:
if coin:
coin = hero.findNearestItem
hero.moveXY(coin.pos.x, coin.pos.y)
pass
# After 35 seconds, attack again!
else:
hero.attack(enemy)
pass
can you please help me solve this code in javascipt.
// Night is coming! Move all the soldiers towards the fire.
function centerFormation(event) {
// event.target is the unit running this event handler.
var unit = event.target;
// Now use unit.moveXY to move the unit to the fire.
}
// This spawns the four soldiers:
game.spawnXY(“soldier”, 16, 57);
game.spawnXY(“soldier”, 15, 13);
game.spawnXY(“soldier”, 63, 13);
game.spawnXY(“soldier”, 67, 57);
// This sets the soldier’s spawn action to the function centerFormation:
game.setActionFor(“soldier”, “spawn”, centerFormation);
# Use your new skill to choose what to do: hero.time
while True:
# If it's the first 10 seconds, attack.
if hero.time < 10:
enemy= hero.findNearestEnemy()
hero.attack(enemy)
pass
# Else, if it's the first 35 seconds, collect coins.
elif hero.time < 35:
items = hero.findItems()
if items:
hero.moveXY(items.pos.x, items.pos.y)
# After 35 seconds, attack again!
elif hero.time>35:
hero.attack(enemy)
else:
hero.moveXY(item.pos.x, item.pos.y)
pass
Welcome to the forum! This is a family-friendly place where coders can share bugs, ask for help on any CodeCombat level (don’t forget to post your code correctly), or just hang out with other coders.
Have a great time!
Shouldn’t both of the hero.attack(enemy)s be in if enemy: loops?
while True:
# If it's the first 10 seconds, attack.
if hero.time < 10:
enemy= hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
pass
# Else, if it's the first 35 seconds, collect coins.
elif hero.time < 35:
items = hero.findNearestItem()
if items:
hero.moveXY(items.pos.x, items.pos.y)
# After 35 seconds, attack again!
elif hero.time > 35:
if enemy:
hero.attack(enemy)
else:
hero.moveXY(item.pos.x, item.pos.y)
pass