def commandPaladins():
for paladin in hero.findByType("paladin", hero.findFriends()):
if paladin.canCast("heal", hero):
self.command(paladin, "cast", "heal", self)
else:
weakestFriend2 = weakestFriend()
if weakestFriend2:
hero.command(paladin, "cast", "heal", weakestFriend2)
else:
hero.command(paladin, "move", {'x': hero.pos.x - 4, 'y': hero.pos.y})
# Fight your way into the Inner Sanctum of the ogre chieftain, and defeat her.
def commandArchers():
for archer in hero.findByType("archer", hero.findFriends()):
enemy = archer.findNearestEnemy()
if enemy:
hero.command(archer, "attack", enemy)
def commandSoldiers():
for soldier in hero.findByType("soldier", hero.findFriends()):
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
def commandGriffins():
for griffin in hero.findByType("griffin-rider", hero.findFriends()):
enemy = griffin.findNearestEnemy()
if enemy:
hero.command(griffin, "attack", enemy)
else:
hero.command(griffin, "defend", hero)
def commandPaladins():
for paladin in hero.findByType("paladin", hero.findFriends()):
if paladin.canCast("heal", hero):
self.command(paladin, "cast", "heal", self)
def summonGriffin():
if hero.gold >= hero.costOf("griffin-rider"):
hero.summon("griffin-rider")
def moveForward():
targetPos = {'x': 290, 'y': 34}
hero.move(targetPos)
def pickTarget():
enemies = hero.findEnemies()
target = None
minDistance = 300
for enemy in enemies:
if enemy.type != "door":
if enemy.type is "catapult":
target = enemy
break
elif enemy.type is "warlock":
target = enemy
break
elif enemy.type is "witch":
target = enemy
break
elif enemy.type is "chieftain":
target = enemy
break
else:
if hero.distanceTo(enemy) < minDistance:
minDistance = hero.distanceTo(enemy)
target = enemy
if target:
return target
def commandHero():
target = pickTarget()
friend = hero.findFriends()
if target:
while target.health > 0:
if hero.isReady("bash") and target.health > 116:
hero.bash(target)
else:
hero.attack(target)
moveForward()
else:
moveForward()
while True:
summonGriffin()
commandArchers()
commandSoldiers()
commandGriffins()
commandPaladins()
commandHero()
pass
Yes, I did get this from someone else, but I’ve altered it and tried out different things during the last month. However, nothing worked and I have no idea what I’m doing wrong. If anyone knows why this code isn’t working, please let me know.
My hero dies, but I don’t have enough gems to buy any armor. Other times, my hero gets stuck on a wall and it just stays there trying to walk somewhere but going nowhere, then the time just runs out.
Have you tried writing your own code from scratch? I’m not massively against using someone else’s code, if you alter it; but writing code specifically for your hero might be a good idea if this code isn’t working.
Danny
Can you elaborate more on the stage based system? I don’t really understand what “hero.time” is. Also, when you command friends, how do you command the different friends?
Hero.time is the time that you are in the level. e.g. when 10 seconds have passed from starting the level, hero.time == 10. Stages can be used to do different parts of a big level. Like fighting the troops at the start, then moving inside the building, then the bit with the paladins, then the end. You can find out when it would happen and make a stage using hero.time. Then you command your friends to do a specific thing at that time.
You just need to define the stages inside your while true loop and you can use them in all your functions.
For the friends:
for friend in friends:
if friend.type == "archer":
#command
In the “for friend in friends”, for the “friends”, do you have to also write: “friends = hero.findFriends” or “hero.findNearestFriend”? I don’t have codecombat open right now, so I don’t know exactly what the code looks like. I have to go now, so I’ll try making my own code tomorrow, thank you so much for your help!
Try to look again at this section. You are commanding an array, not an unit. You should command each friend that is in friends in order for them to execute the commands.
And on what line do you get the error?
Here’s my code:
while True:
paladins =
enemies = hero.findEnemies()
if hero.canCast(“invisibility”, hero):
hero.cast(“invisibility”, hero)
friends = hero.findFriends()
for friend in friends:
if friend.type == “paladin”:
paladins.append(paladin)
for enemy in enemies:
if enemy.type == “catapult”:
while enemy.health > 0:
hero.attack(enemy)
friends = hero.findFriends()
for friend in friends:
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, “attack”, enemy)
fire = hero.findNearestEnemy()
if fire:
while fire.health > 0:
hero.attack(enemy)
if fire.type == ‘tower’:
pass
if hero.pos.x > 160:
hero.moveXY(175, 34)
hero.moveXY(175, 8)
hero.moveXY(245, 14)
hero.moveXY(245, 34)
while hero.health < hero.maxhealth:
hero.moveXY(245, 34)
if hero.gold >= 20:
hero.summon(“soldier”)
paladins = hero.findByType(“paladin”)
for paladin in paladins:
if paladin:
if paladin.canCast(“heal”, paladin):
hero.command(paladin, “cast”, “heal”, hero)
if hero.maxHealth <= hero.health:
if fire:
hero.moveXY(277, 34)
hero.moveXY(277, 5)
DJ = hero.findNearestEnemy()
if DJ:
while DJ.health > 0:
hero.attack(DJ)
hero.moveXY(277, 48)
lie = hero.findNearestEnemy()
if lie:
while lie.health > 0:
hero.attack(lie)
while True:
paladins = hero.findByType(“paladin”)
for paladin in paladins:
if paladin:
if paladin.canCast(“heal”, paladin):
hero.command(paladin, “cast”, “heal”, hero)
fire = hero.findNearestEnemy()
if fire:
hero.attack(fire)
Please help!
while True:
paladins = []
enemies = hero.findEnemies()
if hero.canCast("invisibility", hero):
hero.cast("invisibility", hero)
friends = hero.findFriends()
for friend in friends:
if friend.type == "paladin":
paladins.append(paladin)
for enemy in enemies:
if enemy.type == "catapult":
while enemy.health > 0:
hero.attack(enemy)
friends = hero.findFriends()
for friend in friends:
enemy = hero.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
fire = hero.findNearestEnemy()
if fire:
while fire.health > 0:
hero.attack(enemy)
if fire.type == 'tower':
pass
if hero.pos.x > 160:
hero.moveXY(175, 34)
hero.moveXY(175, 8)
hero.moveXY(245, 14)
hero.moveXY(245, 34)
while hero.health < hero.maxhealth:
hero.moveXY(245, 34)
if hero.gold >= 20:
hero.summon("soldier")
paladins = hero.findByType("paladin")
for paladin in paladins:
if paladin:
if paladin.canCast("heal", paladin):
hero.command(paladin, "cast", "heal", hero)
if hero.maxHealth <= hero.health:
if fire:
hero.moveXY(277, 34)
hero.moveXY(277, 5)
DJ = hero.findNearestEnemy()
if DJ:
while DJ.health > 0:
hero.attack(DJ)
hero.moveXY(277, 48)
lie = hero.findNearestEnemy()
if lie:
while lie.health > 0:
hero.attack(lie)
while True:
paladins = hero.findByType("paladin")
for paladin in paladins:
if paladin:
if paladin.canCast("heal", paladin):
hero.command(paladin, "cast", "heal", hero)
fire = hero.findNearestEnemy()
if fire:
hero.attack(fire)