xxxxxxxxxx
108
let newWorld;
let bloops = [];
let foods = [];
let foodEaten = 0;
function setup() {
//avoid right click browser window pop-up
for (let element of document.getElementsByClassName("p5Canvas")) {
element.addEventListener("contextmenu", (e) => e.preventDefault());
}
//
createCanvas(2560, 1440);
newWorld = new world(8, 400);
}
function draw() {
background(0);
boundary();
newWorld.display();
if (foodEaten > 100 && bloops.length >= 5) {
let newFood = new food(random(windowWidth), random(windowHeight));
foods.push(newFood);
//foodEaten = 0;
}
for (let i = 0; i < bloops.length; i++) {
let birthBloop = bloops[i];
if (birthBloop.health > 300) {
let originalDNA = new DNA();
let newBloop = new bloop(originalDNA);
newBloop.location.x = birthBloop.location.x;
newBloop.location.y = birthBloop.location.y;
bloops.push(newBloop);
birthBloop.health = birthBloop.health * 0.5;
}
}
if(bloops.length <= 0 || foods.length <= 0){
newWorld = new world(8, 400);
}
if(mouseIsPressed && (mouseButton === RIGHT)){
stroke(0, random(180, 255), 250, 100);
strokeWeight(10);
line(mouseX, 0, mouseX, height);
line(0, mouseY, width, mouseY);
strokeWeight(1);
for (let i = bloops.length - 1; i >= 0; i--) {
let killBloop = bloops[i];
if(killBloop.location.x < mouseX+20 && killBloop.location.x > mouseX-20){
bloops.splice(i, 1);
}
if(killBloop.location.y < mouseY+20 && killBloop.location.y > mouseY-20){
bloops.splice(i, 1);
}
}
}
}
function boundary() {
noFill();
stroke(0, 255, 0);
rectMode(CENTER);
rect(width / 2, height / 2, width - 20, height - 20);
noStroke();
for (let j = 0; j < height; j += 250) {
for (let i = 0; i < width; i += 250) {
stroke(255, 10);
line(i, 0, i, height);
line(0, j, width, j);
}
}
}
function keyPressed() {
if (key === ' ') {
newWorld = new world(8, 400);
}
if(key == 'a'){
let fs = fullscreen();
fullscreen(!fs);
}
}
function mouseDragged() {
if (mouseButton === LEFT) {
let addFood = new food(mouseX, mouseY);
foods.push(addFood);
}
}
function mousePressed() {
if (mouseButton === LEFT) {
let addFood = new food(mouseX, mouseY);
foods.push(addFood);
}
if (mouseButton === CENTER) {
let originalDNA = new DNA();
let newBloop = new bloop(originalDNA);
newBloop.location.x = mouseX;
newBloop.location.y = mouseY;
bloops.push(newBloop);
}
}