On Sarven Brawl For Python


#1

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)

#2

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


#3

still saying the same thing


#4

whats your code now?


#5
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)

#6

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


#7

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!


#8

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)

#9

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


#10

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);
}
}


#11

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


#12

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)

#13

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


#14

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()


#15

@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.


#16

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.


#18

@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)


#19

@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.


#20

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


#21

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

so like this:

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