Hi, I was having trouble with Protect and Serve, and I’m not sure how to finish it.
# Protect the workers and animals!
# Defend these two positions:
defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }
defend[2] = {"x": 96, "y":73}
soldiers = []
friends = hero.findFriends()
for index in range(len(friends)):
friend = friends[index]
if friend.type == "soldier":
soldiers.append(friend)
else:
# Defend the workers:
hero.command(soldier, "defend", {'x':friend.pos.x, 'y':friend.pos.y})#Error appears here
while True:
# Use a for-loop to assign each soldier to a corresponding defend[] target
# Use command(soldier, "defend", thang) or command(soldier, "defend", position)
for i in range(len(defend)):
soldier = soldiers[i]
if soldier:
hero.command(soldier, "defend", defend[i])
#Pretty sure that there's something wrong with this code somehow, too.
pass
I see a couple of issues. First, in your for index loop (as you point out), you have:
# Defend the workers:
hero.command(soldier, "defend", {'x':friend.pos.x, 'y':friend.pos.y})#Error appears here
Instead of actively defending, which comes later, you want to add (append) the friend to the ‘defend’ array.
Second, you’ve manually added defend[2] to the code…why?
As you’ve also pointed out, in the while True…you are intending to work through the soldiers array (iterate), but are doing so using an attribute of the defend array. You should use the soldiers array attributes instead.
Thanks, I changed my code, but I added the defend[2] to stop the ogres from taking that group of coins for the bonus objective. I took it out and the code worked fine, but when I tried to reinsert it, my code stopped working. Is this how I’m supposed to get the bonus objective?
Update: I told my hero to defend the position instead, and I got the bonus objective. Thank you for your help!
Sorry. It seems I am not allowed to write in this chat any more:
"An error occurred: We’re sorry, but new users are temporarily limited to 3 replies in the same topic.
# In diesem Level wird dein Held nicht kämpfen.
# Befehle deinen Schützen auf den Gegner mit der höchsten Gesundheit zu schießen
def opfer(units):
#enemies = hero.findEnemies()
enemyIndex = 0
strongest = None
besthealth = 0
for enemyIndex in range(len(units)):
enemy = units[enemyIndex]
if enemy.health > besthealth:
besthealth = enemy.health
strongest = enemy
return strongest
while True:
enemies = hero.findEnemies()
friends = hero.findFriends()
Index = 0
for Index in range(len(friends)):
friend = friends[Index]
target = opfer(enemies)
if friend.type == "archer" and target:
hero.command(friend, "attack", target)
I suggest that you make a new topic for the other level to keep things organized. At the home screen on the top right, you can click on [ + New Topic ] (If you need help in that, feel free to send me a PM)
As for the level protect and serve, can you show me your gear? Like this for example: