xxxxxxxxxx
// Define some constants
const ANT_COUNT = 1000;
const FOOD_COUNT = 100;
const PRED_COUNT = 1;
// Define arrays to hold ants and food
let ants = [];
let food = [];
let preds = [];
// Define a hill object
let hill = {
x: 200,
y: 200,
diameter: 50,
foodCount: 0,
display: function() {
fill(0, 100, 0);
stroke(0, 255, 0);
ellipse(this.x, this.y, this.diameter);
noStroke();
fill(255);
textSize(16);
text("Food: " + this.foodCount, this.x - this.diameter / 2, this.y + this.diameter / 2 + 20);
}
};
function setup() {
createCanvas(1000, 800);
// Create ants
for (let i = 0; i < ANT_COUNT; i++) {
let ant = new Ant(hill.x, hill.y, hill);
ants.push(ant);
}
// Create food
for (let i = 0; i < FOOD_COUNT; i++) {
food.push(new Food(random(width), random(height)));
}
for (let i = 0; i < PRED_COUNT; i++) {
let pred = new Predator(random(width), random(height));
preds.push(pred);
}
}
function draw() {
background(255);
// Display the hill
hill.display();
// Move and display ants
for (let ant of ants) {
ant.move();
ant.display();
ant.live();
if(!ant.living){
ants.splice(ants.indexOf(ant), 1);
}
}
for (let pred of preds) {
pred.move();
pred.display();
pred.live();
if(!pred.living){
preds.splice(preds.indexOf(pred), 1);
}
}
// Display food
for (let f of food) {
f.display();
}
if(food.length < 50){
for (let i = 0; i < abs(random(50,200)); i++) {
food.push(new Food(random(width), random(height)));
}
}
}