My code for "Clash of Clones" in Python is being wonky and I don't know why


#1
hero.moveXY(66, 90)
hero.moveXY(82, 71)

def defeatAllEnemies():
    enemy = hero.findNearestEnemy()
    if enemy:
        if enemy and enemy.type == "anya":
            if hero.isReady("invisibility"):
                hero.cast("invisibility", hero)
                if hero.canCast("chain-lightning", enemy):
                    hero.cast("chain-lightning", enemy)
                else:
                    hero.attack(enemy)
                    hero.shield()
        if enemy and enemy.type == "archer":
            hero.attack(enemy)
            hero.shield
        if enemy.type != "sand-yak" or "anya" or "archer":
            if hero.isReady("invisibility"):
                hero.cast("invisibility", hero)
                hero.attack(enemy)
            else:
                if hero.isReady("bash"):
                    hero.bash(enemy)
                else:
                    if hero.isReady("electrocute"):
                        hero.electrocute(enemy)
                    else:
                        hero.attack(enemy)
                        hero.shield()

while True:
    defeatAllEnemies()
    if hero.health <= 50:
        targetPos = (20, 101)
        hero.moveXY(targetPos)

This is my code. After she attacks one archer, she stops attacking. I have no clue why. Could someone help?


#2

Anaya is type “captain” not “anaya”


#3

She attacks the clone when I use “Anya,” but she’s too far away to be the nearest enemy.
Now she attacks two archers and stops.


#4

What is your equipment?


#5

I’m using:
Steel Striker
Runesword
Enchanted Lenses
Sapphire Sense Stone
Emperor’s Gloves
Engraved Wristwatch
Boots of Leaping
The Precious
Steel Ring
Basic Flags (I’m not using them in this level though)
Programmaticon III


#6

I’m guessing you’re trying the level with them, @Enderlord832?


#7

It’s a structural problem. Try using elif statements on all except the last else statement and line them all up under if hero.isReady("invisibility"):


#8

Ok thanks, MunkeyShynes


#9

Also, try removing the first if enemy: in your function and tab everything over to the left. It’s redundant and unnecessary.

code should look more like this:

if enemy and enemy.type is something:
   do this
elif something:
    do this
elif something:
    do this
else:
    do this

What you have is more like this:

if enemy and enemy.type is something:
   do this
   else:
        if something:
            do this
        else:
            if something:
                do this
            else:
                if something:
                     do this

See the difference?


#10

Waiting for the level to load…


#11

Ok it’s getting better but I don’t want my hero to focus on the other clone until the end when she can chain-lightning her. How would I do that with my current code?

hero.moveXY(66, 90)
hero.moveXY(82, 71)

def defeatAllEnemies():
    enemy = hero.findNearestEnemy()
    if enemy and enemy.type == "anya":
        if hero.isReady("invisibility"):
            hero.cast("invisibility", hero)
            if hero.canCast("chain-lightning", enemy):
                hero.cast("chain-lightning", enemy)
            else:
                hero.attack(enemy)
                hero.shield()
                if enemy.health <= 0:
                    enemy = hero.findNearestEnemy()
    if enemy and enemy.type == "archer":
        if hero.canCast("chain-lightning", enemy):
            hero.cast("chain-lightning", enemy)
        else:
            hero.attack(enemy)
            hero.shield
        if enemy.health <= 0:
            enemy = hero.findNearestEnemy()
    if enemy.type != "sand-yak" or "anya" or "archer":
        if hero.isReady("invisibility"):
            hero.cast("invisibility", hero)
            hero.attack(enemy)
        elif hero.isReady("bash"):
            hero.bash(enemy)
        elif hero.isReady("electrocute"):
            hero.electrocute(enemy)
        else:
            hero.attack(enemy)
            hero.shield()
            if enemy.health <= 0:
                enemy = hero.findNearestEnemy()

while True:
    defeatAllEnemies()
    if hero.health <= 50:
        targetPos = (20, 101)
        hero.moveXY(targetPos)


#12

Two/three more hits and the clone would be dead. TWO/THREE.


#13

BookFanatic, I think a better idea would be to: remove your sword, and de-armor yourself, so the clone will be weaker and you can 1 hit them with a chain-lightning.

If I were you I’d just go behind the archers, chain-lightning them, then watch the show. Of course, you have to disarm the clone in order to just stand there and do nothing (equip a simple sword or something)


#14

But then that also leaves me more open to the ogres, who have more poweful hits

(I think I’m used to one-shotting things since I’ve had the Runesword since the dungeon


#15

*powerful

Spelling nerd strikes again


#16

Plus the ). Idk why I have to correct it but I do XD


#17

SuperSmacker is saying to hide behind your troops and let them do the dirty work. This level isn’t about strength, it’s about strategy.


#18

Which in this case isn’t my strong suit

In others, it is, but not here.

Sigh


#19

After you kill the enemy’s archers to destroy the bulk of their firepower, that is


#20

That’s the first thing she does, that’s the point of the first 2 lines of code. The nearest enemy is then an archer.