Hi everyone in this level I’m having some trouble(I want to get 44 kills) , anyone can help me?
This is my code.
def enemy():
enemy = hero.findNearestEnemy()
if enemy:
distance = hero.distanceTo(enemy)
if enemy.type == "orge":
hero.moveXY(5, 29)
if distance < 25 and enemy.type == "munchkin" or "scout":
if hero.canCast("lightning-bolt"):
hero.cast("lightning-bolt", enemy)
elif hero.canCast("chain-lightning") and distance < 15:
hero.cast("chain-lightning", enemy)
elif hero.canCast("roge"):
hero.cast("roge")
else:
hero.attack(enemy)
elif hero.canCast("regen",hero):
hero.cast("regen", hero)
else:
hero.moveXY(5, 33)
while True:
enemy()
I am a student so I can’t reply to the message right away, and my English is not good so if my word have some grammatical error do not blame me.
Thanks.
so the enemy.type == “ogre” should be enemy.team == “ogres” instead
1 Like
Also how much time did you make it?
only 37.2s.
and this is my new code
def enemy():
enemy = hero.findNearestEnemy()
if enemy:
distance = hero.distanceTo(enemy)
if enemy.type == "orge":
hero.moveXY(5, 29)
if distance < 25 and enemy.team == "munchkin" or "scout":
if hero.canCast("lightning-bolt"):
hero.cast("lightning-bolt", enemy)
elif hero.canCast("chain-lightning") and distance < 15:
hero.cast("chain-lightning", enemy)
elif hero.canCast("roge"):
hero.cast("roge",enemy)
elif hero.canCast("regen",hero):
hero.cast("regen", hero)
else:
hero.attack(enemy)
elif hero.canCast("roge"):
hero.cast("roge",enemy)
elif hero.canCast("regen",hero):
hero.cast("regen", hero)
else:
hero.attack(enemy)
elif hero.canCast("regen",hero):
hero.cast("regen", hero)
else:
hero.moveXY(5, 33)
while True:
enemy()
thank you
No, but don’t know why had before
I can give you a tip, don’t use while True. The seed is always the same, so therefore you can hard-code functions.
For example, this is a function for attacking a munchkin
def attackMunchkin():
enemy = hero.findNearestEnemy()
if enemy and enemy.type == "munchkin":
hero.attack(enemy)
hero.attack(enemy) # You need 2 hits
This is for attacking scout:
def attackScout():
enemy = hero.findNearestEnemy()
if enemy and enemy.type == "scout":
if hero.canCast("root"):
hero.cast("root", enemy)
else:
hero.attack(enemy)
1 Like
and if you want to wait in place, do this:
def waitFor(time):
for i in range(time):
hero.say("wait") #--> talking takes one second.
waitFor(4) #--> wait 4 seconds
Thank you but now I’m going to class so after 45 minutes i will come bake
Okay
I’m come back.
how do i know how many enemies are there.
CodeCombat - Coding games to learn Python and JavaScript?
enemies = hero.findEnemies() # this returns an array of all enemies
print(len(enemies)) # len(enemies) is the length of enemies
Btw, this level don’t support the findEnemies() function, so you can’t know how much enemies are there.
def enemy():
enemy = hero.findNearestEnemy()
if enemy:
distance = hero.distanceTo(enemy)
if enemy.type == "catapult":
hero.moveXY(5, 29)
if distance < 25 and enemy.team == "munchkin" or "scout":
if hero.canCast("lightning-bolt") and enemy.health > 70:
hero.cast("lightning-bolt", enemy)
elif hero.canCast("chain-lightning"):
hero.cast("chain-lightning", enemy)
elif hero.canCast("roge"):
hero.cast("roge",enemy)
elif hero.canCast("root"):
hero.cast("root",enemy)
hero.attack(enemy)
else:
hero.attack(enemy)
else:
hero.attack(enemy)
elif hero.canCast("regen",hero) and hero.health < 70:
hero.cast("regen", hero)
else:
hero.moveXY(5, 33)
while True:
enemy()
I don’t suggest you do while True, I rather prefer hard-coding codes cause the seed is same.
but without repeating he could not execute
1 Like
Your welcome, glad it helped.
It does actually support the findEnemies()
function, I used it.
system
Closed
20
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.