Multiplayer Treasure Grove (Python)... what's wrong?

    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy.pos)
    flag = self.findFlag()
    if flag:
    elif enemy  == True:
        if distance < 30:
            if self.isReady("bash"):
        item = self.findNearestItem()
        position = item.pos
        x = position.x
        y = position.y
        self.moveXY(x, y)

Basically, my knight just ignores the enemy no matter what. Without some sort of “enemy == True” clause though, the code bottoms out after the only enemy dies, and doesn’t pick back up even after the respawn. He chases coins unless there’s a flag down, and even then, the responsiveness to the flag is low (I’m not sure how to break the “else” statement once it’s begun to go for a coin, or if that’s possible).

Note: in the distance variable, enemy needs the .pos or else it bugs, supposedly that’s a known issue.


You need the if enemy check before your try to do self.distanceTo(enemy.pos), or you will be trying to access the position of a None value. Also, you can just do if enemy, you don’t need the == True part. If enemy is None, then if enemy will return False, because None is not truthy.

I don’t think you need the enemy.pos in the distance check any more; are you sure that it was behaving differently without it?


Moving the distance variable fixed the issue, and I no longer need the == True to bypass that problem. The .pos thing is fine without the misplaced variable as well, so goodx2.

Now I’m just trying to determine how to move on with the loop if there is an enemy, but he isn’t within X meters (without having to manually lead him around with a flag).

Edit: I ended up taking out the distance variable and just chunking the code directly into it ala:

elif enemy and self.distanceTo(enemy) < 30:

Seems to work well enough, just need to tune the distance for competitive balance. Thanks for the help though!