xxxxxxxxxx
67
let colony;
let ants = [];
let numAnts;
let hedges = [];
let soilColor = "#CB9547";
let colonyColor = "#DCB85D";
let antColor = "#300E00";
let hedgeColor = "#0B6700"
function setup() {
createCanvas(500, 500);
frameRate(60);
// Garden Hedges
hedges = [
new Hedge(width/2, height/20, width, height/10, hedgeColor),
new Hedge(width/2, 19*height/20, width, height/10, hedgeColor),
new Hedge(width/20, height/2, width/10, height, hedgeColor),
new Hedge(19*width/20, height/2, width/10, height, hedgeColor),
]
// Anthill
colony = new Colony(random(width/4, 3*width/4), random(height/4, 3*height/4), width / 4, colonyColor);
// Initial Ant Population
numAnts = random(10, 50);
for (let ant = 0; ant < numAnts; ant++) {
newAnt = new Ant(colony.x, colony.y, 3, 3, 2, random(0, 2 * PI), antColor);
ants.push(newAnt);
}
}
function draw() {
background(soilColor+"10");
colony.display();
for (let i = 0; i < ants.length; i++) {
ants[i].display();
ants[i].move();
ants[i].bounce();
// If ants meet, they move in opposite directions to original movement
for (var j = 0; j < ants.length; j++) {
if (i != j && ants[i].collide(ants[j]) && !(ants[i].collide(colony))) {
ants[i].speedDir += PI/2;
ants[j].speedDir += PI/2;
}
}
}
// Birth 10-15 ants every 5 seconds
if (frameCount % 300 == 0 && ants.length < 100) {
for (let b = 0; b < random(10,15); b++) {
newAnt = new Ant(colony.x, colony.y, 3, 3, 2, random(0, 2 * PI), antColor);
ants.push(newAnt);
}
}
// Kill 5-15 ants every 20 seconds
if (frameCount % 600 == 0) {
for (let d = 0; d < random(5,15); d++) {
ants.shift();
}
}
// Display hedges (to cover ants below)
rectMode(CENTER);
for (let k = 0; k < hedges.length; k++) {
hedges[k].display();
};
}