xxxxxxxxxx
448
let score = 0;
let highscore = 0;
let playSpeedButton;
let playSpeed = 1;
let boids = [];
let tempStartPoint = null;
let attractToMouse = false;
let predators = [];
let apexPredators = [];
let mode = 0; // Add this line
let instructionsButton;
let tempBoid;
let tempPredator;
let tempApexPredator;
let boidSpeedSlider, boidMaxForceSlider, boidReproductionSlider;
let predatorSpeedSlider, apexPredatorSpeedSlider;
let predatorHungerRateSlider, apexPredatorHungerRateSlider;
let boidImage;
let nextGenButton;
let sliderXPosition;
let sliderYPosition;
function preload() {
boidImage = loadImage('1.png');
}
function createSliders() {
boidSpeedSlider = createSlider(0, 6, 3, 0.1);
boidMaxForceSlider = createSlider(0, 0.5, 0.25, 0.01);
boidReproductionSlider = createSlider(0, 10, 5, 0.01);
predatorSpeedSlider = createSlider(0, 8, 4, 0.1);
predatorMaxForceSlider = createSlider(0, 0.16, 0.08, 0.01);
predatorReproductionSlider = createSlider(0, 10, 5, 1);
apexSpeedSlider = createSlider(0, 6, 3, 0.1);
apexMaxForceSlider = createSlider(0, 0.1, 0.05, 0.01);
apexReproductionSlider = createSlider(0, 6, 3, 1);
predatorHungerRateSlider = createSlider(0, 0.2, 0.1, 0.0001);
apexPredatorHungerRateSlider = createSlider(0, 0.2, 0.1, 0.0001);
boidLifespanSlider = createSlider(0, 100, 50, 1);
predatorLifespanSlider = createSlider(0, 100, 50, 1);
apexPredatorLifespanSlider = createSlider(0, 100, 50, 1);
playSpeedButton = createButton('Play Speed:');
//rename button to include the current play speed
playSpeedButton.html('Play Speed:' + playSpeed + 'x');
playSpeedButton.position(width - 75, 5); // Position it on the top right
playSpeedButton.mousePressed(changePlaySpeed); // Attach a function to be called when the button is pressed
nextGenButton = createButton('Next Generation');
statguideButton = createButton('Stat Guide');
instructionsButton = createButton('Instructions');
instructionsButton.position(statguideButton.x - instructionsButton.width - 10, height - 125);
instructionsButton.mousePressed(showInstructions);
}
function changePlaySpeed(){
if (playSpeed === 1) {
playSpeed = 2;
} else if (playSpeed === 2) {
playSpeed = 4;
} else if (playSpeed === 4) {
playSpeed = 8;
} else if (playSpeed === 8) {
playSpeed = 1;
}
playSpeedButton.html('Play Speed:' + playSpeed + 'x');
}
function showInstructions() {
if (mode === 2) {
hideSliders();
instructionsButton.show();
mode = 0;
} else if (mode === 0) {
mode = 2;
}
}
//function to hide sliders
function hideSliders() {
boidSpeedSlider.hide();
boidMaxForceSlider.hide();
boidReproductionSlider.hide();
predatorSpeedSlider.hide();
predatorMaxForceSlider.hide();
predatorReproductionSlider.hide();
apexSpeedSlider.hide();
apexMaxForceSlider.hide();
apexReproductionSlider.hide();
predatorHungerRateSlider.hide();
apexPredatorHungerRateSlider.hide();
boidLifespanSlider.hide();
predatorLifespanSlider.hide();
apexPredatorLifespanSlider.hide();
nextGenButton.hide();
statguideButton.hide();
instructionsButton.hide();
playSpeedButton.hide();
}
function setup() {
startTime = millis();
sliderXPosition = 350;
sliderYPosition = 100;
frameRate(90);
createCanvas(windowWidth, windowHeight);
createSliders();
// Position sliders
boidSpeedSlider.position(sliderXPosition,sliderYPosition);
sliderYPosition += 25;
boidMaxForceSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
boidReproductionSlider.position(sliderXPosition,sliderYPosition);
sliderYPosition += 25;
boidLifespanSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 60;
predatorSpeedSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
predatorMaxForceSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
predatorReproductionSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
predatorHungerRateSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
predatorLifespanSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 60;
apexSpeedSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
apexMaxForceSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
apexReproductionSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
apexPredatorHungerRateSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 25;
apexPredatorLifespanSlider.position(sliderXPosition, sliderYPosition);
sliderYPosition += 60;
nextGenButton.position(width/2 + 100, height - 75);
nextGenButton.mousePressed(nextGeneration);
statguideButton.position(width/2 - 50, height - 75);
statguideButton.mousePressed(StatGuide);
instructionsButton.position(width /2 - 200 , height - 75);
// Hide sliders
hideSliders();
for (let i = 0; i < 100; i++) {
boids.push(new Boid(random(width), random(height), i * 2.5)); // Color based on index
}
for (let i = 0; i < 3; i++) {
predators.push(new Predator(random(width), random(height)));
}
apexPredators.push(new ApexPredator(random(width) + 1000, random(height) + 1000));
tempBoid = new Boid(100, 145);
tempPredator = new Predator(predatorReproductionSlider.x-125, predatorReproductionSlider.y+5);
tempApexPredator = new ApexPredator(apexReproductionSlider.x-125, apexReproductionSlider.y+15);
}
function draw() {
if (mode === 0) {
displayStartScreen();
} else if (mode === 1) {
//hide sliders
hideSliders();
playSpeedButton.show();
//update all creatures
for (let boid of boids) {
boid.maxSpeed = boidSpeedSlider.value();
boid.maxForce = boidMaxForceSlider.value();
boid.reproductionChance = boidReproductionSlider.value() / 1000;
boid.lifespan = boidLifespanSlider.value();
}
for (let predator of predators) {
predator.maxSpeed = predatorSpeedSlider.value();
predator.maxForce = predatorMaxForceSlider.value();
predator.reproductionThreshold = predatorReproductionSlider.value();
predator.hungerrate = predatorHungerRateSlider.value();
predator.lifespan = predatorLifespanSlider.value()*100;
}
for (let apexPredator of apexPredators) {
apexPredator.maxSpeed = apexSpeedSlider.value();
apexPredator.maxForce = apexMaxForceSlider.value();
apexPredator.reproductionThreshold = apexReproductionSlider.value();
apexPredator.hungerrate = apexPredatorHungerRateSlider.value();
apexPredator.lifespan = apexPredatorLifespanSlider.value();
}
//loop runs playback speed number of times
for (let i = 0; i < playSpeed; i++) {
runSimulation();
}
} else if (mode === 2) {
displaySliderScreen();
} else if (mode === 3) {
displayStatGuideScreen();
statguideButton.show();
}
}
function displayStartScreen() {
background(0,40);
//Title
textFont("Arial");
textSize(80);
fill(200);
text("Pacman Ecosystem", 70,80);
//Continue
statguideButton.position(width/2 - 50, height - 75);
statguideButton.mousePressed(StatGuide);
//Description
fill(200);
strokeWeight(1);
textFont("Arial");
textSize(25);
text("Welcome to the Pacman Ecosystem, a simulation program where\n you, as a player, are tasked with maintaining balance in a dynamic\n ecosystem. The simulation features three main entities: boids, \npacmen, and ghosts. Your goal is to ensure that none of the\n creatures die out. You will be prompted every generation to alter\n the creatures' stats, allowing you to influence their survival and\n evolution." , 70,210);
}
function displayStatGuideScreen() {
hideSliders();
background(0,40);
//Title
textFont("Arial");
textSize(80);
fill(200);
text("Stat Guide", 70,80);
//Description
fill(200);
strokeWeight(1);
textFont("Arial");
textSize(25);
let statGuide = "Speed: The speed of the entity.\n\nAgility: The ability of the entity to change direction quickly.\n\nReproduction Chance: The probability that the entity will reproduce.\n\nReproduction Threshold: The minimum age for the entity to reproduce.\n\nLife Span: The maximum age of the entity.\n\nHunger Rate: The rate at which the entity becomes hungry.";
text(statGuide, 70,145);
// text("Speed: Controls the maximum speed at which the creature can move.\n\n\nAgility: The maximum steering force the creature can apply when changing its direction.\n\n\nReproduction: Controls the rate at which the creature reproduces.\n\n\nHunger Rate: Controls how quickly the creature gets hungry.\n\n\nLife Span: The maximum age of the creature.", 70,210);
}
function runSimulation() {
textSize(25);
score++;
if (score > highscore) {
highscore = score;
fill(0, 255, 0); // Green color
text("New High Score!", 10, 60);
}
background(0, 40);
noStroke();
// Score
fill(255);
text("Score: " + score, 10, 30);
// Boids
if (boids.length < 20) {
fill(255, 0, 0); // Red color
} else {
fill(255); // White color
}
text("Boids: " + boids.length, 175, 30);
// Predators
if (predators.length < 5) {
fill(255, 0, 0); // Red color
} else {
fill(255); // White color
}
text("Predators: " + predators.length, 325, 30);
// Ghosts
if (apexPredators.length < 2) {
fill(255, 0, 0); // Red color
} else {
fill(255); // White color
}
text("Ghosts: " + apexPredators.length, 550, 30);
//update all creatures
for (let apexPredator of apexPredators) {
if (apexPredator.cooldown <= 0) {
apexPredator.startHunting();
}
apexPredator.reproduce(apexPredators);
apexPredator.seek(predators);
apexPredator.update(predators,apexPredators);
apexPredator.display(predators);
}
for (let boid of boids) {
boid.flock(boids, predators);
boid.reproduce(boids);
boid.update(boids);
boid.wrapAround();
boid.display();
}
for (let predator of predators) {
predator.seek(boids, apexPredators); // Pass the apexPredators array
predator.reproduce(predators);
predator.update(predators);
predator.display(boids);
}
textSize(60);
strokeWeight(1);
if (boids.length === 0 || predators.length === 0 || apexPredators.length === 0 || boids.length > 200) {
let message;
if (boids.length === 0) {
message = "Boids died out";
} else if (predators.length === 0) {
message = "Predators died out";
} else if (apexPredators.length === 0) {
message = "Ghosts died out";
} else if (boids.length > 200) {
message = "Boids overpopulated";
}
print(message);
// Display the message
fill(255, 0, 0); // Red color
text(message, width / 2-300, height / 2);
// Delay the transition to the next page
setTimeout(() => {
mode = 2; // Replace with the mode for the next page
}, 2000); // Delay for 2 seconds
}
}
function displaySliderScreen() {
background(0);
// Display text
fill(255);
textSize(32);
text("Adjust creature stats", 20, 40);
//display highscore
text("High Score: " + highscore, 400, 40);
// Display sliders
boidSpeedSlider.show();
boidMaxForceSlider.show();
boidReproductionSlider.show();
predatorSpeedSlider.show();
predatorMaxForceSlider.show();
predatorReproductionSlider.show();
apexSpeedSlider.show();
apexMaxForceSlider.show();
apexReproductionSlider.show();
predatorHungerRateSlider.show();
apexPredatorHungerRateSlider.show();
boidLifespanSlider.show();
predatorLifespanSlider.show();
apexPredatorLifespanSlider.show();
nextGenButton.show();
statguideButton.show();
instructionsButton.show();
playSpeedButton.show();
// Labeling the Sliders
textSize(15);
noStroke();
text("Boid Speed: " + boidSpeedSlider.value(), boidSpeedSlider.x +150, boidSpeedSlider.y+15);
text("Boid Agility: " + boidMaxForceSlider.value(), boidMaxForceSlider.x +150, boidMaxForceSlider.y+15);
text("Boid Reproduction Chance: " + boidReproductionSlider.value(), boidReproductionSlider.x +150, boidReproductionSlider.y+15);
text("Pacman Speed: " + predatorSpeedSlider.value(), predatorSpeedSlider.x +150, predatorSpeedSlider.y+15);
text("Pacman Agility: " + predatorMaxForceSlider.value(), predatorMaxForceSlider.x +150, predatorMaxForceSlider.y+15);
text("Pacman Reproduction Threshold: " + predatorReproductionSlider.value(), predatorReproductionSlider.x +150, predatorReproductionSlider.y+15);
text("Ghost Speed: " + apexSpeedSlider.value(), apexSpeedSlider.x +150, apexSpeedSlider.y+15);
text("Ghost Agility: " + apexMaxForceSlider.value(), apexMaxForceSlider.x +150, apexMaxForceSlider.y+15);
text("Ghost Reproduction Threshold: " + apexReproductionSlider.value(), apexReproductionSlider.x +150, apexReproductionSlider.y+15);
text("Pacman Hunger Rate: " + predatorHungerRateSlider.value(), predatorHungerRateSlider.x +150, predatorHungerRateSlider.y+15);
text("Ghost Hunger Rate: " + apexPredatorHungerRateSlider.value(), apexPredatorHungerRateSlider.x +150, apexPredatorHungerRateSlider.y+15);
text("Boid Life Span: " + boidLifespanSlider.value(), boidLifespanSlider.x +150, boidLifespanSlider.y+15);
text("Pacman Life Span: " + predatorLifespanSlider.value(), predatorLifespanSlider.x +150, predatorLifespanSlider.y+15);
text("Ghost Life Span: " + apexPredatorLifespanSlider.value(), apexPredatorLifespanSlider.x +150, apexPredatorLifespanSlider.y+15);
image(boidImage, 175, 105, boidImage.width / 2, boidImage.height / 2);
tempPredator.display([]);
tempApexPredator.display([]);
}
function nextGeneration() {
// Remove all creatures
score = 0;
boids = [];
predators = [];
apexPredators = [];
for (let i = 0; i < 100; i++) {
boids.push(new Boid(random(width), random(height), i * 2.5)); // Color based on index
}
for (let i = 0; i < 3; i++) {
predators.push(new Predator(random(width), random(height)));
}
apexPredators.push(new ApexPredator());
setInterval(() => {
for (let apexPredator of apexPredators) {
apexPredator.startHunting();
}
}, 10000);
mode = 1;
}
function StatGuide(){
if (mode === 2){
mode = 3;
} else if (mode === 3){
mode = 2;
}
}
function mousePressed() {
if (mode === 0 && score === 0) {
mode = 1;
}
}