Mirage Maker help

I have all of my code working. Except at the end I can’t get my warrior to stay in the ambush spot. I have tried “break,” but then my code won’t run.

totalGold = 0
currentHealth = self.health
healthThreashold = self.maxHealth
healthIndex = 0
loop:
    item = self.findNearest(self.findItems())
    enemies = self.findEnemies()
    if item:
        pos = item.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)
        totalGold = totalGold + item.value
    if self.gold >= 25:
        self.buildXY("decoy", 73, 68)
        self.say("here piggy piggie")
        self.say("here piggy piggie")
        self.say("here piggy piggie")
        self.say("here piggy piggie")
        self.say("come get some")
        self.say("you suck")
        self.say("you're slow")
        self.say("and fat")
        self.say("come and get me") 
        self.moveXY(22, 16)

Why does your hero leave the spot? What is causing that piece of code to fire?

Where did you place the “break”? Which piece of code should the break be part of?

Read my guess after you think about the above questions.

You don’t have any attack logic, so it can’t be that, and I don’t think you should be able to see any coins to go collect. So . . . Are you wearing a “Boss Star”?

The break would/should be part of the “if gold” . . . if not indented properly it would not be IN the if and would run the first time through the loop:
Enter loop - if item then get item - not enough gold - BREAK !! Oops.

Hi Vlevo,

I know why my hero is leaving the spot. I have him in a loop. so I know that he will always start to the loop over again. I was hoping that by adding a “break” my warrior would drop out of the loop and stay at the ambush spot.

I’m assuming that I really need a while loop in instead of a loop; however, I can’t ever seem to get the while loops to work correctly. Obviously my understanding of how they work is flawed. I know that a loop goes on indefinitely and a while loop goes on until the condition is met.

I know that I want to collect gold until I reach 25 then build a decoy, so I’m assuming I could have while loop here. I have tried installing the while loop but I’m messing something up.

I’m going to keep twidling away and hopefully I’ll figure something out.

I feel like I am getting much closer.
Now I am getting my hero to do stay in the ambush spot but here he is not staying long enough to lure the ogres away nor is he saying “here little piggy”

totalGold = 0
while self.gold < 25:
 item = self.findNearest(self.findItems()) 
    if item:
        pos = item.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)
        totalGold = totalGold + item.value   

    
if self.gold == 25:
    self.moveXY(72, 68)
    self.buildXY("decoy", 72, 68)
currentHealth = self.maxHealth
while currentHealth == 1800: 
    self.say("here little Piggie")
    currentHealth = currentHealth -1
self.moveXY(22, 15)

I can now get him to call out to the ogre but he is not going to the ambush spot. Dang I feel like I am sooooo close.


totalGold = 0
while self.gold < 25:
    item = self.findNearest(self.findItems()) 
    if item:
        pos = item.pos
        itemX = pos.x
        itemY = pos.y
        self.moveXY(itemX, itemY)
        totalGold = totalGold + item.value   

    
if self.gold == 25:
    self.moveXY(72, 68)
    self.buildXY("decoy", 74, 68)
currentHealth = self.maxHealth
while currentHealth < 1799.57: 
    self.say("here little Piggie")
    currentHealth -= currentHealth + 1
self.moveXY(22, 15)
    


I tried to get my script to do stay at the antagonization spot by adding a bunch of “here little piggies” and I hit submit and now even when I take all the code out and try to run it doesn’t run correctly. :frowning:

Yes, it is in a loop, but he will only go build a decoy if you have >= 25 gold and once you stop collecting coins the only way to do that is with a Boss Star… iow: no boss star no second run to build a decoy.

the break would go here:

if self.gold >= 25:
        self.buildXY("decoy", 73, 68)
        self.say("here piggy piggie")
        #the rest of say commands removed to shorten example
        self.moveXY(22, 16)
        break

Anywhere else and the break won’t work properly.

I see why you have so many say statements. It is hard to guarantee that all the ogres will follow you.

The default code comments suggest that you yell insults until an ogre hits you…
(that is the only legitimate way your health goes down…)

something like:

while self.health == self.maxHealth: 
    self.say("Ayyyyyyyyyy ugly piggy!")     # CodeCombat style
self.moveXY(22, 15)

should do what is intended, of course if you want to run before being hit, insulting until distanceTo nearest ogre < 10, is almost the same.

1 Like

Thank you. That helped a lot. :smile:

I was able to successfully complete the level with the following code. I solved it differently, by using a flag (that can be placed at any time) to trigger the decoy once the proper amount of gold has been collected. However, when i put the value of the gold at + 1, like i have on previous levels, the player would run to the flag once they had collected 21 gold, as apposed to the 25 that is required. When i switched the number to + .5, the player would run to the flag once 25 gold was collected. I’m happy the code worked, but have no idea why this is the case. any ideas?

    totalGold = 0
loop: 
    coin = self.findNearestItem()    
    flag = self.findFlag()
    if coin:
        self.moveXY(coin.pos.x, coin.pos.y)
        totalGold = totalGold + coin.value
        totalGold = totalGold + .5
        if totalGold >= 25:
            if flag:
                self.buildXY("decoy", flag.pos.x, flag.pos.y)
                self.pickUpFlag(flag)
            
            if self.pickUpFlag(flag):
                self.say("This way, losers!")
      
    if self.health < self.maxHealth:
        self.moveXY(22,16)

Why are you trying to keep track of the gold yourself?
Why are you adding some arbitrary amount?
Do you not have a wristwatch that tracks gold for you?

You really can’t keep an accurate count like this, since it only counts the coins you are aiming at and not any you happen to get close enough to to pick up while getting the one you are aiming at…

This is how Code Combat instructed me to collect gold, once i obtained the wristwatch, in a previous level. If you have an answer to my question, i would appreciate it.

Sorry, I kind of did, but you are right it wasn’t very obvious.

You don’t keep track of the number that appears up in the corner, the game does . . . therefore it (the number on display) and when your hero runs to the flag have “little” to do with each other. Your count is just an approximation, which is the reason you are trying to adjust your count by adding extra (+.5, +1) to it.

So it should be easy to see why “totalGold” might hit 25 before the real count does. (for example say you never pick up extra coins . . . then you have +1 per actual coin collected and will hit 25, number of coins early.) That the .5 gets you to what appears to be a matching gold amount is a lucky coincidence of your seed (a different seed and you collect different coins and a different amount of extras, there is almost certainly a seed out there where +1 works and +0.5 doesn’t).

Wow, awesome, thank you. Will definitely save me time in future situations.