I getting stuck since the enemy appear on the sides
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.
items = hero.findItems()
nearest = hero.findNearest(items)
if nearest:
hero.move(nearest.pos)
def summonTroops():
# Summon soldiers if you have the gold.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
pass
# 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):
enemy = hero.findNearestEnemy()
# Soldiers should attack enemies.
hero.command(soldier, "attack", enemy)
In this one, you need to define friends and use a for loop to command each soldier to attack the enemy.
After this, you need to write a commandArcher function. This function is basically the same as the commandSoldier function but archers should only attack enemies who are closer than 25 meters (in game), otherwise, stay still.
This is comments from the game:
# 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.
Then you just need to use the functions in a while True loop. But you also have to use a for-loop for commanding your friends.
Lydia
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
# Collect coins.
items = hero.findItems()
nearest = hero.findNearest(items)
if nearest:
hero.move(nearest.pos)
def summonTroops():
# Summon soldiers if you have the gold.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
pass
# 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):
enemy = hero.findNearestEnemy()
# Soldiers should attack enemies.
hero.command(soldier, "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.
while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == "soldier":
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier(friend)
elif friend.type == "archer":
# Be sure to command your archers.
enemyDistance = hero.distanceTo(enemy)
for friend in friends:
if enemyDistance < 25:
if friend.type == "archer":
hero.command(friend, "attack", enemy)
else:
if enemyDistance > 25:
hero.command(friend, "move", friend.pos)
pass
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
# Collect coins.
items = hero.findItems()
nearest = hero.findNearest(items)
if nearest:
hero.move(nearest.pos)
def summonTroops():
# Summon soldiers if you have the gold.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
pass
# 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):
enemy = hero.findNearestEnemy()
# Soldiers should attack enemies.
hero.command(soldier, "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.
while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == "soldier":
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier(friend)
elif friend.type == "archer":
# Be sure to command your archers.
def commandArcher():
enemy = hero.findNearestEnemy()
friends = hero.findByType("archer", hero.findFriends())
if enemy:
enemy = hero.findNearestEnemy()
enemyDistance = hero.distanceTo(enemy)
for friend in friends:
if enemyDistance < 25:
if friend.type == "archer":
hero.command(friend, "attack", enemy)
else:
if enemyDistance > 25:
hero.command(friend, "move", friend.pos)
if friend.type == "archer":
commandArcher(archer)
# Ogres are trying to take out your reindeer!
# Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
# Collect coins.
items = hero.findItems()
nearest = hero.findNearest(items)
if nearest:
hero.move(nearest.pos)
def summonTroops():
# Summon soldiers if you have the gold.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
pass
# 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):
enemy = hero.findNearestEnemy()
# Soldiers should attack enemies.
hero.command(soldier, "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.
while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == "soldier":
commandSoldier(friend)
elif friend.type == "archer":
# Be sure to command your archers.
def commandArcher():
enemy = hero.findNearestEnemy()
friends = hero.findByType("archer", hero.findFriends())
if enemy:
enemy = hero.findNearestEnemy()
enemyDistance = hero.distanceTo(enemy)
for friend in friends:
if enemyDistance < 25:
if friend.type == "archer":
hero.command(friend, "attack", enemy)
else:
if enemyDistance > 25:
hero.command(friend, "move", friend.pos)
if friend.type == "archer":
commandArcher(archer)
# 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.move(item.pos)
pass
def summonTroops():
# Summon soldiers if you have the gold.
if hero.gold > hero.costOf("soldier"):
hero.summon("soldier")
pass
# 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():
for soldier in hero.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
hero.command(soldier, "attack", enemy)
pass
# 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():
for archer in hero.findFriends():
enemy = archer.findNearestEnemy()
if enemy and archer.distanceTo(enemy) < 25:
hero.command(archer, "attack", enemy)
pass
while True:
pickUpCoin()
summonTroops()
friends = hero.findFriends()
for friend in friends:
if friend.type == "soldier":
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier("soldier")
elif friend.type == "archer":
# Be sure to command your archers.
commandArcher("archer")
pass
So in commandSoldier function, you first need to define friends as friends = hero.findFriends(), and then use a for-loop to command each soldier to attack the enemy (perferably the nearest to them)
Then you need to write commandArcher function to command the archers to attack enemies that is closer than 25 meters, if the enemy is more than 25 meters away, then stay still.
Then use a while True loop to use the functions.
Lydia