Here is my code:
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
def commandSoldiers(nearest):
for friend in self.findFriends():
if friend and nearest:
self.command(friend, "attack", nearest)
loop:
if item:
self.move(item.pos)
nearest = self.findNearest(self.findEnemies())
if nearest:
if self.isReady("chain-lightning"):
self.cast("chain-lightning", nearest)
if self.isReady("cleave"):
self.cleave(nearest)
self.attack(nearest)
item = self.findNearest(self.findItems())
if item:
self.move(item.pos)
if self.gold>=20:
self.summon("soldier")
commandSoldiers(nearest)
When I run it, my soldiers don’t attack! The only reason I can think of for my soldiers not attacking is that commandSoldiers does not work. Can anybody tell me why?
Note:This isuue happens with every replayable level.
You placed commandSoldiers outside the block where you check if nearest exists. Move it inside.
But commandSoldiers checks whether nearest exists or not.
Ok, a lot of things are wrong
To answer your questions, you search for the nearest enemy and then you order your hero to attack it ( up to 3 times using all his abilities in a row). By the time you reach the solider command code your nearest enemy is probably dead.
- Your enemy and your hero can act in parallel. But everything your hero does takes time (attacking, moving, summoning, special powers, shielding)
For example
loop:
var i
for(i=0;i<10;i++)
enemy=self.findNearestEnemy();
self.attack(enemy);
}
self.commandSoldiers();
here your hero takes 10 attacks before commanding the soldiers. At 0.5seconds per attack the soliders will spend almost 5 seconds on each loop looking at the grass grow.
Take only one action per loop
You want this type of code:
loop:
self.commandSoldiers()
if cleave and enemy
cleave enemy
continue
if enemy
attack enemy
continue
if potion
collect potion
continue
The continue skips to the end of the loop so you’ll be able to command your soldiers again before taking another action
=============================================================================
- Asking all your soldiers to attack the same enemy is a bad idea, unless you want to focus fire.
The soldiers will push each other away and spend most of the time running to get to your enemy.
With the archers you do not have this problem, but it can still result into an overkill and attacks lost on a dying enemy
Your troops have their own friend.findNearestEnemy, friend.findEnemies, friend.distanceTo
Use that
I followed your instructions and now my soldiers are attacking… some of the time. Here is my new code:
# Survive the waves of ogres.
# If you win, the level gets harder, and gives more rewards.
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
def commandSoldiers():
for friend in self.findFriends():
nearest=friend.findNearest(friend.findEnemies())
if friend and nearest:
self.command(friend, "attack", nearest)
loop:
commandSoldiers()
item = self.findNearest(self.findItems())
if item:
self.move(item.pos)
if self.gold>=20:
self.summon("soldier")
nearest = self.findNearest(self.findEnemies())
if self.isReady("invisibility"):
self.cast("invisibility", self)
continue
if self.hasEffect("hide"):
continue
if nearest:
if self.isReady("chain-lightning"):
self.cast("chain-lightning", nearest)
continue
if self.isReady("cleave"):
self.cleave(nearest)
continue
self.attack(nearest)
- You might want to add a continue inside the if after self.move(item.pos) and self.summon
if item:
self.move(item.pos)
continue
- With high level glasses, you can see lots of enemies that are to far for your solders to see. Use your sight to detect the enemies and let the soldiers only pick the nearest:
enemies=this.findEnemies()
commandSoldiers(enemies)
and
def commandSoldiers(enemies):
...........
nearest=friend.findNearest(enemies)
My code still doesn’t work. Did I follow your instructions correctly?
# Survive the waves of ogres.
# If you win, the level gets harder, and gives more rewards.
# If you lose, you must wait a day to re-submit.
# Each time you submit gives a new random seed.
def commandSoldiers(enemies):
for friend in self.findFriends():
nearest=friend.findNearest(enemies)
if friend and nearest:
self.command(friend, "attack", nearest)
loop:
commandSoldiers(self.findEnemies())
item = self.findNearest(self.findItems())
if item:
self.move(item.pos)
continue
if self.gold>=20:
self.summon("soldier")
nearest = self.findNearest(self.findEnemies())
if self.isReady("invisibility"):
self.cast("invisibility", self)
continue
if self.hasEffect("hide"):
continue
if nearest:
if self.isReady("chain-lightning"):
self.cast("chain-lightning", nearest)
continue
if self.isReady("cleave"):
self.cleave(nearest)
continue
self.attack(nearest)
-
It works very well for me. The only portion where the soldiers are not attacking is when your hero tries to attack an enemy which is far away.
Try this (in the place of your last cleave-attack part):
distance=self.distanceTo(enemy)
if (distance > 6):
self.move(enemy)
else
if self.isReady("cleave"):
self.cleave(nearest)
continue
self.attack(nearest)
You need boots with move (not moveXY) for this.
- Remove this part:
if self.hasEffect("hide"):
continue
it tells your hero not to do anything at all if invisible
If I attack while invisible, the invisibility goes away.
Oh, learned something new. But the idea is to kill enemy as fast as possible: in brawls the enemies keep spawning so if you do not kill them fast enough you’ll have more and more enemies on you.
Invisibility then should be used only for strategic purposes, for example if you have a squishy wizard and you want to hide & run.
what level are you on? i am on level 4 and i think its impossible to finish this level
Preparing to win level 7.
Hi, I am really stuck on game development 2 level 5 Chokepoint and my soliders are not moving as well how do I fix this???
this is my code so far
Ogres are advancing through the forest lanes!
Spawn some soldiers and have them defend their lanes!
def defendLane(event):
# Remember to create a variable for the target, to remember:
unit = event.target
# Save the unit’s starting pos.x
startX = unit.pos.x
while True:
enemy = unit.findNearestEnemy()
# If there is an enemy.
if enemy:
# Use unit.attack to attack the enemy:
unit.attack(enemy)
pass
else:
# Move the unit back to it’s starting x and y.
unit.moveXY(startX, 11, 16)
def commandSoldiers():
for friend in self.findFriends():
nearest=friend.findNearest(friend.findEnemies())
if friend and nearest:
self.command(friend, “attack”, nearest)
game.spawnXY(“soldier”, 12, 15)
game.spawnXY(“soldier”, 31, 16)
game.spawnXY(“soldier”, 54, 16)
game.spawnXY(“soldier”, 75, 16)
Set the event handler defendLane on “spawn” event for "soldier"s.
def defendNearestFriend(event):
unit = event.target
friend = unit.findearestFriend
while True:
enemy = unit.findNearestEnemy()
if enemy.distanceTo(friend) :
unit.attack(enemy)