xxxxxxxxxx
185
let rain = [];
let range = 400;
let size = 30;
let intervalId;
let health = 3;
let ctr = 0;
let lost = 0;
let win = 0;
let starttime = 2;
let timer = 30+starttime;
function resetGame() {
rain = [];
range = 400;
size = 30;
health = 3;
ctr = 0;
lost = 0;
win = 0;
starttime = 2;
timer = 30 + starttime;
for (let i = 0; i < 120; i++) {
rain.push(new raindrop());
}
intervalId = setInterval(countDown, 1000);
}
function countDown() {
timer--;
if (timer <= 0) {
clearInterval(intervalId);
console.log("Time's up!");
}
}
function displayTimer() {
if (health < 2) {
fill(255, 0, 0);
} else {
fill(255);
}
textSize(24);
textAlign(CENTER, CENTER);
text("Time: " + timer + " Health: " + health, width / 2, height / 10);
}
function drawTriangleAroundCursor() {
push();
translate(mouseX, mouseY);
stroke(0);
fill(175, 200);
triangle(20, -20, -20, 20, 20, 20);
pop();
}
function triangleArea(ax, ay, bx, by, cx, cy) {
return (ax - cx) * (by - cy) - (bx - cx) * (ay - cy);
}
function insideTriangle(ax, ay, bx, by, cx, cy, px, py) {
let b0 = triangleArea(ax, ay, bx, by, px, py) < 0.0;
let b1 = triangleArea(bx, by, cx, cy, px, py) < 0.0;
let b2 = triangleArea(cx, cy, ax, ay, px, py) < 0.0;
return b0 == b1 && b1 == b2;
}
class raindrop {
constructor() {
this.xPos = mouseX + random(-range, range);
this.yPos = 0;
this.xSpeed = 0;
this.ySpeed = random(2, 3);
}
move() {
this.xPos += this.xSpeed;
this.yPos += this.ySpeed;
if (this.yPos == 400) {
this.yPos = 0;
}
}
checkForCollisions() {
let triangleX = [mouseX - 10, mouseX + 10, mouseX];
let triangleY = [mouseY + 5, mouseY + 5, mouseY - 10];
let isInside = insideTriangle(
triangleX[0],
triangleY[0],
triangleX[1],
triangleY[1],
triangleX[2],
triangleY[2],
this.xPos,
this.yPos
);
if (isInside) {
this.xPos = mouseX + random(-range, range);
this.yPos = 0;
this.ySpeed = random(2, 4);
health--;
console.log("Collision detected!");
} else if (this.yPos >= height - 10) {
this.xPos = mouseX + random(-range, range);
this.yPos = 0;
this.ySpeed = random(2, 3);
}
}
draw() {
if (ctr == 0) {
fill(100, 100, 230);
} else if (ctr == 1) {
fill(100, 230, 100);
} else if (ctr == 2) {
fill(230, 100, 100);
}
rect(this.xPos, this.yPos, 4, 10);
}
}
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 120; i++) {
rain.push(new raindrop());
}
intervalId = setInterval(countDown, 1000);
}
function draw() {
noCursor();
background(0);
fill(200, 200, 100);
triangle(
mouseX - 10,
mouseY + 5,
mouseX + 10,
mouseY + 5,
mouseX,
mouseY - 10
);
if (timer >= 30) {
fill(255);
textSize(24);
textAlign(CENTER, CENTER);
text("OBJECTIVE: SURVIVE", width / 2, height / 2);
} else if (timer < 30) {
displayTimer();
for (let i = 0; i < 120; i++) {
rain[i].draw();
rain[i].move();
rain[i].checkForCollisions();
ctr++;
if (ctr > 2) {
ctr = 0;
}
}
if ((health > 0 && timer == 0) || win == 1) {
background(0);
fill(255);
textSize(24);
textAlign(CENTER, CENTER);
text("MISSION ACCOMPLISED", width / 2, height / 2);
win = 1;
resetGame();
} else if ((health <= 0 && timer != 0) || lost == 1) {
background(0);
fill(255);
textSize(24);
textAlign(CENTER, CENTER);
text("GAME OVER", width / 2, height / 2);
lost = 1;
resetGame();
}
}
}