xxxxxxxxxx
105
let progress = 0; // Initial progress value
const progressSpeed = 0.007; // Speed at which the progress decreases
const progressIncrement = 0.085; // Value by which the progress increases when spacebar is pressed
let flag = 0;
let startTime = 0; // Variable to store the start time of the timer
let timerStarted = false; // Flag to check if the timer has started
let eruptionTime = 0; // Variable to store the eruption time in seconds
let volcano1, volcano2, volcano3; // Variables to store the images
let eruptionSound; // Variable to store the eruption sound
let eruptionSoundPlayed = false;
function preload() {
volcano1 = loadImage("volcano1.png"); // Load the "volcano1.png" image
volcano2 = loadImage("volcano2.png"); // Load the "volcano2.png" image
volcano3 = loadImage("volcano3.png"); // Load the "volcano3.png" image
eruptionSound = loadSound("volcanoeruption.mp3"); // Load the "volcanoeurption.mp3" sound file
shakeSound = loadSound("shake.mp3");
}
function setup() {
createCanvas(400, 400);
}
function draw() {
if (progress >= 0.95 || flag === 1) {
flag = 1;
// Set the background image as "volcano2.png"
background(volcano2);
// Display the eruption message
textAlign(CENTER, CENTER);
textSize(20);
fill(255, 0, 0); // Set text color to red
stroke(0); // Set outline color to black
strokeWeight(2); // Set outline thickness
let msg = "It took you " + eruptionTime + " seconds to erupt the volcano";
text(msg, width / 2, height - 20);
// Play the eruption sound
if (!eruptionSoundPlayed) {
eruptionSound.play();
eruptionSoundPlayed = true;
}
} else {
// Passive decrease of progress
progress -= progressSpeed;
// Set the background image as "volcano1.png" or "volcano3.png" based on progress
if (progress >= 0.45) {
background(volcano3);
} else {
background(volcano1);
}
// Shake the screen if spacebar is pressed
if (keyIsPressed && (key === " " || keyCode === UP_ARROW)) {
translate(
random(-3 + progress, 3 + progress),
random(-3 + progress, 3 + progress)
);
}
// Draw the progress bar
let progressBarHeight = progress * height;
fill(255 * progress, 255 - 255 * progress, 255 - 255 * progress); // Set text color to red
rect(10, height - progressBarHeight, 30, progressBarHeight);
// Display the timer if it has started
if (timerStarted) {
let elapsedTime = millis() - startTime;
let seconds = Math.floor(elapsedTime / 1000);
let minutes = Math.floor(seconds / 60);
seconds %= 60;
textAlign(CENTER, CENTER);
textSize(20);
fill(255 * progress, 255 - 255 * progress, 255 - 255 * progress); // Set text color to red
stroke(0); // Set outline color to black
strokeWeight(2); // Set outline thickness
text(nf(minutes, 2) + ":" + nf(seconds, 2), width / 2, height - 20);
}
}
}
function keyPressed() {
if (key === " " || keyCode === UP_ARROW) {
// Respond to spacebar or up arrow key
if (!timerStarted) {
startTime = millis();
timerStarted = true;
}
progress += progressIncrement;
progress = constrain(progress, 0, 1);
if (progress >= 0.95 || flag === 1) {
eruptionTime = Math.floor((millis() - startTime) / 1000);
flag = 1;
} else {
shakeSound.play();
}
}
}