Level: Leave It To Cleaver Help (solved)

so i did everything but in the end of fighting ogres a ogre pops up when theres like 3 seconds left and my code at that time is running the code find nearest enemy plz help

This shows how to define a function called cleaveWhenClose

The function defines a parameter called target

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
ready = hero.isReady(“cleave”)
hero.cleave(enemy)
else:
enemy = hero.findNearestEnemy()
hero.attack(enemy)

This code is not part of the function.

while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy))

plzzzzzzzzzzzz help pulzzzzzzzzzzzzzzzz

Hello,

To help us see the formatting of the code, please review the link below to show you how to post your code with the formatting. That being said, I see a problem with your code.

ready = hero.isReady(“cleave”) # this should be an if statement not a variable

Your hero is waiting until the cleave is ready instead of attacking the nearest enemy.

Also, you should be using the variable target and not enemy in the function since that is the parameter provided.

hero.cleave(target)
hero.attack(target)

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
if enemy:
hero.cleave(enemy)
if enemy:
hero.attack(enemy)

This code is not part of the function.

while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)
if enemy:
hero.attack(enemy)

Hi, if you need help with your code, please post it formatted:

Danny

here is the code properly formatted

def cleaveWhenClose(target):
if hero.distanceTo(target) < 5:
pass
# Put your attack code here
# If cleave is ready, then cleave target
if enemy:
hero.cleave(enemy)
if enemy:
hero.attack(enemy)

This code is not part of the function.
while True:
enemy = hero.findNearestEnemy()
if enemy:
# Note that inside cleaveWhenClose, we refer to the enemy as target.
cleaveWhenClose(enemy)
if enemy:
hero.attack(enemy)

You don’t have any indentation there, and I’d prefer the person to do it themselves so they can do it in the future.
Danny

i’m using python cause mah robotics using python and im stuck too and u can’t use just else: but if u use if/else:, it will become if enemy: meaning if there is enemy and ur hero attacks it

Assuming I understand your statement…you are stuck, because you are not using ‘else’ properly, right?

‘if’ is used to qualify a statement as either true, or false. ‘else’ is the false clause:

if conditions are true:
    do this code
else:
    do that code

Where is the problem in that?

Continuing the discussion from Level: Leave It To Cleaver Help (solved):

Hi, I used the below code and it keeps waiting for the cleave to load instead of going back to attacking the nearest enemy. What’s wrong with my code? Thanks in advance.

# The function defines a parameter called `target`
def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        ready = hero.isReady("cleave")
        hero.cleave(target)
        # else, just attack `target`!
        hero.attack(target)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy)

Hi @aweijie and welcome to the forum. You should put

if ready:
    #cleave the enemy
else:
    #attack the enemy

Thanks @AnSeDra!
Will try it out!

can someone please help me with my code

def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        if enemy:
            hero.isReady("cleave")
            hero.cleave(enemy)
        # else, just attack `target`!
    else:
        hero.attack(enemy)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy)

My hero would fight through until the last 21 seconds the hero stops I don’t know what is wrong with my code

Isn’t actually doing anything thing…it needs to be an ‘if’ statement. This comment is helping to guide you:

1 Like

thanks
@dedreous

1 Like

I think I didn’t quite catch what you have said I tried doin the hero.cleave (enemy) by it self and still didn’t get anywhere could you help me again sorry if I too much of a bother here is my new code

def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        if enemy:
            hero.cleave(enemy)
        # else, just attack `target`!
    else:
        hero.attack(enemy)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy)

Well, not quite there yet. If you don’t test to see if the hero is ready to cleave, then he will attempt to do so, ready or not. This means he may just stand still, until the cooldown period ends.

I just noticed something else too. You are passing the object ‘target’ to your function, but then don’t use it. ‘target’ is actually the enemy you want to be working with in the code. You don’t need to test for enemy again, as you’ve already done that in the code that is calling this function.

Here’s an outline (not code) of how it might look:

def cleaveWhenClose(target):
    if hero is closer than 5 meters to the target, then:
        if hero is ready to cleave the target, then:
            cleave the target!
        otherwise:
            kill the target!
2 Likes

@dedreous
I tried that and still near the end there are 3 ogre popping out and my hero won’t go to defeat them and then it would just stop

def cleaveWhenClose(target):
    if hero.distanceTo(target) < 5:
        pass
        # Put your attack code here
        # If cleave is ready, then cleave target
        if enemy:
            hero.cleave(enemy)
        # else, just attack `target`!
    else:
        hero.attack(enemy)

# This code is not part of the function.
while True:
    enemy = hero.findNearestEnemy()

    if enemy:
        # Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy)

What should I do now?

Umm…where did you make changes? I still see:

a lot of references to ‘enemy’ an you are still not testing to see if he is ready to cleave.

1 Like