xxxxxxxxxx
194
// Vivianna Mo
// Intro to IM - Michael Shiloh
// Oct. 13, 2022
// Midterm Project
// Description: A game where you catch the food falling, and if you miss one it's game over!
// Note: the class Food is in a separate js file for better readability
// Oct. 13: make a few adjustments to the instructions to make it more clear for the player
let gameState = "start";
// images and class variables
let titleImg;
let bkgImg;
let endImg;
let foods = [];
let addFoods = [];
// sound variables
let bkgSong;
let gameOverSong;
let eatSound;
let score = 0;
let highScore = 0;
function preload() {
bkgImg = loadImage("Assets/background.jpg");
titleImg = loadImage("Assets/titleImg.jpg")
character = loadImage("Assets/noFace.png");
for (let i = 0; i < 5; i++) {
foods[i] = loadImage("Assets/food" + i + ".png")
}
endImg = loadImage("Assets/end.gif");
bkgSong = loadSound("Assets/themeSong.mp3");
gameOverSong = loadSound("Assets/endSong.mp3");
eatSound = loadSound("Assets/eatSound.mp3");
}
function setup() {
createCanvas(755, 575);
bkgSong.loop();
bkgSong.playMode("restart");
bkgSong.setVolume(0.7);
gameOverSong.playMode("restart");
eatSound.playMode("restart");
addFoods.push(new Food()); // This makes sure that the food is added to the list
}
function draw() {
if(gameState == 'start') {
instructionScreen();
} else if(gameState == 'play') {
drawGame();
} else if(gameState == 'end') {
gameOver();
}
// print(addFoods.length);
// print(gameState);
}
function instructionScreen() {
background("#E7A64C");
if(!bkgSong.isPlaying()){
bkgSong.play();
}
image(titleImg, 40, height/2 - 120);
textFont("Silkscreen");
fill(128, 43, 31, 220);
textSize(65);
text("feed no face", 110, 130);
fill(207,62,40);
text("feed no face", 115, 133);
textSize(24);
text("Press enter to start", width/2 - 165, height/2 + 150);
push();
noStroke();
fill(50);
rectMode(CENTER);
rect(width/2, height/2 - 15, 350, 220, 20);
pop();
fill(230);
textFont("Ubuntu");
text("When the food reaches No", width/2 - 145, height/2 - 80);
text("Face's face CLICK with the", width/2 - 140, height/2 - 50);
text("cursor to eat the food!", width/2 - 125, height/2 - 20);
text("Don't let it go past his face", width/2 - 145, height/2 + 30);
text("or he'll be hangry...", width/2 - 105, height/2 + 60);
if(keyCode == ENTER) {
gameState = "play";
}
}
function drawGame() {
// Create the background image to fit the canvas size
bkgImg.resize(755, 575);
image(bkgImg, 0, 0);
// Display the score
textSize(56);
textFont("Silkscreen");
fill(255);
text(score, width - 110, 80);
// Function that draws the character
noFace();
// Flappy Bird Coding Challenge (The Coding Train):
// https://editor.p5js.org/codingtrain/sketches/FtBc2BhHM
for(let i = addFoods.length - 1; i >= 0; i--) {
addFoods[i].rainingFood();
addFoods[i].missedFood();
addFoods[i].show();
}
if(frameCount % 100 == 0) { // Adds new food to the array
addFoods.push(new Food());
} else if(frameCount % 250 == 0) {
addFoods.push(new Food());
}
}
function gameOver() {
bkgSong.stop();
if(!gameOverSong.isPlaying()) {
gameOverSong.play();
}
background("#231F20");
image(endImg, width/2 - 230, height - 280);
// Keeps track of the best score
if(highScore < score) {
highScore = score;
} else {
highScore = highScore;
}
push();
textFont("Silkscreen");
fill("#BB4430");
textSize(75);
text("OH NO!", 230, 80);
text("He's Hangry", 90, 150);
textSize(44);
fill("#E7A64C");
text("SCORE: " + score, width/2 - 120, height/2 - 80);
text("BEST: " + highScore, width/2 - 100, height/2 - 40);
textSize(24);
fill(187, 68, 48, 150);
text("Press backspace to restart", width/2 - 215, height/2);
pop();
if (keyCode == BACKSPACE) {
gameState = "start";
gameOverSong.stop();
score = 0;
addFoods.splice(0, addFoods.length);
}
}
// This adds the character no Face to the game
function noFace() {
push();
scale(0.7);
image(character, mouseX - 125, height + 50);
pop();
}
function mousePressed() {
// Removing objects from arrays (The Coding Train):
// https://www.youtube.com/watch?v=tA_ZgruFF9k
for(let i = addFoods.length - 1; i >= 0; i--) {
if(addFoods[i].checkFoodEatenAt(mouseX, mouseY)) {
addFoods.splice(i, 1);
score += 1;
}
}
}