Hey guys I need help on hoarding gold with coin.value. This is my code so far and I’m using quartz sense stone. Everytime I use the code it says coin.value is unavailable.
# Use break to stop collecting when totalGold >= 25
totalGold = totalGold + coin.value
loop:
coin = self.findNearestItem()
if coin:
pos = coin.pos
itemX = pos.x
itemY = pos.y
self.moveXY(itemX, itemY)
totalGold = totalGold + 1
# Pick up the coin and add its value to the total.
# Get its value with: coin.value
if totalGold >= 25:
loop:
coin = self.findNearestItem()
if coin:
pos = coin.pos
itemX = pos.x
itemY = pos.y
self.moveXY(itemX, itemY)
totalGold = totalGold + coin.value
totalGold += coin.value
# Pick up the coin and add its value to the total.
# Get its value with: coin.value
if totalGold >= 25:
no. you just need one of those lines. pick one they do the same thing. btw. to preserve spacing of code block put three ` characters on a line of its own both before and after the code block.
# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.
totalGold = 0
loop:
coin = self.findNearestItem()
if coin:
pos = coin.pos
itemX = pos.x
itemY = pos.y
self.moveXY(itemX, itemY)
totalGold = totalGold + coin.value
# Pick up the coin and add its value to the total.
# Get its value with: coin.value
if totalGold >= 25:
self.moveXY(59, 33)
self.say("Done collecting gold")
break
# Go to Naria and say how much gold you collected.
^My code.
It works, but it won’t finish. I go over to Naria and tell her I finished collecting, yet it doesn’t end.
Some help please?
outside of your ‘if’ statement, and also you’ll want to tell Naria how much gold you’ve collected, not just that you’ve finished collecting gold. Make sure you do that.
// Collect 25 gold, and then tell Naria the total.
// Use break to stop collecting when totalGold >= 25.
loop {
var coin = this.findNearest(this.findItems());
// Pick up the coin and add its value to the total.
// Get its value with: coin.value
var cY = coin.pos.y;
var cX = coin.pos.x;
this.moveXY(cX, cY);
var totalGold = 0 + this.gold;
if (totalGold >= 25) {
// >= means totalGold is greater than or equal to 25.
// This breaks out of the loop to run code at the bottom.
break;
}
}
this.say("Done collecting gold!");
// Go to Naria and say how much gold you collected.
this.moveXY(59, 33);
this.say(this.gold);
I didn’t use “coin.value” like the instructions said because that caused an error to pop because I used the word “coin” to define my x and y values for the coins; in coin.pos.x and coin.pos.y. I also used “this.gold” to add to totalGold, for the if statement, which I got from the item description for “gold” under the quartz sense stone. Lastly, I couldn’t get my character to say the right amount of gold when I clicked “submit” to finish the level. I first started using “this.say(“25 gold”);” Since the final submission of your code generates the level at random I would pick up more than 25 gold and the level wouldn’t finish properly. I discovered if you type "this.say(this.gold); your character says whatever amount of gold they have at the time.
var totalGold = 0 + this.gold;
if (totalGold >= 25) {...}
you can directly say
if (this.gold >= 25) {...}
This is a bit better to understand for another person. If I look at code, I always check if/while/for ect. first when debugging, and this way I can see that you use the unmodified goldValue. totalGold can be anything.
But that’s just me nitpicking.
Collect 25 gold, and then tell Naria the total.
Use break to stop collecting when totalGold >= 25.
Having trouble with this mission. Basically my character will collect 24 gold, then go over to Naria and say she has 26 gold. I’ve tried with different variables, and it always ends up being that she says she has 2 additional gold that she does not actually have, UNLESS I say “if total gold = <= 0” then she will walk into the gold between the start and Naria, and say she has 3. Can anyone tell me what is wrong with my code?
totalGold = 0
loop:
# Pick up the coin and add its value to the total.
# Get its value with: coin.value
coin = self.findNearestItem()
totalGold = totalGold + coin.value
if totalGold <= 25:
self.moveXY(coin.pos.x, coin.pos.y)
else:
break
self.say("Done collecting gold!")
self.moveXY(59, 33)
self.say(totalGold)
Fixed it.
You preserve the whitespaces by placing backticks like this (I used a trick to display these backticks, normally they would disappear and be replaced by the codeblock):
You need to collect the last coin instead of breaking when it would bring you over 25, since you already added it to your total. Break after you collect it.
# Collect 25 gold, and then tell Naria the total.
#收集25枚金牌,然后告诉Naria总数。
# Use break to stop collecting when totalGold >= 25.
#用刹车时要停止收集totalGold>=25。
totalGold = 0
loop:
coin = self.findNearestItem()
# Pick up the coin and add its value to the total.
#拾起硬币并添加其总价值。
# Get its value with: coin.value
#获取其值:coin.value
self.moveXY(coin.pos.x, coin.pos.y)
totalGold = self.gold
if totalGold >= 25:
# >= means totalGold is greater than or equal to 25.
# This breaks out of the loop to run code at the bottom.
break
self.say("Done collecting gold!")
# Go to Naria and say how much gold you collected.
self.moveXY(59, 33)
self.say("collect 25 gold")