xxxxxxxxxx
// Background color
var bg;
// Font for title and text
var insideout;
// Text
var txtcolor;
var headertxt;
var descriptiontxt;
// Circles
var szcircle = 150;
var circleclr = '#491763';
var space = 200; //space between circles
var startx = 100; //x position
var starty = 100; //y position
//make array for happy images with sound and image attached
var happyimages = [];
//make array for nonhappy images with sound and imgs
var nonhappyImages = [];
//revealing array
var revealed = [];
//different scenes
var sceneId = 0;
//startscene
var startbtn;
var buttonClicked = false;
//gamescreen
var happycount = 0; //track the no. of happy sounds revealed
//win screen
var wintxt;
var wingif;
var btnreplay;
function preload() {
//load font
insideout = loadFont('insideout.ttf');
// Load images and sounds in an array of objects
//sound and images grouped as the prompt describes the sound played
happyImages = [
//happy
{ img: loadImage('children.png'), sound: loadSound('children.mp3') },
{ img: loadImage('coffee.png'), sound: loadSound('coffee.mp3') },
{ img: loadImage('food.png'), sound: loadSound('food.mp3') },
{ img: loadImage('money.png'), sound: loadSound('money.mp3') },
{ img: loadImage('metro.png'), sound: loadSound('metro.mp3') },
{ img: loadImage('order.png'), sound: loadSound('order.mp3') }
];
nonhappyImages = [
//sad
{ img: loadImage('away.png'), sound: loadSound('away.mp3') },
{ img: loadImage('death.png'), sound: loadSound('death.mp3') },
{ img: loadImage('walk.png'), sound: loadSound('walk.mp3') },
//fear
{ img: loadImage('alone.png'), sound: loadSound('alone.mp3')},
{ img: loadImage('drill.png'), sound: loadSound('drill.mp3')},
{ img: loadImage('stage.png'), sound: loadSound('stage.mp3')},
//anger
{ img: loadImage('alarm.png'), sound: loadSound('alarm.mp3')},
{ img: loadImage('mosquitto.png'), sound: loadSound('mosquitto.mp3')},
{ img: loadImage('pen.png'), sound: loadSound('pen.mp3')},
//disgust
{ img: loadImage('chewing.png'), sound: loadSound('chewing.mp3')},
{ img: loadImage('drain.png'), sound: loadSound('drain.mp3')},
{ img: loadImage('trash.png'), sound: loadSound('trash.mp3')}
];
//winning gif
wingif = createImg('confetti.gif', '');
//hide gif initially
wingif.hide();
}
function setup() {
createCanvas(600, 600);
//define bg color
bg = '#faedcd';
background(bg);
winningImages();
//text
headertxt = "HUNT FOR HAPPINESS";
descriptiontxt = "Find 3 sounds of joy to win!";
txtcolor = '#2f0f40';
//start button
startbtn = createButton('Start');
startbtn.position(width / 2 + 300, 400);
startbtn.size(250,100);
startbtn.style("border-radius","100px");
startbtn.style("font-family", "insideout");
startbtn.style("font-size", "65px");
startbtn.style("background", "#2f0f40");
startbtn.style("color", "#fff");
startbtn.style('transition', 'all 0.5s ease-in-out');
startbtn.mousePressed(startBtnPressed);
//winning mechanism
winningImages();
//replay button
btnreplay = createButton('Replay');
btnreplay.position(width / 2 + 300, 400);
btnreplay.size(250, 100);
btnreplay.style("font-family", "insideout");
btnreplay.style("font-size", "65px");
btnreplay.style("border-radius","100px");
btnreplay.style("background", "#2f0f40");
btnreplay.style("color", "#fff");
btnreplay.mousePressed(replayPressed);
}
function draw() {
background(bg);
if (sceneId == 0) {
showstartscreen();
} else if (sceneId == 1) {
gamescreen();
} else if (sceneId == 2) {
winscreen();
}
}
function showstartscreen() {
// show start button
startbtn.show();
//hide win screen elements
btnreplay.hide();
wingif.hide();
//text
headertxt = "Hunt for Happiness";
descriptiontxt = "Find three sounds of joy to win the game!";
txtcolor = '#2f0f40';
// draw the text
textAlign(CENTER);
textSize(75);
textFont(insideout);
fill(txtcolor);
text(headertxt, width / 2, 150); // header txt
textSize(28);
text(descriptiontxt, width / 2, 240); // description txt
}
function startBtnPressed() {
startbtn.hide();
sceneId = 1;
buttonClicked = true;
setTimeout(() => {
buttonClicked = false;
}, 100);
}
function gamescreen() {
noStroke();
// Draw circles and images
var index = 0;
for (var row = 0; row < 3; row = row + 1) {
for (var col = 0; col < 3; col = col + 1) {
var x = startx + col * space;
var y = starty + row * space;
if (revealed[index]) {
//display image
imageMode(CENTER);
image(shuffledImages[index].img, x, y, 180, 180);
} else {
//display circle
fill(circleclr);
ellipse(x, y, szcircle);
}
index = index + 1;
}
}
}
function winningImages() {
//create an empty array to hold the selected images
var selectedImages = [];
//select 3 unique happy images
while (selectedImages.length < 3) {
var happyImage = random(happyImages); //create variable to choose randomly from happyimage array
if (!selectedImages.includes(happyImage)) { //avoid same img being added twice
selectedImages.push(happyImage); //push ims into the empty selectedimages array
}
}
//fill the remaining slots with unique non-happy images
while (selectedImages.length < 9) {
var nonHappyImage = random(nonhappyImages); //same logic as the happyimages selection
if (!selectedImages.includes(nonHappyImage)) {
selectedImages.push(nonHappyImage);
}
}
// Shuffle the selected images for random placement
shuffledImages = shuffle(selectedImages);
// Reset the revealed array
revealed = Array(9).fill(false);
//reset the happy count
happycount = 0;
}
function mousePressed() {
//avoid double click issue
if (buttonClicked) return;
if (sceneId == 1) {
var index = 0;
for (var row = 0; row < 3; row = row + 1) {
for (var col = 0; col < 3; col = col + 1) {
var x = startx + col * space;
var y = starty + row * space;
if (dist(mouseX, mouseY, x, y) < szcircle / 2 && !revealed[index]) {
revealed[index] = true; //reveal image
playSound(index); //play corresponding sound
// Check if the revealed image is happy
if (happyImages.some(img => img.img == shuffledImages[index].img)) {
//add to happy count
happycount = happycount + 1;
// check if 3 happy images have been revealed
if (happycount == 3) {
//give a little pause when game is won
setTimeout(() => {
sceneId = 2; // go to the win screen
}, 800);
}
}
return;
}
index = index + 1;
}
}
}
}
function winscreen() {
// show replay button
btnreplay.show();
//stop sounds
stopAllsounds();
// Set the text color and content
wintxt = "YOU WIN!";
txtcolor = '#2f0f40';
// Draw the text on the screen
textAlign(CENTER);
textSize(150);
textFont(insideout);
fill(txtcolor);
text(wintxt, width / 2, height / 2 - 80); // Display "YOU WIN!" in the center
//show the gif
wingif.position(420, 60);
wingif.show();
}
function stopAllsounds() {
//create function to stop other sounds
//if items in shuffle image array are playing then stop playing
for (var item of shuffledImages) {
if (item.sound.isPlaying()){
item.sound.stop();
}
}
}
function playSound(index) {
//call the stop all sounds function
stopAllsounds();
//play a sound when it is called in mousepressed
shuffledImages[index].sound.play();
}
function replayPressed() {
//reset game initial values
sceneId = 0; //back to the start screen
happycount = 0; //reset happy count
winningImages();
}