xxxxxxxxxx
332
//Create Variables as Needed
let mpFont;
let gameState = 'start';
let goomba = [];
let randI;
let photo;
let p1Click = false;
let p2Click = false;
let tally;
let p1Tally;
let p2Tally;
let restartCheck = false;
let frameCheck;
let gameEnd = false;
let winner;
let loser;
let allWin = false;
let allLose = false;
let gameMusic;
let startSFX;
let finishSFX;
//Preload Font, Images, Sounds
function preload() {
mpFont = loadFont('MPFont.ttf');
photo = loadImage("Goomba.png");
foreground = loadImage("Foreground.png");
backdrop = loadImage("Background.png");
soundFormats('mp3');
gameMusic = loadSound("Minigame.mp3");
soundFormats('wav');
startSFX = loadSound("start.wav");
finishSFX = loadSound("finish.wav");
}
//Create a Class for Tally
class Tally {
constructor(){
this.p1Number = 0;
this.p2Number = 0;
}
//Keeps Track of Each Press; One Press = One Tally
checkP1Click(){
this.p1Number ++;
}
checkP2Click(){
this.p2Number ++;
}
checkTally(){
print(this.p1Number);
print(this.p2Number);
p1Tally = this.p1Number
p2Tally = this.p2Number
}
//Compare Tally to Actual Number for Results
checkResults(){
if(this.p1Number === randI && this.p2Number === randI){
print("Both Win!");
winner = 'Everyone';
allWin = true;
} else if(this.p1Number === randI){
print("P1 Win!");
winner = 'Player 1';
loser = 'Player 2';
} else if(this.p2Number === randI){
print("P2 Win!");
winner = 'Player 2';
loser = 'Player 1';
} else{
winner = 'Nobody';
loser = 'Both of you'
allLose = true;
}
}
//Resets Tally to 0
resetTally(){
this.p1Number = 0;
this.p2Number = 0;
}
}
//Create Class for Goombas
class Goomba {
constructor(){
this.xPos = 0;
this.yPos = 492;
this.xSpeed = random(5, 10);
this.ySpeed = 0;
this.check = false;
}
//Used to "Stall" Each Goomba to Make Them Come Out in Order
stall(order){
if((frameCount - frameCheck) / (25 * (order)) == 1){
this.check = true;
}
}
//Changes Goomba's Position Based on Speed
move(){
if(this.check == true){
this.xPos += this.xSpeed;
this.yPos += this.ySpeed;
}
}
//Draws Goomba
draw(){
image(photo, this.xPos, this.yPos, 60, 70);
}
//Constantly Randomizes Goomba Speeds
random(){
if(frameCount % 20 == 0){
this.xSpeed = random(0, 15);
}
}
//Checks if Game has Ended (If Last Goomba has Passed)
checkGameEnd(order){
if(order == randI){
if(this.xPos > 820){
gameEnd = true;
}
}
}
//Resets Position and Speed of Goombas for Another Round
reset(){
if(this.xPos > 820){
this.check = false;
this.xPos = 0;
this.xSpeed = random(5, 10);
}
}
}
//Function for Start Screen
function startScreen(){
//Make Gradient Background
let from = color(255, 135, 0);
let to = color(0, 240, 255);
noStroke();
for (let i = 0; i < 664; i = i + 6.64){
fill(lerpColor(from, to, 0 + i * 0.0015))
rect(0, i, 820, 7);
}
//Make Inner Rectangle for Text
fill(200);
strokeWeight(4);
rect(15, 95, 785, 555, 20);
//Write Text
fill('#ED225D');
textFont(mpFont);
textSize(100);
text('Goomba Spotting', 60, 80)
textSize(50);
text('Goombas have invaded NYUAD!', 25, 130);
text('You two have been hired as', 25, 170);
text('Goomba Spotters to assist with', 25, 210);
text('the resistance effort.', 25, 250);
text('Your task: To count the exact', 25, 290);
text('number of Goombas that pass by.', 25, 330);
text('A last-minute refresher!', 25, 370);
text('Goombas look like:', 25, 430);
text('Tap once for each Goomba you see!', 25, 490);
text('Player 1: Tap on the A key!', 25, 530);
text('Player 2: Tap on the L key!', 25, 570);
text('Good luck, Spotters.', 25, 630);
image(photo, 420, 380, 58.8, 70);
//Make Start Button
strokeWeight(4);
rect(610, 510, 190, 140, 20);
fill(0);
text('START!', 615, 595);
}
//Function for End Screen
function endScreen(){
//Make Gradient Background
let from = color(255, 135, 0);
let to = color(0, 240, 255);
noStroke();
for (let i = 0; i < 664; i = i + 6.64){
fill(lerpColor(from, to, 0 + i * 0.0015))
rect(0, i, 820, 7);
}
//Make Inner Rectangle for Text
fill(200);
strokeWeight(4);
rect(15, 95, 785, 555, 20);
//Write Text
fill('#ED225D');
textFont(mpFont);
textSize(100);
text('Results', 250, 80)
textSize(50);
text("Okay, let's see here.", 25, 130);
text('Player 1.', 25, 170);
text('You counted ' + p1Tally + ' Goombas.', 25, 210);
text('Player 2.', 25, 250);
text('You counted ' + p2Tally + ' Goombas.', 25, 290);
text('There were ' + randI + ' Goombas.', 25, 350);
//Vary Dialogue Depending on Results
if (allWin == true){
text('Both of you, great work!', 25, 410);
text('See me in my office.', 25, 450);
text('Promotions await you.', 25, 490);
text(winner + ' WINS!', 25, 530);
} else if (allLose == true){
text('Both of you, see me in my office.', 25, 410);
text("...You won't like what awaits you.", 25, 450);
text(winner + ' WINS!', 25, 490);
} else{
text(winner + ', great work!', 25, 410);
text('A promotion awaits you.', 25, 450);
text(loser + ', see me in my office.', 25, 490);
text("...I await you.", 25, 530);
text(winner + ' WINS!', 25, 570);
}
//Make Restart Button
strokeWeight(4);
rect(610, 510, 190, 140, 20);
fill(0);
textSize(39);
text('RESTART!', 613, 595);
}
//Function for Game Screen
function gameScreen(){
//Background Image
image(backdrop, 0, 0, 820, 664);
//Functions for Each Goomba; *Different # Each Time Using randI*
for (let i = 0; i < randI; i++){
goomba[i].stall(i + 1);
goomba[i].draw();
goomba[i].move();
goomba[i].random();
goomba[i].checkGameEnd(i + 1);
}
//Foreground Image
image(foreground, 0, 0, 820, 664);
//Makes Finish Button Appear After Last Goomba has Passed
if (gameEnd == true){
fill('#ED225D');
strokeWeight(4);
rect(610, 510, 190, 140, 20);
fill(0);
textSize(50);
text('Check', 640, 570);
text('Results!', 615, 615);
}
}
function setup() {
//Canvas Initialization
createCanvas(820, 664);
//Generate 30 Values in Array for 30 Goombas
for (let i = 0; i < 30; i++){
goomba[i] = new Goomba();
}
//Generates Value for Tallying
tally = new Tally();
}
//Draw Function: Displays Different Screen Based on gameState
function draw() {
//For Start
if (gameState == 'start') {
startScreen();
} //For Playing
else if (gameState == 'playing') {
//Reinitializes Values As Needed
if (restartCheck == false){
restartCheck = true;
allWin = false;
allLose = false;
tally.resetTally();
randI = random(15, 30);
randI = Math.round(randI);
//*Current Frame is Recorded and Used for Delaying Goombas*
frameCheck = frameCount;
}
gameScreen();
} //For End
else if (gameState == 'end') {
//Reinitializes Values As Needed
if (restartCheck == true){
for (let i = 0; i < randI; i++){
goomba[i].reset();
}
restartCheck = false;
gameEnd = false;
//Checks Tally & Results
tally.checkTally();
tally.checkResults();
}
endScreen();
}
}
//Function for Checking Key Presses
function keyPressed() {
//Player 1: A Key
if (keyCode === 65) {
tally.checkP1Click();
}
//Player 2: L Key
if (keyCode === 76) {
tally.checkP2Click();
}
}
//Function for Checking Mouse Clicks; *Changes gameState*
function mousePressed() {
//For Start Button
if (gameState == 'start') {
if (610 < mouseX && mouseX < 800 && 510 < mouseY && mouseY < 650){
startSFX.play();
gameMusic.play();
gameState = 'playing';
}
} //For Finish Button
else if (gameState == 'playing'){
if (gameEnd == true){
if (610 < mouseX && mouseX < 800 && 510 < mouseY && mouseY < 650){
finishSFX.play();
gameMusic.stop();
gameState = 'end';
}
}
} //For Restart Button
else if (gameState == 'end'){
if (610 < mouseX && mouseX < 800 && 510 < mouseY && mouseY < 650){
gameState = 'start';
}
}
}