Skip to main content

Hey Ants - Go Do Stuff!

In this lesson you will learn how to instruct your ants to go out and gather food.

We can program the ants by entering and editing certain parameters in the development environment. When we run the updated program, the ants will behave the way we've instructed them to.

Our newly created ant colony in the sandbox is what programmers refer to as a “class”. A class is a template or plan that specifies how several similar objects should behave. This template describes the objects' attributes and methods. (“Attribute” and “method” both have special meanings in programming. We'll talk about them later.)

“Objects” are specific instances of a class. Our class in AntMe! is the anthill and the objects are the ants themselves. We can specify how they should behave by editing their class. First we want to teach them how to move. Find the “AlphaClass.cs” heading in the Solution Explorer on the right and click on it.

That will open a view in the Code Editor in the middle of the screen that shows the source code that makes up the class of our colony, Alpha. In the lower part you'll see five blocks of code, one each for Castes, Movement, Food, Communication and Combat. We call such blocks “methods”. Methods are sub-programs that describe the behavior of objects. Based on these instructions on how to behave, objects can interact with each other, both behind the scenes in the code and onscreen in the game. The first instruction that we want to give to our ants is to leave the anthill and search for food.

The Movement class tells the ants how to move. Click on the little “+” sign to the left of “Movement” and insert the following command in “Wait()” in the row between the brackets { }:

public override void Wartet()
{
GeheGeradeaus();
}

The whole thing will look like this in the editor:

When you're done, click the green “Play” button in the development environment to start AntMe!'s 3D view. Your programming command to “GoStraightAhead” will now have an effect on the game world. The ants will swarm out of the anthill and fan out across the sandbox.

At the moment, our ants are just kind of running aimlessly around the sandbox. You can change that by telling them to search for food.

Find the “See” method for sugar under “Food”. It looks like this:

public override void Sieht(Zucker zucker)
{

}

Now change the method like this:

public override void Sieht(Zucker zucker)
{
if(AktuelleLast == 0)
{
GeheZuZiel(zucker);
}
}

You just instructed the ants to move towards a mound of sugar as soon as they see one. But they'll only do so if their current load = 0—that is, if they aren't already carrying something.

Now look for the “See” method for fruit. Make the same change to it, except with (fruit) in the parentheses instead of (sugar). This tells the ants to move towards a piece of fruit instead of towards a mound of sugar. Just as before, they'll only do so if that they aren't already carrying something.

public override void Sieht(Obst obst)
{
if(AktuelleLast == 0)
{
GeheZuZiel(obst);
}
}

It will look like this in the editor:

Now your ants will run toward a mound of sugar or a piece of fruit as soon as they see one. But once your ants reach it, they won't do anything. We still need to tell them what to do once they reach some food.

Find the “ReachedDestination” method for sugar. Add the following lines:

public override void ZielErreicht(Zucker zucker)
{
Nimm(zucker);
GeheZuBau();
}

When you’re done, find the “ReachedDestination” method for fruit. Make the same changes, but substitute (fruit) for (sugar).

public override void ZielErreicht(Obst zucker)
{
Nimm(obst);
GeheZuBau();
}

In the editor it will look like this:

Now your ants will search for food (sugar and fruit) and run towards it as soon as they see some. When they arrive, they'll pick up the food and carry it back to the anthill.

Imagine—you accomplished all that just by writing eight lines of source code! Source code is what we call the text that we write for a program using a programming language. It gets translated to machine code later. It's this translation that actually tells the computer what to do. We call the thing that creates the translation a “compiler”. A compiler is a program that translates another program written in a certain programming language so that a computer can execute it.

If you look at the game closely, you'll notice that more and more ants keep running over to a piece of fruit even if there are already several ants carrying it. That's not really ideal, but you can improve their behavior with a few lines of code. Find the “See” method for fruit. Edit by adding this line of code:

public override void Sieht(Obst obst)
{
if(AktuelleLast == 0 && BrauchtNochTräger(obst))
{
GeheZuZiel(obst);
}
}

Find the “ReachedDestination” method for fruit. Add a new “if” condition:

public override void ZielErreicht(Obst obst)
{
if(BrauchtNochTräger(obst))
{
Nimm(obst);
GeheZuBau();
}
}

The “See” method now tells our ants now check if the piece of fruit needs more ants to carry it. In the “ReachedDestination” method we check again with “ifNeedsMoreCarriers” to see if more ants are needed. The reason that we want our ants to check twice is that more ants may be needed to carry the fruit at the moment that the ant first sees it, but that by the time the ant actually reaches the fruit, another ant may have already arrived and help will no longer be needed. So now you've taught your ants how to get food in the form of fruit and sugar and how to carry it back to their anthill. You'll earn one point for every grain of sugar carried back to the anthill and 250 points for every piece of fruit carried back to the anthill. In the next lesson (lesson five if you're keeping count), we'll discuss something even more exciting: battling bugs!