Hello, I’m on the Library Tactician level and I’m having problems with summoning soldiers in a loop.
When I try to summon soldiers inside of the loop it does it only once but if I put multiple:
if self.gold >= self.costOf("soldier"):
self.summon("soldier")
it does it twice.
Here’s my full code:
def commandSoldier(soldier, soldierIndex, numSoldiers):
angle = Math.PI * 2 * soldierIndex / numSoldiers
defendPos = {"x": 41, "y": 40}
defendPos.x += 10 * Math.cos(angle)
defendPos.y += 10 * Math.sin(angle)
self.findFriends()
self.command(soldier, "defend", defendPos);
# Find the strongest target (most health)
# This function returns something! When you call the function, you will get some value back.
def findStrongestTarget():
mostHealth = 0
bestTarget = None
enemies = self.findEnemies()
bestTarget = enemies
# Figure out which enemy has the most health, and set bestTarget to be that enemy.
if enemies.health > mostHealth:
mostHealth = enemy.health
bestTarget = enemy
# If the strongestTarget has more than 15 health, attack that target. Otherwise, attack the nearest target.
def commandArcher(archer):
enemies = self.findEnemies()
closest = archer.findNearestEnemy
if archerTarget:
self.command(archer, "attack", archerTarget)
elif closest:
self.command(archer, "attack", nearest)
archerTarget = None
loop:
# If archerTarget is dead or doesn't exist, find a new one.
if self.gold >= self.costOf("soldier"):
self.summon("soldier")
elif not archerTarget or archerTarget.health <= 0:
# Set archerTarget to be the target that is returned by findStrongestTarget()
archerTarget = findStrongestTarget()
friends = self.findFriends()
soldiers = self.findByType("soldier")
for i, soldier in enumerate(soldiers):
commandSoldier(soldier, i, len(soldiers));
# use commandArcher() to command your archers
commandArcher(friends)
commandSoldier(friends)
Is there any way to fix this? Thank you.