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

Hunters and prey help

Trying to beat “Hunters and Prey”, but my archers always move when there’s an enemy, even if the distance is move than 25. Help please! Here is my code:

Ogres are trying to take out your reindeer!

Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
# Collect coins.
item = hero.findNearestItem()
if item:
hero.moveXY(item.pos.x, item.pos.y)

def summonTroops():
# Summon soldiers if you have the gold.
if >= 20:

This function has an argument named soldier.

Arguments are like variables.

The value of an argument is determined when the function is called.

def commandSoldier(soldier):
# Soldiers should attack enemies.
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(friend, “attack”, enemy)

Write a commandArcher function to tell your archers what to do!

It should take one argument that will represent the archer passed to the function when it’s called.

Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.

def commandArcher(archer):
enemy = hero.findNearestEnemy()
if enemy:
archerDistance = archer.distanceTo(enemy)
if archerDistance <= 25:
hero.command(friend, “attack”, enemy)
while True:
friends = hero.findFriends()
for friend in friends:
if friend.type == “soldier”:
# This friend will be assigned to the variable soldier in commandSoldier
elif friend.type == “archer”:
# Be sure to command your archers.

Please read the FAQ to format your code properly:

Is there a problem here because you didn’t define friend in this function

Will it be better if you make the archers move [‘x’:archer.pos.x, ‘y’:archer.pos.y] in “if enemy”, and only attack if enemy is within range

1 Like

All you need to do is to change “friend” into either “archer” or “soldier” in your functions, because the program doesn’t know what a “friend” is, because when you use args, it only knows what a “soldier” or an “archer” is because

you’re defining it like that.