xxxxxxxxxx
105
function c2c(c1_x, c1_y, c1_r, c2_x, c2_y, c2_r) {
let d1 = c2_x - c1_x;
let d2 = c2_y - c1_y;
let r = c1_r + c2_r;
if (((d1*d1) + (d2*d2)) <= (r*r))
return true;
return false;
}
function obstacle(y,radius,col,amplitude,theta) {
this.x = width + random(50,1500);
this.y = y;
this.color = col;
this.radius = radius;
this.amplitude = amplitude;
this.theta = theta;
this.update = function() {
this.theta += 0.02;
this.y = height/2 + sin(this.theta) * this.amplitude;
this.x--;
if ((this.x + this.radius) <= 0)
this.x = width+random(50,1500);
};
this.draw = function() {
fill(this.color);
noStroke();
ellipse(this.x, this.y, this.radius*2, this.radius*2);
};
this.collide = function(p) {
if (c2c(this.x,this.y,this.radius,p.x,p.y,p.radius))
return true;
return false;
};
};
function Player(x, y, col) {
this.x = x;
this.y = y;
this.color = col;
this.radius = 25;
this.update = function(y) {
//this.y = 0;
};
this.draw = function() {
fill(this.color);
ellipse(this.x,this.y,this.radius*2,this.radius*2);
};
};
let max_radius;
let circles;
let player;
let state;
function setup() {
createCanvas(800, 400);
max_radius = 50;
circles = [];
state = true;
for (let i = 0; i < 10; i++) {
let o = new obstacle(height, random(5,max_radius), color(255), random(1,100), 0.0);
circles.push(o)
}
player = new Player(150,height/2,color(0,255,0,190));
}
function draw() {
background(0);
if (state) {
for (let c = 0; c < circles.length; c++) {
//if (circles[c].collide(player))
if (c2c(circles[c].x,circles[c].y,circles[c].radius,player.x,player.y,player.radius))
state = false;
circles[c].update();
circles[c].draw();
}
if (keyIsDown(UP_ARROW)) {
player.y-=5;
player.y = max(player.radius,player.y);
}
if (keyIsDown(DOWN_ARROW)) {
player.y+=5;
player.y = min(height-player.radius,player.y);
}
player.draw();
} else {
textSize(48);
textAlign(CENTER,CENTER);
text("GAME OVER",0,0,width,height);
}
}