[SOLVED] Army Training 2, Python

Game Development 2, Army Training 2, Python

# Defeat the ogres using event handlers to command units.

# Spawn 2 "soldier"s.
game.spawnXY("soldier", 35, 20)
game.spawnXY("soldier", 45, 20)
# Spawn 2 "archer"s.
game.spawnXY("archer", 36, 22)
game.spawnXY("archer", 88, 12)
def fightEnemies(event):
    while True:
        # event.target is the unit that is executing this event handler function!
        friendUnit = event.target
        enemy = friendUnit.findNearestEnemy()
        # Have friendUnit attack() the enemy!
        friendUnit.attack(enemy)

# This attaches the fightEnemies handler to all soldiers' "spawn" events.
game.setActionFor("soldier", "spawn", fightEnemies)

# Now, attach fightEnemies to the archers' "spawn" events:
fightEnemies()

1 Like

Here put instead something like this:

game.setActionFor("archer","spawn",fightEnemies)

After this put

if enemy:

And put this

In the if.
Does it work now?

1 Like

Yes, it worked. Thanks!

It didn’t actually work for me… I’m stuck on the same exact one. And tried so many times to fix it. Help

If you send your equipment, code and problem we are glad to help you!

I don’t see what am I doing wrong… help

Can you format your code like here:

1 Like

В мене не виходить, пише: