Use the image file in this post and run the code in NodeBox. Clicking in the window after you render will add food sources to the scene.
You will also need the ants & color libraries installed.
try:
ants = ximport("ants")
colors = ximport("colors")except:
ants = ximport("__init__")reload(ants)size(500,500)speed(200)
clr = colors.rgb(0.6, 0.4, 0, 0.5)defsetup():
# Starts a colony with 30 ants in it.global colony, has_clicked
colony = ants.colony(20, WIDTH/2, HEIGHT/2, 100)# Add some food in the vicinity of the colony.for i inrange(0):
x = 50 + random(WIDTH-100)
y = 50 + random(HEIGHT-100)
s = random(20,40)
colony.foodsources.append(ants.food(x,y,s))defdraw():
global colony, has_clicked
if mousedown:
ifnot has_clicked:
x = MOUSEX
y = MOUSEY
s = random(20,40)
colony.foodsources.append(ants.food(x,y,s))
has_clicked = Trueelse:
has_clicked = Falseimage("dirt3.jpg", 0, 0)# Draw the hoarded food in the colony.
s = colony.food#colony hole#strokewidth(10)#stroke(1, .5, .4)#fill(0, 0, 0)#oval(245, 245, 20, 20)nostroke()# Draw each foodsource in green.# Watch it shrink as the ants eat away its size parameter!#fill(0.6,0.8,0)fill(0.6, 0.4, 0, 0.6)for f in colony.foodsources:
#food sourceoval(f.x-f.size/2, f.y-f.size/2, f.size, f.size)for ant in colony:
#stroke(0,0,0, 0.4)strokewidth(1)nofill()autoclosepath(False)# Draw the pheromone trail for each ant.# Ants leave a trail of scent from the foodsource,# enabling other ants to find the food as well!iflen(ant.trail) > 0:
beginpath(ant.trail[0].x, ant.trail[0].y)for p in ant.trail: lineto(p.x, p.y)endpath()nostroke()# ant color no foodfill(0,0,0, 0.8)# Change ant color when carrying food.if ant.has_food:
fill(0,0,0)# The main ant behaviour:# 1) follow an encountered trail,# 2) harvest nearby food source,# 3) bring food back to colony,# 4) wander aimlessly
ant.forage()rect(ant.x, ant.y, 3, 3)
Interactive ants!
Posted by mike.arney on Oct 15, 2009Use the image file in this post and run the code in NodeBox. Clicking in the window after you render will add food sources to the scene.
You will also need the ants & color libraries installed.