xxxxxxxxxx
72
let moles=[];
let score=0;
let output;
let timeSpan;
function setup() {
createCanvas(500, 500);
output = createElement("h3", "Output");
output.position(10,10);
for (let i=0;i<3;i++){
moles.push(new Mole());
}
}
function draw() {
background(255);
for (let i=0;i<moles.length;i++){
let timeSpan = moles[i].timeSpan;
if (moles[i].counter<timeSpan) { //delay before appearance
moles[i].counter++;
} else if (moles[i].counter>=timeSpan && moles[i].counter<timeSpan*2){ //appearance
moles[i].drawMole();
} else{ // if over time, disappear
moles.push(new Mole());
moles.splice(i,1);
}
}
output.html("Score: "+score);
}
function mouseClicked(){
for (let i=0;i<moles.length;i++){
if (dist(mouseX,mouseY,moles[i].x,moles[i].y)<moles[i].d/2){
if (moles[i].type===0) score++;
else score--;
if (score<0) score=0;
moles.push(new Mole());
moles.splice(i,1);
}
}
}
class Mole{
constructor(){
this.timeSpan=int(random(100,300));
this.d=100; //diameter of the ellipse
this.x=random(this.d/2,width-this.d/2);
this.y=random(this.d/2,height-this.d/2);
this.type=int(random(2)); //determine red or blue
this.counter=0;
}
drawMole(){
if(this.type===0) fill(255,102,178);//red, need to be whacked
else fill(102,178,255);//blue, need to be ignored
noStroke();
ellipse(this.x,this.y,this.d);
this.counter++;
}
}