Help stuck on mad maxer

i need help on mad maxer my guy attacks the decoys and also attacks the enemys but goes into a corner
this is my code

enemies = hero.findEnemies()
enemy = hero.findNearestEnemy()

while enemyIndex < len(enemies):
    target = enemies[enemyIndex]
    enemyIndex = enemyIndex+1
    # Is this enemy farther than the farthest we've seen so far?
    distance = hero.distanceTo(target)
    if distance > maxDistance:
        maxDistance = distance
        farthest = target
    hero.moveXY(34, 37)
    hero.moveXY(28, 18)
    hero.attack(enemy)
    hero.attack(enemy)
    hero.attack(enemy)
    if farthest:
        hero.attack(farthest)
        if enemy==wizard:
            hero.attack(enemy)
            hero.attack(enemy)
        

Welcome to the forum @Logan_Jordan1 ! :partying_face: This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders! Have a great time!

Thank you for formatting your code :slight_smile:

I think we can fix some things:

  • put the whole thing in a while true loop
  • define what enemyIndex, farthest, and what maxDistance are in before the while loop that loops over all the enemies
  • i suggest you remove both move commands and the 3 attack commands
  • inside the if farthest, add a while loop checking if the health if farthest is greater than 0
  • the if enemy == wizard should be removed with the lines inside it

thankyou i will deffinately do so

farthest = None
maxDistance = 0
enemyIndex = 0
enemies = hero.findEnemies()
enemy = hero.findNearestEnemy()
 
target = enemies[farthest]
enemyIndex = enemyIndex+1
    # Is this enemy farther than the farthest we've seen so far?
distance = hero.distanceTo(target)
while True:
    
    if distance > maxDistance:
        maxDistance = distance
        farthest = target
    
    if farthest:
        hero.attack(farthest)
        if enemy==wizard:
            hero.attack(enemy)
            hero.attack(enemy)
        
this is my updated code any other suggestions

my character just goes to the corner and does not do anything

I suggest you put the WHOLE thing inside the while true loop, and keep the while loop you already had, like this for example:

while True:
    enemies = hero.findEnemies()
    enemyIndex = 0
    while enemyIndex < len(enemies)
        #Do something here

And i believe that this part should be edited to this:

if farthest:
    while farthest.health > 0:
        hero.attack(furthest)
farthest = target
maxDistance = 0
enemyIndex = 0
enemies = hero.findEnemies()
enemy = hero.findNearestEnemy()
while True:
     
    target = enemies[farthest]
    enemyIndex = enemyIndex+0
    # Is this enemy farther than the farthest we've seen so far?
    if enemy:
        if target: distance = hero.distanceTo(target)
    while True:
        
        if distance > maxDistance:
            maxDistance = distance
            farthest = target
            
        if farthest:
            while farthest.health > 0:
                hero.attack(furthest)
        this is my new code but my guy wont stop running into the wall and not fighting

i will start from scratch nevermind but if i have any questions i will put them in here

hmmm this is my new code but he attacks all the decoys

    maxDistance = 0
    enemyIndex = 0
    enemies = hero.findEnemies()
    
    # Look at all the enemies to figure out which one is farthest away.
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1
        
        # Is this enemy farther than the farthest we've seen so far?
        distance = hero.distanceTo(target)
        if distance > maxDistance:
            maxDistance = distance
            farthest = target
    
    if farthest:
        # Take out the farthest enemy!
        # Keep attacking the enemy while its health is greater than 0.
        enemy = hero.findNearestEnemy()
        while enemy.health > 0:
            hero.attack(enemy)
        pass

This works really well, except put EVERYTHING in a while true loop

and here, just remove the line where you define enemy, and instead of enemy.health let it be farthest.health and hero.attack(farthest)

ok thankyou i will do that

this is my new code he attacks the flying person then the decoys push him to the wall any thing else

maxDistance = 0
enemyIndex = 0
enemies = hero.findEnemies()
while True:
        
    # Look at all the enemies to figure out which one is farthest away.
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1
        
        # Is this enemy farther than the farthest we've seen so far?
        distance = hero.distanceTo(target)
        if distance > maxDistance:
            maxDistance = distance
            farthest = target
    
    if farthest:
        # Take out the farthest enemy!
        # Keep attacking the enemy while its health is greater than 0.
        while farthest.health > 0:
            hero.attack(farthest)
            
        pass

Just put those right under the while True: and you are good to go

1 Like

ok i will do so your wish is my command

1 Like

Did you pass the level?

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.