Hi everyone, I am stuck on the level Protect and Serve…can anyone give me any pointers please?
My code works, the animals all survive, but not all of the villagers survive (and they need to).
# Protect the workers and animals!
# Defend these two positions:
defend = []
defend[0] = { "x": 98, "y": 28 }
defend[1] = { "x": 84, "y": 7 }
soldiers = []
friends = hero.findFriends()
for index in range(len(friends)):
friend = friends[index]
if friend.type == "soldier":
soldiers.append(friend)
else:
# Defend the workers:
defend.append(friend)
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(soldiers)):
soldier = soldiers[i]
target = defend[i]
hero.command(soldier, "defend", target)
hero.moveXY(100, 18)
enemy = hero.findNearestEnemy()
if enemy and enemy.health > 0:
hero.attack(enemy)
pass