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

[Solved] Pls help in Sand Snake

I really need help I can’t figure it out I’ve worked on it for a long time and it still doesn’t work here is my code:

while True:
    farthest = None
    maxDistance = 0
    enemyIndex = 0
    enemies = hero.findEnemies()
    enemy=hero.findNearestEnemy()
    # Look at all the enemies to figure out which one is farthest away.
    while enemyIndex < len(enemies):
        target = enemies[enemyIndex]
        enemyIndex += 1
        # Is this enemy farther than the farthest we've seen so far?
        distance = hero.distanceTo(target)
        if distance > maxDistance:
            maxDistance = distance
            farthest = target
    if farthest:
        # Take out the farthest enemy!
        # Keep attacking the enemy while its health is greater than 0.
        if farthest.health>0:
            hero.attack(farthest)
        pass

Are you sure that is the code?
image
Sand snakes look like this.
Lydia

whoops wrong code sorry let me repost it

while True:
coins = hero.findItems()
coinIndex = 0
nearest = None
nearestDistance = 9999

# Loop through all the coins to find the nearest one.
while coinIndex < len(coins):
    coin = coins[coinIndex]
    coinIndex += 1
    distance = hero.distanceTo(coin)
    # If this coin's distance is less than the nearestDistance
    if self.distanceTo(coin) < nearestDistance:
        # Set nearest to coin
        nearest = coin
        # Set nearestDistance to distance
        nearestDistance = distance
        # If there's a nearest coin, move to its position. You'll need moveXY so you don't cut corners and hit a trap.
if nearest:
    hero.moveXY(nearest.pos.x, nearest.pos.y)

Just indent everything by 1 from coins = hero.findItems() to hero.moveXY(nearest.pos.x, nearest.pos.y)
Lydia

Thanks it worked :grinning: :grinning:

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.