Hi, I am struggling on how to get started on this level. I have no idea what to do. This is the given code:
# This field is covered in firetraps. Thankfully we've sent a scout ahead to find a path. He left coins along the path so that if we always stick to the nearest coin, we'll avoid the traps.
# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
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
# Set nearest to coin
# Set nearestDistance to 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.
OK heres the gist.
Loop thru all the coins.
If the coins distance to you is smaller then nearest distance.
Make nearestDistance to the distance, and make nearest to the coin.
So lets say you start.
Coin A is close then 9999.
So you pick it up.
Then,since COIN A is gone, you get coinB.
If its closer then 9999:
Pick it up.
Its kinda like finding the weakest enemy.
You make weakest to none, and make weakest health to 9999
Then, loop thru all enemies, and if its health is less then weakest, make it weakest.
Lets say, there is an ogre, a shaman, and a thrower.
120 is less then 9999 so weakestHealth is 120, and ogre is weakest. Then, you find the shaman in the loop.
60 is less then 120, so weakestHealth is 60, and the shaman is weakest.
Then, you find the thrower.
7 is less then 60, so then weakestHealth is 7, and thrower is weakest.
If the game spawns an enemy thatās health is less then 7, its weakest enemy.
Here is some code for getting the weakest enemy, and it might give you some ideas for something similar for the NEAREST item.
while True:
weakest = None
leastHealth = 99999
enemies = hero.findEnemies()
for enemy in enemies:
if enemy.health < leastHealth:
weakest = enemy
lowestHealth = enemy.health
# Loop through all enemies.
if weakest:
hero.attack(weakest)
Then, for the items, just do it with distance, and do items instead of enemies.
I hope this helps.
the instructions are basically telling you write code that finds out if the coin is the closest.
like imagine coinA, coinB, and coinC
coinA is 5 m away, coinB is 3 m away, and coinC is 13 m away
you have to cycle through every coin and see which is the closest.
nearestDistance = 999
nearest = 0
coinA distance < nearestDistance so coinA distance becomes the new nearest distance and coinA becomes nearest
nearestDistance = 5
coinB distance < nearestDistance so coinB becomes nearest and repeat.
So basically, youāre looping through all the coins to find the nearest coin.
They gave you the nearest as None and nearestDistance at 9999 (cause its just a starting distance and every coinās distance would be lower than that)
So here itās telling you, if the distance to the coin is less than the nearestDistance, do nearest = coin. And do nearestDistance = distance.
If there is a nearest, use moxeXY to move to the nearest coin
Iām still struggling on this code. I think Iāve got most of it, but there are two spots I canāt figure it out. Hereās my current code:
# You'll need to find the nearest coins on your own.
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 item.distance < nearestDistance:
nearest = item
nearestDistance = item.distance
# Set nearest to coin
# Set nearestDistance to 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.
hero.moveXY(item.pos.x, item.pos.y)
I donāt get what to do right before the given code, when it says You'll need to find the nearest coins on your own. And I also donāt understand what to do when it says # If this coin's distance is less than the nearestDistance
I switched it out with coins, and now my hero moves. She picks up 1 coin, and blows up.
This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
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 coin.distance < nearestDistance:
nearest = coin
nearestDistance = coin.distance
# Set nearest to coin
# Set nearestDistance to 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.
hero.moveXY(coin.pos.x, coin.pos.y)
I am doing exactly what you are telling me, and I donāt move.
# This canyon seems to interfere with your findNearest glasses!
# You'll need to find the nearest coins on your own.
hero.findItems()
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 coin.distance < nearestDistance:
nearest = coin
nearestDistance = coin.distance
hero.moveXY(nearest.pos.x, nearest.pos.y)
# Set nearest to coin
# Set nearestDistance to 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.