Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Help! Im stuck on zoo keeper


#1

help im stuck on this level! this is my code

points = []
points[0] = {“x”: 33, “y”: 42}
points[1] = {“x”: 47, “y”: 42}
points[2] = {“x”: 33, “y”: 26}
points[3] = {“x”: 47, “y”: 26}
while self.gold <=80:
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
for i in range(4):
self.summon(“soldier”)
loop:
friends = self.findFriends()
for j in range(len(friends)):
point = points[j]
friend = friends[j]
enemy = friend.findNearestEnemy()
if enemy and enemy.team == “ogres” and friend.distanceTo(enemy) < 5:
self.command(friend, “attack”, enemy)
if friend:
self.command(friend, “move”, point)


#2

does anyone know whats wrong with it?


#3

my friends dont attack the ogres and im not sure what the problem is.


#4

Maybe the if friend: cancels out the “attack” command above it, but it’s hard to be sure since the code isn’t formatted properly.

Please format your code properly by putting the proper back ticks or using the preformated text button. It’s explained in the FAQ that everyone should read "before posting."


#5

Y U NO FORMAT


#6

Please format correctly by looking at the FAQ.


#7

If you give two successive orders to a unit in the same loop cycle (or without letting time pass)
the second order will override the first:
command(attack) + command(move) = command(move)

See:


#8

thanks! It worked!!:grin:


#9

My code doesn’t work. My hero just walks away for about 3 second and than summons the soldiers and doesn’t collect gold first.

points = []
points[0] = {"x": 33, "y": 42}
points[1] = {"x": 47, "y": 42}
points[2] = {"x": 33, "y": 26}
points[3] = {"x": 47, "y": 26}
item = hero.findNearestItem()
# 1. Collect 80 gold.
for i in range(80):
    hero.move(item.pos)
# 2. Build 4 soldiers.
for i in range(4):
    hero.summon("soldier")
    
# 3. Send your soldiers into position.
while True:
    friends = hero.findFriends()
    for j in range(len(friends)):
        point = points[j]
        friend = friends[j]
        enemy = friend.findNearestEnemy()
        if enemy and enemy.team == "ogres" and friend.distanceTo(enemy) < 5:
            # Command friend to attack.
            hero.command(friend, "attack", enemy)
            pass
            
        elif friend:
            # Command friend to move to point.
            hero.command(friend, "move", point)
            pass
            
            

#10

Hi, the problem is that your hero’s only able to move towards one item, because you’ve only defined on item.
And since move takes practically no time he’ll only move towards it properly when move’s looped.
You should try and use something like:
while hero.gold …
but remember to define the item inside the loop.
:lion: :lion: :lion:


#11

Thank you so much I was stuck for weeks.


#12

My pleasure :grin:,
:lion: