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

Please help in timber guard[SOLVED]

I really need help in timber guard
here is my code:
while True:

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': 72, 'y': 42}
                    hero.command(friend, "move", pos)

1 Like

hero.move coin.pos. no need for coin.pos, coin.pos. like this: hero.move(coin.pos)

you can do greaterthan or equal to

try replacing

 coins = hero.findNearestItem()
    hero.move(coins.pos,coins.pos)

with

 item = hero.findNearestItem()
    if (item):
        if (hero.isReady("jump")):
            hero.jumpTo({'x': item.pos.x, 'y': item.pos.y})
1 Like

Start there I do not think it considers coin as a variable

1 Like

it doesn’t work with the

item = hero.findNearestItem()
    if (item):
        if (hero.isReady("jump")):
            hero.jumpTo({'x': item.pos.x, 'y': item.pos.y})

:grinning:
with this:

coin = hero.findNearestEnemy
if coin:
    hero.move(coin.pos)

You have found the nearest item and defined it as coin. Then check if there is the coin with and if statement. Then move to coin.pos.

because if you use that you have to use a if/else statement but i did a more flexible code you should try @Dragonlouis way

1 Like

Once you solve your code ill show you what i did but then i will have to delete my code afterwards

2 Likes

Don’t write this. You should write hero.findFriends in the while true loop. Basically, you should do this:

friends = hero.findFriends()
for friend in friends:
    #whatever goes here

nice new avatar! I liked the old one though. And yes, that is true.

thanks, but that is off topic.

what happens is I can spawn the soldiers but after like 4 of them go the the spot I chose the rest that spawns just stay on the left side instead of the right side so If I can get the soldiers to go the the right side then I would have finished the level

Replace that with this:

hero.command(friend, "move", {"x": 83, "y": 46})

I already solved it
Make title solved @Ryan_Wong

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.