xxxxxxxxxx
195
// Zhaniya Korpebayeva - Oct.13, 2022
// Introduction to Interactive Media- Midterm
// Collect 11 flowers to create your own bouquet. With each flower, the speed will increase. If Sofia doesn't catch the flower, then the game will be lost.
let flower;
let x = 50;
let y = 0;
let fwspeed = 2;
let stage = 0;
// images and sound
let bg;
let sofia;
let flowers;
let home;
let lost;
let win;
let bgsound;
let winSound;
let loseSound;
// score calculator
let score = 0;
//uploading images and sound
function preload() {
bg = loadImage("Images/background.jpg");
sofia = loadImage("Images/sofi.png");
flowers = loadImage("Images/flower.png");
home = loadImage("Images/start.jpg");
lost = loadImage("Images/try.png");
win = loadImage("Images/gamewonn.jpg");
bgsound = loadSound("Images/bkgsound.mp3");
winSound = loadSound("Images/winsound.mp3")
loseSound = loadSound("Images/losesound.mp3")
}
function setup() {
createCanvas(600,600);
backgroundMusic();
}
// bg sound
// https://www.youtube.com/watch?v=uHNgkQsHLXQ
function backgroundMusic(){
bgsound.play();
bgsound.loop();
bgsound.setVolume(0.3);
userStartAudio();
}
// game stages/screens for each ending and homepage
// tutorial from Mr.Erdreich
// https://www.youtube.com/watch?v=SpfJUlSusj0
function draw() {
// === means strict equality
if (stage === 0) {
homeScreen();
}
if (stage === 1) {
startGame();
}
if (stage === 2) {
finishGame();
}
if (stage === 3){
gameWon();
}
}
// stages/screens to be displayed in each case
function mousePressed() {
if (stage === 0) {
stage = 1;
}
if (stage === 2) {
stage = 0;
}
if (stage === 3) {
stage = 0;
}
}
// Game start page
function homeScreen() {
background(home);
textAlign(CENTER);
fill(0);
textSize(18);
textFont('Georgia');
textStyle(BOLDITALIC);
text("Welcome To The Garden Walk Game!", width / 2, 280);
textSize(13);
text("💚click anywhere to start collecting your bouquet💚", width / 2, 310);
text("🌱Please note that to collect a bouquet you must catch 11 flowers🌱", 360, 570)
restart();
}
// you won page
function gameWon() {
background(win);
textAlign(CENTER);
fill(0);
textSize(18);
textFont('Georgia');
textStyle(BOLDITALIC);
text("🥳Congratulations🥳", width / 2, 280);
textSize(13);
text("💚click anywhere to start collecting your bouquet💚", width / 2, 310);
restart();
}
//Function for the game
function startGame() {
background(bg);
//Sofia-rectangle shape was a placeholder before
// rectMode(CENTER);
// fill(255);
// rect(mouseX, height - 20, 30,30);
image(sofia,mouseX, height-150, 150, 150);
//score text display
fill(0);
noStroke();
textSize(18);
textStyle(BOLD);
text("🌸Score: " + score, 50, 20);
//change the speed y- flower
y += speed;
// Flower- circle was the initial placeholder
// fill(255);
// stroke(255);
// strokeWeight(5);
// favourite The Coding Train https://www.youtube.com/watch?v=FVYGyaxG4To
image(flowers, x, y, 35, 35);
//this condition displays game lose screen if the flower is outside the canvas+ sound
if (y > height) {
loseSound.play();
stage = 2;
}
// condition to win the game, player has to collect 11 flowers to win the game+ sound
if (score >= 12){
winSound.play();
stage = 3 ;
}
//The collision of flower and sofia
if (y > height - 100 && x > mouseX - 100 && x < mouseX + 100) {
y = 0;
score++;
speed += 1.0;
x = random(width);
}
}
//game lose screen in case if any flower falls outside the canva
function finishGame() {
background(255, 204, 204);
image(lost, 80, 130, 450, 450);
noStroke();
fill(255, 77, 77);
textAlign(CENTER);
textStyle(BOLD);
textSize(15);
textFont('Georgia');
text("Oh no!", 510, 30);
text("your total score is : " + score, 510, 70);
text("💐Click To Play Again💐", 510, 110);
}
//restarting the function from homepage and with 0 score
function restart() {
y = 0;
speed = 2;
score = 0;
}