Wondering what’s wrong with code on level “Shine Getter”. If players got the hang of figuring out different values of coins (gold, silver, bronze), other levels would make a lot more sense.
Code:
coins = self.findItems()
coinIndex = 0
# Wrap this into a loop that iterates over all coins.
coin = coins[coinIndex]
# Gold coins are worth 3.
if coin.value == 3:
# Only pick up gold coins.
pos=coin.pos
x=pos.x
y=pos.y
self.moveXY(x, y)
All my character does is stand there: he’s not interpreting the last
x=pos.x
y=pos.y
self.moveXY(x, y)```
at all. Any help would be appreciated!
I did not do this level yet, and I don’t know this language, but it seems to me that a kind of loop is missing, with something like coinIndex++ somewhere, isn’t it ?
Vettax is right. You check exactly one coin, the one at position 0.
At Shine Getter you should already have encountered iterating over arrays, but for completness:
coins = self.findItems()
coinIndex = 0
while coinIndex < len(coins):
if coin.value == 3:
#collect the coin
coinIndex += 1 #Increase coinIndex in every case
Thanks! I was not sure about the len() command, but I originally tried coinIndex = coinIndex + 1. I tried while coinIndex < 3, but that didn’t work. Thanks for introducing the len() command to me!
Is there a way to differentiate from enemies as well? (and attack them in priority, etc)
Yes, some levels are designed to show you how to select your enemies (do not attack the yak, for example).
Code to attack by priorities is a little bit more complex, but feasible and needed for multiplayer levels for example.
# Wrap this into a loop that iterates over all coins.
while len(coins) > coinIndex:
coin = coins[coinIndex]
coinIndex = coinIndex + 1
# Gold coins are worth 3.
if coin.value == 3:
# Only pick up gold coins.
self.moveXY(coin.pos.x, coin.pos.y)
So, this is my code, which works fine. From the past levels I know the while loop works with len() and then I believe I get the idea of the [coinIndex] and the effect of coinIndex + 1… BUT, I am not sure why they are necessary.
The [coinIndex] indicates where in the array the loop is, right? And the coinIndex + 1 advances it forward to loop again. But why is that necessary here? Why can’t it just say, if a coin has a value of 3, then retrieve it.
Hi. I’m having a little trouble understanding the code.
I’m not sure why code adding a number to the index, such as coinIndex += 1 in this case, should be placed outside the if-condition. From what I understand, aren’t you only supposed to count the gold coins as part of the coinIndex?
Any helpful response will be much appreciated. Thank you.
If you only count coinIndex + 1 when inside the if-condition you would only increment when the coin in question is a goldie. Which in turn means the while-loop would get stuck as soon as it looks at a copper or silver. The loop would look at the coin thinking “Hm this one isn’t worth 3, better not increment coinIndex” and then because coinIndex is still the same, it would take the SAME coin again, thinking the SAME thing again. And so on. In infinity.
So the +1 has to be outside the “if” for the loop to actually look at all the coins, and also to let the loop fulfill its quitting condition. The condition you set is “while len(coins) > coinIndex:” meaning it will only quit when it has counted up to the total number of coins. And if you only count the goldies it will be impossible to reach this condition.
But I guess the second part is irrelevant because it would be stuck already. Confusing stuff. Hope this works as some sort of explanation.
Hey Guys, i’ve also got an issue here, So i’ve updated my boots and now i can use move(item.pos);
however, my dear hero doesn’t like it and starts moving around, in the end not grabing any coin
i’ve got something that looks like a GIF http://i.imgur.com/nAHVM3x.gif, basically he stands in place trying to find a better coin
here’s my code`
loop {
var coins = this.findItems();
var coinIndex = 0;
// Wrap this into a loop that iterates over all coins.
for (i=0; i < coins.length; i++) {
var coin = coins[i];
// Gold coins are worth 3.
if (coin.value == 3) {
// Only pick up gold coins.
this.move(coin.pos);
}
}
}
Sorry, I should have been more clear. You’ll notice that in the previous version of your code you had two loops, a while-loop and a loop: command. My intention was for you to move the line coins=self.findItems() inside the loop:, not inside the while-loop.
# Wrap this into a loop that iterates over all coins.
while coinIndex < len(coins):
if coin.value == 3:
coinIndex += 1
pos = coin.pos
X = pos.x
Y = pos.y
self.moveXY(X, Y)
I’ve been trying for like 20 minutes, keeps telling me that coin is not defined.