Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Desert Combat [Python Help]

#1

This is not working, and I don’t understand.

ordersGiven = 0

while ordersGiven < 5:
    self.say("Attack!")
    x = self.pos.x
    y = self.pos.y - 10
    self.moveXY(x, y)
    ordersGiven = ordersGiven+1.

while ordersGiven>=5:
    ogre = hero.findNearestEnemy()
    ogre == "palisade"
    while True:
        enemy = hero.findNearestEnemy()
        enemy == "ogre"
        if ogre:
            hero.attack(ogre)
        if hero.isReady("cleave"):
            hero.cleave(ogre)
        if enemy:
            hero.attack(enemy)
        if hero.isReady("cleave"):
            hero.cleave(enemy)
#2

To command all of the soldiers, move the self.say("Attack!") after you move.

When you are using == it is checking for a comparison and should have an if statement to validate True or False. Are you trying to see if ogre (nearest Enemy) is a palisade? For this, you need to check the ogre.type. If you want to see if it is not equal use != instead.

    ogre == "palisade"
        enemy == "ogre"

Hint: This level doesn’t need all the options you have coded. Also, replace the while ordersGiven>=5 with the while True. The first loop will end with the < 5 and you can keep looping until you defeat the ogre.

#3

I’m confused. Also, this whole level doesn’t make sense to me at all.
And finally, I am able to actually talk now since it muted me for talking too much. Lol

#4

The first section of code that gives the commands works, but it doesn’t get to the last soldier since you say attack before moving. If you move the self.say("Attack!") after the self.moveXY() you will also get the last soldier.

The ordersGiven is to make sure you don’t get stuck in the first loop. You don’t need to use that any more after that.

In this level, the “palisade” is an enemy and can be found with hero.findNearestEnemy() so all you really need to do at this point is loop through finding the enemy and attacking. You don’t have to separate them out.

If you want to identify them by type you need to use the enemy.type == "palisade" or `enemy.type == “ogre”. If you click on the enemy, you can see what their type is at the bottom and in this level, he is actually a “brawler”.

More detailed look if the explanation doesn't make sense
while True:
    enemy = hero.findNearestEnemy()
    if enemy and enemy.type == "brawler":
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)
    elif enemy and enemy.type == "palisade":
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        else:
            hero.attack(enemy)