[SOLVED] Keeping Time Help Needed

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

add () to the end of the 6th line of code and in the 12th line of code.

I tried it, same results.

  1. You have a conditional statement, if enemy, before you define the variable, enemy.
  2. You have a conditional statement, if coin, before you define the variable, coin.

You must define your variables first before using/calling them.

1 Like

I mean like coin = hero.findNearestItem()

Also, check if the enemy exists after 35 seconds.

This worked. Thanks.

this is the wrong code can you help me.

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

Could you please post your code correctly as it says in this
Lydia

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);

Please post your code formatted correctly.
Lydia

# 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

I have no idea what I did wrong, please help.

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! :partying_face:

Shouldn’t both of the hero.attack(enemy)s be in if enemy: loops?

Can you demonstrate what you mean by that, please?

So, where you have

in your code, surround them with an if enemy loop to make them look like this:

if enemy:
    hero.attack(enemy)
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

like this?

Yep

Also move this to the top of the while true loop (just below where it says: # If it's the first 10 seconds, attack.)

Anyway, I’ve got to go now, sorry.

Thank you! It worked after I changed a few more things.