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)
It’s a new skill on the higher-level glasses, which we introduced for Adventurers for the Boulder Forest level yesterday, and which will be also explained in a mountain level next week. Jacob, do you have the higher-level glasses equipped?
Hmm, I don’t understand this. I had to state the variables “coin”, “pos”, “x”, and “y” at the beginning of the loop, because if they were only stated in my ‘while-loop’ then they would come up as null sometimes and break my code. The isPathClear suddenly started to work now, but now my code runs through and works but keeps giving me an error saying “pos” on line 7 is undefined. Also, when he picks up all of the items in the first grove, he just stands there and waits, he doesn’t move to (28, 28) like he should.
loop:
enemies = self.findEnemies()
enemyIndex = 0
minDistance = 10
coins = self.findItems()
coin = self.findNearest(coins)
pos = coin.pos
x = pos.x
y = pos.y
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) < 7:
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) < 7:
self.moveXY(x, y)
# Else if there are not enough enemies, we walk to the new grove.
elif len(enemies) < 7:
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)
Having figured that out I realized that you aren’t using the function correctly. So, what it is doing is telling you that there is no, “isPathClear(pos)” function . . . the function is:
isPathClear(start,end)
iow, it doesn’t assume you always mean from my position…
ok i get you the tooltip is saying find the clearance between two positions each with their own xy. the next step i guess would be to figure out how to find the XY of a path with no handle. the tooltip shows how to find the handle of an item but not “10 metres in front”. probably im misusing this method and making this beginner level harder than it needs to be.
but you have the pieces above for what I’m guessing you mean…
You know where you are, this.pos, and you know where you want to go, 20 meters in front of you, so, just add in yourself as the start…???..
var path_clear = this.isPathClear(this.pos, {x: this.pos.x + 20, y: this.pos.y});
That is how you would get to some arbitrary point (of course you have to figure out the proper angle if it wasn’t some straight X or Y direction, but there are Math functions for that (and Vector stuff in javascript)
By the way, there is a level specific workaround if you want isPathClear:
Note that level consists of three chambers. Inside these chambers path is always clear, between chambers it is usually not.
Let point in the center (junction between chambers) have coordinates (CENTER_X, CENTER_Y) Use something like:
Well after tinkering with the code, I got through this.
loop:
enemy = self.findNearestEnemy()
item = self.findNearestItem()
flag = self.findFlag(“green”)
if item:
pos = item.pos
x = pos.x
y = pos.y
self.moveXY(x, y)
if flag:
self.pickUpFlag(flag)
elif self.isReady(“cleave”):
self.cleave(enemy)
elif enemy:
self.attack(enemy)
Right, but it says “Line 20: tmp41 [tmp42] is not a function” what does that mean?