Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Multiplayer Treasure grove (Python) issue [Solved]

So I tried the following code in the level in the title, and it didn’t work, can someone please help me? I thought I could use this to single out only gold and silver coins, but my hero isn’t moving at all

# Be the first to 100 gold!
# If you are defeated, you will respawn at 67% gold.

while True:
    items = hero.findItems()
    enemy = hero.findNearestEnemy()
    index = 0
    enemyDistance = hero.distanceTo(enemy)
    
    
    
    if  enemy and enemyDistance < 10:
        hero.electrocute(enemy)
        hero.attack(enemy)
    
       itemValue = items[index].value
       hero.say(itemValue)
       #Says Item Value

    if items and itemValue >= 2: <------ So for the reasons commented, I concluded this is the problem
        hero.say(items[index].value)
        #Doesn't Say Item value
        hero.moveXY(items[index].pos.x, items[index].pos.y)
        #Doesn't Move
    index = index + 1

So did I write something wrong in the condition, I get no errors, only the hero standing still

Instead of items = hero.findItems try coin = hero.findItems

2 Likes

tried it same issue, I just changed variable names, which I think doesn’t matter?? Not sure

There are a few issues with this. Firstly, you need another loop if you want to loop through the coins. At the moment you’re setting index to 0 at the start of the loop, then adding 1 to it at the end, but straight after that the loop will repeat and index will go back to 0. You need to add a while loop like you do in the desert coin levels(e.g. Madmaxer levels). (If you’ve done them).
The reason this bit isn’t working:

must be because items[0] (which is what index always is in your code) must be either bronze or silver (value < 3), so the code never runs.
Danny

4 Likes

Oh, thanks so much, I didn’t notice, so now if he cant find a siler or gold coin, he stand still, should I make him move around in circles to find coins or use flags?

1 Like

So tht didn’t work I was hoping this new code would but the hero didn’t move

index = 0
items = hero.findItems()


def shouldPickUp(coin):
    if coin.value >= 2:
        return True
    elif coin.value == 1:
        return False
    else:
        return "None"

while True:
    #  Find coins and/or attack the enemy.
    # Use flags and your special moves to win!
    coins = items[index]
    
    ShouldPickUp = shouldPickUp(coins)
    
    if shouldPickUp == True:
        hero.moveXY(coins.pos.x, coins.pos.y)
        index += 1
    elif shouldPickUp == False:
        index += 1
    elif shouldPickUp == "None":
        index += 1

why is this one not working?

Your still not doing coin = hero.findItems

what is the difference?

1 Like

If you’re not finding the coins first you can’t pick them up.

1 Like

this should be coins = hero.findItems()

@Ankh_Coding Also, you should use:

coin=hero.findNearest(coins)

on coins to transfer it from an array of items(which, in this case will only contain coins), to the nearest one, and then place an

if coin:
    

statement whenever you are picking up the coin or finding its value, so your code will never throw the error “cannot read property x of null,” nor the same for the property y or the property value.
Also, for this:

it will not move, because you are asking it to move to the x and y position of an array of multiple items, which this action is not performed, as this is impossible, nor will it say the value of such array, because you are asking it the value of items[index], which;
a. is an array length, and thus just a number
b. defined as 0 at the start of each loop through the while True loop you are using, because you still have the

in your code.

@Ankh_Coding In short, there are several errors remaining unfixed, even in your new code.
Hope this helps!

2 Likes

so then how do I recall the second entry in an array, oh and the index = 0 is outside the while true loop

So findNearest(coins) finds the nearest from the array, but that isn’t what I wanted, I wanted it to cycle through the array so that it doesn’t get stuck on a bronze coin

@enPointe77 @thebagel

NeverMind I got it now I used the following code:

REMOVED BASED ON SUGGESTION

2 Likes

Good job, @Ankh_Coding!

1 Like

If it works please delete it.

3 Likes

Yes, edit the post and delete the code

2 Likes

Or else others can copy it and say it is theirs.

1 Like

@thebagel @098765432123

I deleted it, but it would only work if you had the infinite range glasses anyways

2 Likes

Yes, but the infinite range and see through walls glasses (midnight glasses) are always a good aquisation