xxxxxxxxxx
623
let highscores = [];
let rain = [];
let range = 400;
let size = 30;
let intervalId;
let health = 3;
let ctr = 0;
let count = 0;
let lost = 0;
let win = 0;
let starttime = 2;
let timer = 0;
let speedinc = 1;
let level = 1;
let retryText;
let mainMenuText;
let shield;
let shieldActive = false;
let shieldTimer = 10;
let powerupflag = 0;
let slowdown;
let slowdownActive = false;
let slowdownTimer = 2;
let clearActive = false;
let clearCooldown = 0;
let invincible = false;
let invincibilityframes = 90;
let framectr = 0;
let clear;
let score = 0;
let soundtrack;
let mainmenu;
let gameover;
let healthicon;
let shieldicon;
let slowdownicon;
let clearicon;
let damagesound;
let shieldsound;
let slowdownsound;
let speedupsound;
let storedScores;
let font;
let clicked = false;
function preload() {
//highscores = loadStrings("highscores.txt");
soundFormats("mp3", "ogg");
font = loadFont("Arcade Gamer.ttf");
soundtrack = loadSound("soundtrack.mp3");
damagesound = loadSound("Damage.mp3");
shieldsound = loadSound("shield.mp3");
slowdownsound = loadSound("slowdown.mp3");
speedupsound = loadSound("speedup.mp3");
mainmenu = loadImage("mainmenu.png");
gameover = loadImage("gameover.png");
healthicon = loadImage("health.png");
shieldicon = loadImage("shield.png");
slowdownicon = loadImage("Slowdown.png");
clearicon = loadImage("Forcefield.png");
storedScores = getItem("highscores");
}
function Audio() {
soundtrack.playMode("sustain");
soundtrack.play();
}
function UI() {
fill(47, 79, 79);
rect(0, 0, 400, 50);
push();
tint(255, 255 / (clearCooldown + 0.1));
image(clearicon, (3 * width) / 4 - 15, 15, 25, 25);
pop();
for (i = 0; i < health; i++) {
image(healthicon, (3 * width) / 4 + 15 + i * 27, 15, 25, 25);
}
if (shieldActive) {
push();
tint(255, 255 / (10 - shieldTimer));
image(shieldicon, 15, 15, 25, 25);
pop();
}
if (slowdownActive) {
push();
tint(255, 255 / (2 - slowdownTimer));
image(slowdownicon, 15 + 27, 15, 25, 25);
pop();
}
}
function goToMainMenu() {
level = 0;
resetGame();
}
function resetGame() {
rain = [];
range = 400;
size = 30;
health = 3;
ctr = 0;
count = 0;
lost = 0;
win = 0;
speedinc = 0;
starttime = 2;
timer = 0;
shieldActive = false;
shieldTimer = 10;
slowdownActive = false;
slowdownTimer = 2;
powerupflag = 0;
for (let i = 0; i < 120; i++) {
rain.push(new raindrop());
}
}
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);
}
updatespeed() {
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 && invincible == false) {
this.xPos = mouseX + random(-range, range);
this.yPos = 0;
this.ySpeed = random(2, 4);
if (shieldActive == 0 && invincible == false) {
damagesound.play();
health--;
invincible = true;
} else {
shieldActive = false;
invincible = true;
shieldTimer = 5;
}
console.log("Collision detected!");
} else if (this.yPos >= height - 10) {
this.xPos = mouseX + random(-range, range);
this.yPos = 0;
this.ySpeed = random(2, 3) + speedinc;
}
}
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);
}
}
class Shield {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 20;
this.img = shieldicon;
}
draw() {
image(
this.img,
this.x - this.size / 2,
this.y - this.size / 2,
this.size,
this.size
);
}
checkForCollisions() {
let distance = dist(mouseX, mouseY, this.x, this.y);
if (distance < this.size / 2) {
print("Picked up shield");
shieldsound.play();
shieldActive = true;
powerupflag = 0;
}
}
}
class Slowdown {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 20;
this.img = slowdownicon;
}
draw() {
image(
this.img,
this.x - this.size / 2,
this.y - this.size / 2,
this.size,
this.size
);
}
checkForCollisions() {
let distance = dist(mouseX, mouseY, this.x, this.y);
if (distance < this.size / 2) {
print("Picked up Slowdown");
slowdownsound.play();
slowdownActive = true;
powerupflag = 0;
}
}
}
class Clear {
//create a circle an the cursor which enlarges
//if the circle touches the raindrop, the raindrop is removed
//if the circle is too big, the circle is removed
constructor() {
this.x = mouseX;
this.y = mouseY;
this.size = 5;
}
draw() {
//make circle light blue
fill(100, 100, 230);
ellipse(this.x, this.y, this.size, this.size);
}
checkForCollisions() {
for (let i = 0; i < rain.length; i++) {
let distance = dist(this.x, this.y, rain[i].xPos, rain[i].yPos);
if (distance < this.size / 2) {
rain[i].xPos = mouseX + random(-range, range);
rain[i].yPos = 0;
rain[i].ySpeed = random(2, 3);
}
}
}
update() {
this.x = mouseX;
this.y = mouseY;
this.size += 3;
}
checkSize() {
if (this.size > 200) {
return true;
}
return false;
}
}
function loadScores() {
let storedScores = getItem("highscores");
if (storedScores) {
highscores = storedScores;
console.log("Highscores loaded:", highscores);
} else {
console.log("No highscores found.");
}
}
function saveScores() {
// make changes to the highscores array here...
storeItem("highscores", highscores);
console.log("Highscores saved:", highscores);
}
function setup() {
loadScores();
highscores.sort((a, b) => b.score - a.score);
createCanvas(400, 500);
for (let i = 0; i < 120; i++) {
rain.push(new raindrop());
}
shield = new Shield(random(50, width - 50), random(150, height - 50));
slowdown = new Slowdown(random(50, width - 50), random(150, height - 50));
clear = new Clear();
soundtrack.play();
}
function mouseClicked() {
if (!clicked) {
clicked = true; // set the clicked variable to true
}
}
function draw() {
if (level == 0) {
noCursor();
background(0);
UI();
//if space is pressed activate clear
if ((keyIsDown(32) || clicked) && clearCooldown <= 0) {
clearActive = true;
clearCooldown = 22;
} else if (keyIsDown(32) || clicked ) {
print("clear is on cooldown!");
}
if (clearActive == true) {
clear.draw();
clear.checkForCollisions();
clear.update();
if (clear.checkSize() == true) {
clearActive = false;
clear.size = 5;
}
}
if (powerupflag > 0) {
if (powerupflag == 1) {
shield.draw();
shield.checkForCollisions();
} else if (powerupflag == 2) {
slowdown.draw();
slowdown.checkForCollisions();
}
}
if (invincible) {
framectr++;
if (framectr >= invincibilityframes) {
invincible = false;
framectr = 0;
}
}
if (shieldActive == true) {
fill(100, 100, 230);
triangle(
mouseX - 13,
mouseY + 7,
mouseX + 13,
mouseY + 7,
mouseX,
mouseY - 14
);
}
if (invincible == false) {
fill(200, 200, 100);
} else {
fill(200, 200, 100, 100);
}
triangle(
mouseX - 10,
mouseY + 5,
mouseX + 10,
mouseY + 5,
mouseX,
mouseY - 10
);
if (frameCount % (5 * 60) == 0) {
speedinc = speedinc + 0.125;
}
if (frameCount % (20 * 60) == 0) {
let choices = [1, 2];
powerupflag = random(choices);
print("powerup");
}
if (frameCount % 60 == 0) {
if (shieldActive == true) {
shieldTimer--;
if (shieldTimer <= 0) {
shieldActive = false;
shieldTimer = 10;
}
}
if (slowdownActive == true) {
slowdownTimer--;
if (slowdownTimer <= 0) {
slowdownActive = false;
slowdownTimer = 2;
}
}
if (clearCooldown > 0) {
clearCooldown--;
}
timer++;
}
if (timer <= starttime) {
// fill(255);
// textSize(24);
// textAlign(CENTER, CENTER);
// text("OBJECTIVE: SURVIVE", width / 2, height / 2);
} else if (timer > starttime) {
//displayTimer();
for (let i = 0; i < 120; i++) {
noStroke();
rain[i].draw();
if (slowdownActive == true) {
rain[i].ySpeed = 1.5;
}
rain[i].move();
rain[i].checkForCollisions();
ctr++;
if (ctr > 2) {
ctr = 0;
}
}
if (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) {
score = timer;
let name = prompt(
"Please enter your name to add you score to the leaderboards:"
);
if(name){
highscores.push({ name, score });
highscores.sort((a, b) => b.score - a.score);
highscores.splice(10); // Keep only the top 10 high scores
}
saveScores();
resetGame();
level = 2;
}
}
} else if (level == 1) {
background(0);
image(
mainmenu,
0,
0,
width,
height,
0,
0,
mainmenu.width,
mainmenu.height,
CONTAIN,
LEFT
);
fill(255);
textSize(24);
textAlign(CENTER, CENTER);
resetGame();
fill(100, 100, 230, 1);
stroke(0);
if (
mouseX > width / 2 - 75 &&
mouseX < width / 2 + 75 &&
mouseY > height / 2 - 25 &&
mouseY < height / 2 + 25
) {
stroke(100, 100, 230);
rect(width / 2 - 45, height / 2 - 15, 87, 30);
if (clicked) {
level = 0;
}
}
if (
mouseX > width / 2 - 97 &&
mouseX < width / 2 + 97 &&
mouseY > height / 2 - 25 + 45 &&
mouseY < height / 2 + 25 + 45
) {
stroke(100, 100, 230);
rect(width / 2 - 97, height / 2 + 45, 193, 30);
if (clicked) {
level = 3;
}
}
} else if (level == 2) {
cursor(CROSS);
background(0);
image(
gameover,
0,
0,
width,
height,
0,
0,
gameover.width,
gameover.height,
CONTAIN,
LEFT
);
fill(255);
textFont(font);
textSize(24);
textAlign(CENTER, CENTER);
fill(100, 100, 230, 1);
if (
mouseX > width / 2 - 75 &&
mouseX < width / 2 + 75 &&
mouseY > height / 2 - 25 &&
mouseY < height / 2 + 25
) {
stroke(230, 100, 100);
rect(width / 2 - 55, height / 2 - 15, 107, 30);
if (clicked) {
level = 0;
}
}
if (
mouseX > width / 2 - 170 &&
mouseX < width / 2 + 170 &&
mouseY > height / 2 - 25 + 45 &&
mouseY < height / 2 + 25 + 45
) {
stroke(230, 100, 100);
rect(width / 2 - 170, height / 2 + 45, 337, 30);
if (clicked) {
resetGame();
level = 1;
}
}
} else if (level == 3) {
background(0);
textFont(font);
textSize(26);
fill(255);
let title = "High Scores:";
let titleWidth = textWidth(title);
text(title, width / 2, height / 2 - 115);
textSize(16);
for (let i = 0; i < highscores.length; i++) {
let scoreText = `${highscores[i].name}: ${highscores[i].score}`;
text(scoreText, width / 2, 150 + i * 30 + 60);
}
textSize(12);
fill(255);
text("< Main Menu", 77, 25);
if (mouseX >= 5 && mouseX <= 145 && mouseY >= 10 && mouseY <= 40) {
push();
fill(0, 0, 0, 1);
stroke(255);
rect(5, 10, 145, 30);
pop();
if (clicked) {
resetGame();
level = 1;
}
}
}
clicked = false; // reset the clicked variable
}