I wrote the code and added comments to make it clearer. I needed to add the condition that we only walk to our desired coin if there was a clear path, because otherwise I pick up all the coins in my area and then walk into the wall. So if there is not a clear path, I make my character walk into the next grove. But I get an error for if self.isPathClear(self.pos, pos) as “isPathClear is not defined”. I can’t seem to fix it.
loop: enemies = self.findEnemies() enemyIndex = 0 minDistance = 10 coins = self.findItems() coinIndex = 0 # While there are less than 5 enemies, we cycle through each coin and determine the closest one. We rename it "closestCoin" while coinIndex < len(coins) and len(enemies) < 6: coin = coins[coinIndex] distance = self.distanceTo(coin) coinIndex += 1 if coin.value >= 2 and distance < minDistance: minDistance = distance closestCoin = coin pos = closestCoin.pos x = pos.x y = pos.y # If there is a clear path to "closestCoin" and not enough enemies, we walk there. if self.isPathClear(self.pos, pos) and len(enemies) < 6: self.moveXY(x, y) # Else if there are not enough enemies, we walk to the new grove. elif len(enemies) < 6: self.moveXY(28, 28) pass #Otherwise, we kill the closest enemy. else: enemy = self.findNearest(enemies) while enemy.health > 0: if self.isReady("power-up"): self.powerUp() else: self.attack(enemy)