Backwoods Treasure - Help in Python



loop:
    
    enemies = self.findEnemies()
    enemyIndex = 0
    
    coins = self.findItems()
    coinIndex = 0
    
    self.moveXY(53, 18)

    while len(enemies) > enemyIndex:
        enemy = enemies[enemyIndex]
        enemyIndex += 1
                
        if enemy:
            
            while enemy.health > 0:
                if self.isReady("power-up"):
                    self.powerUp()
                elif self.isReady("bash"):
                    self.bash(enemy)
                else:
                    self.attack(enemy)
                       
    while len(coins) > coinIndex:
        coin = coins[coinIndex]
        coinIndex += 1
                        
        if enemies:
            break
        if coin:
            self.moveXY(coin.pos.x, coin.pos.y)
                       
                

I’ve been trying to make my hero stop grabbing coin and attack an enemy if there is one but he either attacks only enemies or grabs coins. I’ve tried using distanceto, if not enemy, and a few other attempts. Any ideas?

The code you have above does:

loop
– get enemies and coins
– kill all enemies until there are no more
– (then) pick up all coins until there are no more
– (never loops back to enemies since there are probably always coins)

make it:

loop:
– get Nearest-enemy and Nearest-coin
– if enemy attack
–else if coin pick up

then it will attack if there are enemies and get coins otherwise . . . one at a time

you could also make the first if a “if enemy and distance < #” then it will pick up coins until they get close…

1 Like

my code always says to switch the item x and y to my hero’s x and y, how do i fix it?

We would have to see the code to understand the exact nature of the error?

item.pos.x,
item.x,
which command,
etc…