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

SARVEN DESERT: Help with Sarven Brawl using the Python Coding Language

It is saying ‘enemy’ is null

loop:
    enemy = self.findNearestEnemy()
    enemyindex = 0
    while enemyindex < 100:
        if enemy.type != "sand-yak":
            while enemy.health > 0:
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        elif self.findNearestEnemy():
            self.moveXY(93, 70)

check for “enemy is ‘null’” in this forum, but to make it short:

if enemy:
   do sth
else:
  do sth else

means that you first have to check if there is an enemy. That means when computer runs your cude and asks for

if enemy.type != "sand-yak":

it isnt for sure that there is an enemy, and he cant find the type of an enemy who is not there. So first chek for enemys

still saying the same thing

whats your code now?

loop:
    enemyindex = 0
    while enemyindex < 100:
        enemy = self.findNearest(self.findEnemies())
        if enemy.type != "sand-yak" and enemy.health > 0:
                if self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
        elif self.findNearest(self.findEnemies()):
            self.moveXY(93, 70)

How can you say “still” when you didn’t make the change he suggested.

you do not ask if there generally is an enemy. but i dont want to give you the right answer, because i want to find the solution on your own, because this is really importent!

I did this code, and it WORKED!!

loop:
    enemy = self.findNearest(self.findEnemies())
    if enemy and enemy.type != "sand-yak":
        self.attack(enemy)
    else:
        self.moveXY(91, 71)
2 Likes

jeah, thats the way you go. now if you want to, you can reuse your code from above and bash for example instead of just attacking :wink: but i think in this level its overrated

if you are very low health you can run away from the enemy by using the flag to running out of two minutes.

here the example code


loop{
var enemy = this.findNearestEnemy();
var flag = this.findFlag("green");

if(flag){
    this.pickUpFlag(flag); 
    var pos = flag.pos;
    var x = pos.x;
    var y = pos.y;
    this.moveXY(x, y);
}

if(enemy && enemy.type != "sand-yak"){
    var dis = this.distanceTo(enemy);
    if(this.isReady("cleave")){
    this.cleave(enemy);
    }  
    else {
        this.attack(enemy);
    }
}
else {
    this.moveXY(91, 71);
}
}

I had the same problem i used a selfwait for 1 sec and then the code found the enemies I think they need time to appear

Hello, I have a problem with my code. My hero does not attack the office, I do not understand why ?

loop:

enemy = self.findNearest(self.findEnemies())
    if enemy:
        if enemy.type is "sand-yak":
            pass
        else:
            if enemy.health>0:
                if self.cast("chain-lightning", enemy):
                    self.cast("chain-lightning", enemy)
                if self.isReady("bash"):
                    self.bash(enemy)
                if self.canElectrocute(enemy):
                    self.electrocute(enemy)
                else:
                    self.attack(enemy)
    self.moveXY(63, 72)

i used the flags to retreat as you said, but my hero ignored the flag!

Here’s my code (Python) my hero has 1475 hp but ends with 755 hp.

while True:
enemy = hero.findNearestEnemy()
if enemy and enemy.type != “sand-yak”:
hero.attack(enemy)
else:
hero.shield()

@Mr_NotSoPerfect, how do you end with 755 hp? Is it -755? Or 755 hp? If it is 755 with out the minus, it might just be a glitch.

I checked back at the code, and hero, my hero actually has 1471, and im guessing its the second round (the title has a small 1 next to it) and it says success, with 991 hp left & 1 enemy left too.

Hero:

Code:

while True:
enemy = hero.findNearestEnemy()
if enemy and enemy.type != “sand-yak”:
hero.attack(enemy)
else:
hero.shield()

Hopefully this answers your question! If not reply back to me and I will check for any errors that you find.

@Emblu056 I cant submit the run because it has not been a day yet, but here is my hero and code and i ran it twice and both times it said it was successful. Your hero does run into the wall a bunch of times, but the code works.

Hero:

Code:
while True:
enemies = self.findEnemies()
enemyIndex = 0
while enemyIndex < len(enemies):
Target = enemies[enemyIndex]
enemyIndex += 1
enemy = self.findNearestEnemy()
if enemy.type == “sand-yak”:
self.moveXY(self.pos.x -5, self.pos.y)
if enemy.type != “sand-yak”:
self.attack(enemy)
else:
self.attack(enemy)

1 Like

@Mr_NotSoPerfect I tried both codes and they both didn’t work. Maybe you need to cleave. Try: self. cleave(enemy). If you have the sword. For the last code you gave me, the hero did nothing and just stood there.

@Emblu056 did you put in the indents? and what does your hero look like?

move enemy = self.findNearestEnemy() in the while enemyindex < 100

so like this:

loop:
enemy = self.findNearestEnemy()
enemyindex = 0
while enemyindex < 100:
enemy = self.findNearestEnemy()