Jeffrey Warren edited this page Nov 3, 2015 · 17 revisions
Clone this wiki locally

Welcome to the Antfarm wiki!

Meet the ants at http://ant.farmfarm.org

There are lots of commands you can use to give the ants behaviors. Also please post interesting ones you've come up with in the Examples section below.

Also, please feel free to suggest new properties and commands, and/or submit them via pull requests!



Full colony behavior, where ants who are on their way to food can "recruit" others, teaching them where the food is. Ants also don't keep looking for stale food locations. http://ant.farmfarm.org/examples/learning (source code here)


Very basic avoidance -- each ant which comes close to another (in the same 50px square) receives a call of its method ant.onTouch(neighbor), if it has one. In this example, each ant "touching" another changes its direction by 90 degrees.

###Foraging colony

Queen ant whose children hunt for food, bring it back to her, then go back to where they last saw it.

Try it out in the example: http://ant.farmfarm.org/examples/colony

Source code: https://gist.github.com/jywarren/e9d03f93c07f245ce933

###Foraging, wall-following colony

Not working yet...

Source code: https://gist.github.com/jywarren/ffc3c6348ab4bb94afc4 ###Queen ant

Queen produces other ants: https://gist.github.com/jywarren/130619c76cc6fd56bf1f

###Ant death

Ants die at ant.age = 300, and turn grey: https://gist.github.com/jywarren/5b1d1ff67d2820a3c652

##Commands & Properties


ant.onRun() - a method which is called every frame -- most basic behaviors can be defined inside, like:

onRun = function() {
  // do stuff here:
  ant.direction += 5;

ant.onTouch(neighbor) - a method called each time an ant passes close to another. The neighbor parameter is a reference to the other ant in the "meeting".

ant.onBump() - a method called each time an ant hits a wall (either the edge of the field or a drawn red wall). Default behavior is to turn 90 degrees.

ant.x, ant.y - most basic x,y location

ant.color - get or set color using CSS colors, such as "red", "#ff0066", "#bbc", or "rgba(255,255,255,0.5)"

ant.width, ant.height - dimensions in pixels

ant.direction - the direction the ant is going, out of 360 degrees. 0 is 'up.'

ant.speed - how far the ant travels, in pixels, each cycle. Be careful, they can escape if speeds are too high!

ant.point(target) - points ant at given target; can be called as: ant.point(x,y), ant.point([x,y]), or ant.point(object) where object is any object with properties object.x and object.y -- for example, another ant!

ant.distance(target) - returns distance from ant to given target; can be called as: ant.distance(x,y), ant.distance([x,y]), or ant.distance(object) where object is any object with properties object.x and object.y -- for example, another ant!

ant.red(value), ant.green(value), ant.blue(value) - get color of ant's x,y position, or if value is provided, set the value of that color channel (0-255).

ant.grow(pixels) - increase or decrease size (width and height) by given pixels


field.red(x,y), field.green(x,y), field.blue(x,y) - get color of given x,y position

field.trailRed(x,y,amount), field.trailGreen(x,y,amount), field.trailBlue(x,y,amount) - paint one color of x,y position, by <amount> (0-255)

field.color(x,y,color,value) - get or set color using "red", "green", "blue", or "alpha". Omit value to get.


// change color when you bump a wall
onBump = function () {

  ant.direction += 90;
  ant.color = "blue";