let mice = [];
const mouseCount = 20;
let traps = [];
const trapCount = 5;
let cheeses = [];
const cheeseCount = 7;
function setup(){
createCanvas(800, 800);
for(let i = 0; i < mouseCount; i++){
mice.push(new Mouse(createVector(random(width), random(height))));
}
for(let i = 0; i < trapCount; i++){
traps.push(new Trap(createVector(random(width), random(height))));
}
for(let i = 0; i < cheeseCount; i++){
cheeses.push(new Cheese(createVector(random(width), random(height))));
}
}
function draw(){
background(200, 220, 255);
// Draw and move the mice
for(let i = mice.length - 1; i >= 0; i--){
let mouse = mice[i];
if(mouse.alive){
mouse.move();
// Check for traps
for(let trap of traps){
trap.activate(mouse);
}
// Check for cheese
for(let j = cheeses.length - 1; j >= 0; j--){
let cheese = cheeses[j];
if(mouse.eat(cheese)){
cheeses.splice(j, 1); // Remove cheese when eaten
cheeses.push(new Cheese(createVector(random(width), random(height)))); // Spawn new cheese
}
}
// Check for reproduction
mouse.reproduce(mice);
mouse.display();
} else {
mice.splice(i, 1); // Remove dead mice
}
}
// Draw the traps
for(let trap of traps){
trap.display();
}
// Draw the cheese
for(let cheese of cheeses){
cheese.display();
}
}
class Mouse {
constructor(_pos){
this.pos = _pos;
this.vel = createVector(0, 0);
this.accel = p5.Vector.random2D();
this.radius = 10;
this.maxSpeed = 3;
this.alive = true;
this.canReproduce = false;
// Mouse can reproduce after 3 seconds
setTimeout(() => {
this.canReproduce = true;
}, 3000);
}
move(){
this.accel = p5.Vector.random2D();
this.vel.add(this.accel);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
this.borders();
}
eat(cheese){
if(this.pos.dist(cheese.pos) <= this.radius + cheese.radius){
this.radius += 5; // Mouse gets fatter
return true; // Cheese is eaten
}
return false;
}
reproduce(mice){
if(this.canReproduce){
for(let otherMouse of mice){
if(otherMouse !== this && otherMouse.alive){
let distance = this.pos.dist(otherMouse.pos);
if(distance < 50 && random(1) < 0.005){ // Small chance to reproduce
let babyPos = p5.Vector.lerp(this.pos, otherMouse.pos, 0.5); // New baby position
mice.push(new Mouse(babyPos)); // Add new mouse to the swarm
this.canReproduce = false; // Reset reproduction timer
setTimeout(() => {
this.canReproduce = true;
}, 5000);
}
}
}
}
}
borders() {
if (this.pos.x < -this.radius) this.pos.x = width + this.radius;
if (this.pos.y < -this.radius) this.pos.y = height + this.radius;
if (this.pos.x > width + this.radius) this.pos.x = -this.radius;
if (this.pos.y > height + this.radius) this.pos.y = -this.radius;
}
display(){
fill(150);
noStroke();
ellipse(this.pos.x, this.pos.y, this.radius * 2, this.radius * 2);
}
}
class Trap {
constructor(_pos){
this.pos = _pos;
this.radius = 30;
}
activate(mouse){
if(this.pos.dist(mouse.pos) < this.radius){
mouse.alive = false; // Kill the mouse if too close
this.respawn(); // Respawn the trap in a new location
}
}
respawn(){
// Move trap to a new random location
this.pos = createVector(random(width), random(height));
}
display(){
fill(255, 0, 0);
noStroke();
rect(this.pos.x - this.radius, this.pos.y - this.radius, this.radius * 2, this.radius * 2); // Square trap
}
}
class Cheese {
constructor(_pos){
this.pos = _pos;
this.radius = 10;
}
display(){
fill(255, 255, 0);
noStroke();
ellipse(this.pos.x, this.pos.y, this.radius * 2, this.radius * 2); // Circular cheese
}
}
Written Code that depicts renewing cheese in the mice ecosystem.