[SOLVED] Timber Guard help please

im having issues with my code. help is really appreciated

2 Likes
if gold:
    hero.moveXY(gold.pos.x, gold.pos.y)
# If you have enough gold, summon a soldier.
if {hero.gold > hero.costOf("soldier")}:
    hero.summon("soldier")
# Use a for-loop to command each soldier.
# For loops have two parts: "for X in Y"
# Y is the array to loop over.
# The loop will run once for each item in Y, with X set to the current item.
for friend in hero.findFriends():
    if friend.type == "soldier":
        enemy = friend.findNearestEnemy()
        defendPos = {"x": 91, "y": 40}
        # If there's an enemy, command her to attack.
        # Otherwise, move her to the right side of the map.
        if enemy:
            hero.command(friend, "attack", enemy)
        else:
            hero.command(friend, "move", defendPos)
2 Likes

@AnSeDra (20characters)

2 Likes

Try to put all of this in a while True and before the if gold put a:

gold=hero.findNearestItem()

my hero dosen’t move and a error screen appears saying “don’t know how to transform: set”

2 Likes

Can you show me your code?

while True:
    gold=hero.findNearestItem()
    # If you have enough gold, summon a soldier.
    if {hero.gold > hero.costOf("soldier")}:
        hero.summon("soldier")
        # Use a for-loop to command each soldier.
        # For loops have two parts: "for X in Y"
        # Y is the array to loop over.
        # The loop will run once for each item in Y, with X set  the current item.
        for friend in hero.findFriends():
            if friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                defendPos = {"x": 91, "y": 40}
                # If there's an enemy, command her to attack.
                # Otherwise, move her to the right side of the map.
                if enemy:
                    hero.command(friend, "attack", enemy)
                else:
                    hero.command(friend, "move", defendPos)
2 Likes

Don’t forget to put this

After this

Try to delete the { and the }

Try to put this outside this if

Do you need any more assistance at this level?

yes my hero now collects gold and spawns troops but the troops dont attack the enemys they just go to the far right side of the screen

2 Likes
while True:
    gold=hero.findNearestItem()
    if gold:
        hero.moveXY(gold.pos.x, gold.pos.y)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        # Use a for-loop to command each soldier.
        # For loops have two parts: "for X in Y"
        # Y is the array to loop over.
        # The loop will run once for each item in Y, with X set  the current item.
        for friend in hero.findFriends():
            if friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                defendPos = {"x": 91, "y": 40}
                # If there's an enemy, command her to attack.
                # Otherwise, move her to the right side of the map.
                if enemy:
                    hero.command(friend, "attack", enemy)
                else:
                        hero.command(friend, "move", defendPos)

2 Likes

Put this outside this if

Do you need any more assistance at this level?

the troops are attacking but the enemy gets there to quick

2 Likes

Can you show me your code again?

while True:
    gold=hero.findNearestItem()
    if gold:
        hero.moveXY(gold.pos.x, gold.pos.y)
    # If you have enough gold, summon a soldier.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
        # Use a for-loop to command each soldier.
        # For loops have two parts: "for X in Y"
        # Y is the array to loop over.
        # The loop will run once for each item in Y, with X set  the current item.
    for friend in hero.findFriends():
        if friend.type == "soldier":
            enemy = friend.findNearestEnemy()
            defendPos = {"x": 91, "y": 40}
            # If there's an enemy, command her to attack.
            # Otherwise, move her to the right side of the map.
            if enemy:
                hero.command(friend, "attack", enemy)
            else:
                hero.command(friend, "move", defendPos)
2 Likes

Try to chance the x and y coordonates of the defendPos.

what do you want me to change the pos to? and do you need a sword for this lvl or no?

2 Likes

You do not need a sword. As for coordonates, try to improvise until you get it right.
Can you let me know when have you finished the level?

it finally working! thanks @AnSeDra for the help! :smiley:

2 Likes