xxxxxxxxxx
46
class prut{
constructor(x,y,s,c){
this.x=x
this.y=y
this.s=s
this.c=c
}
gras(){
fill(this.c)
circle(min(this.x,w-this.s/2),min(this.y,h-this.s/2),this.s)
}
}
function fdist(f1,f2){
return sqrt(sq(f1.x-f2.x)+sq(f1.y-f2.y))
}
let w=900
let h=800
let p=0
let fluffy=[]
function setup() {
createCanvas(w, h);
fluffy.push(new prut(200,200,50,'blue'))
fluffy.push(new prut(100,200,100,'pink'))
fluffy.push(new prut(300,200,150,'lightgreen'))
}
function draw() {
background(220);
if(keyIsDown(RIGHT_ARROW)){fluffy[0].x=min(w-fluffy[0].s/2,fluffy[0].x+p+5)}
if(keyIsDown(LEFT_ARROW)){fluffy[0].x=max(fluffy[0].s/2,fluffy[0].x-(p+5))}
if(keyIsDown(UP_ARROW)){fluffy[0].y=max(fluffy[0].s/2,fluffy[0].y-(p+5))}
if(keyIsDown(DOWN_ARROW)){fluffy[0].y=min(h-fluffy[0].s/2,fluffy[0].y+p+5)}
fluffy[2].x+=p*(fluffy[0].x-fluffy[2].x)/fdist(fluffy[0],fluffy[2])
fluffy[2].y+=p*(fluffy[0].y-fluffy[2].y)/fdist(fluffy[0],fluffy[2])
for(let i=0;i<fluffy.length;i++){
if(i>0&&fdist(fluffy[0],fluffy[i])<(fluffy[0].s+fluffy[i].s)/2){
fluffy[i].x=random(fluffy[i].s/2,w-fluffy[i].s/2)
fluffy[i].y=random(fluffy[i].s/2,h-fluffy[i].s/2)
if(i==1){p++}else{p--}
}
fluffy[i].gras()
}
fill('black')
textSize(50)
text('Stig/Point: '+p,10,50)
}