Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[SOLVED] Timber Guard Python help

Hi,

I’m stuck on this level, it all seems to work fine but the soldiers I send out don’t all go to the same point, they often stop walking half way. The first soldier I send out moves to the XY coordinates but the rest sort of stop en route, is it something to do with them spotting an enemy?

Here’s my code:

while True:
# Collect gold.
coins = hero.findNearestItem()
hero.moveXY(coins.pos.x,coins.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()
# 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:
pos = {‘x’: 68, ‘y’: 42}
hero.command(friend, “move”, pos)

Thanks in advance

Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly

Apologies! Hopefully it’s formatted correctly this time:

while True:
    # Collect gold.
    coins = hero.findNearestItem()
    hero.moveXY(coins.pos.x,coins.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()
                # 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:
                    pos = {'x': 68, 'y': 42}
                    hero.command(friend, "move", pos)

Try using the move method instead of moveXY. When using the moveXY method, once it identifies the nearest item the code doesn’t iterate through until the moveXY is complete. When using the move method, the hero takes a single step towards the item and then the code looks to see if there’s anything else it could be doing before taking another step. You need to command your troops and can’t do that if you’re moveXY is completing. MoveXY is good in some situations and move is better in others. Make sure that you are equipped with boots supporting the move method.

Thanks for the response. I added your suggested change but the same still happened - I also tried commanding them to go further to the right of the screen:

while True:
    # Collect gold.
    coins = hero.findNearestItem()
    hero.move(coins.pos,coins.pos)
    # 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()
                # 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:
                    pos = {'x': 75, 'y': 42}
                    hero.command(friend, "move", pos)

image

The screenshot is just to show the reinforcements not moving, they walk a little bit and then stop

click on your boots in the middle of the game screen to see the proper use of the move method. You should always put an if conditional statement before using a method to find items. If there is ever a split second where there are no items, the code will fail.

Also, what glasses are you using?

I’m using crude telephoto glasses and I’ve got boots of jumping. I’ve added an if statement at the start for if item but I don’t see what I’ve done wrong with the move method. I’ve realised that the first soldier goes off fine but the subsequent ones get confused when the enemy they are moving towards dies, they then freeze and don’t move again for 5 or so seconds.

This is a structure problem. Look where the for loop is located. The only time you will command your friends is when you summon another soldier. The for loop needs to be inside the while True loop, but it is erroneously nested in another if conditional.

Ah I see, thanks for the help. Makes sense now!