let numMatches = 0; //to see if all matches found and game ends
let numTries = 0; //counter to keep track of counts
//10 card image variables
//array variables for image of cards and tiles etc
//variables for images and sounds
let gameState; //to enter different stages: instructions, game, end
//loading all images and sounds
flipSide = loadImage("back.jpg");
card1 = loadImage("card1.jpg");
card2 = loadImage("card2.jpg");
card3 = loadImage("card3.jpg");
card4 = loadImage("card4.jpg");
card5 = loadImage("card5.jpg");
card6 = loadImage("card6.jpg");
card7 = loadImage("card7.jpg");
card8 = loadImage("card8.jpg");
card9 = loadImage("card9.jpg");
card10 = loadImage("card10.jpg");
startgame = loadImage ("startgame.png");
backgroundImage = loadImage ("game.png");
instructionsImage = loadImage("instructions.png")
endImage = loadImage("end.jpg");
mymusic = loadSound('Baisara-Beera-Kalpana-Patowary-Papon (mp3cut.net).mp3');
flipsound = loadSound ('Card-flip-sound-effect-1.mp3');
clicksound = loadSound('mixkit-game-click-1114.wav');
// Set up canvas and other initialization tasks
mymusic.loop(); //stays on throughout
//draws back if not face up otherwise the image of food
image(this.face, this.x, this.y, this.size, this.size);
image(flipSide, this.x, this.y, this.size, this.size);
//black outline of the tiles
rect(this.x, this.y, this.size, this.size,5);
//checks if when clicked the tile is under the mouse
return x >= this.x && x <= this.x + this.size && y >= this.y && y <= this.y + this.size;
//function to randomly shuffle the array of tiles with 2 copies each
function shuffleArray(array)
let counter = array.length;
// while there are elements in the array
//selecting a random index
let index = Math.floor(Math.random() * counter);
//swap last element with it
var temp = array[counter];
array[counter] = array[index];
if (gameState == 'start')
//displaying image for background of game
image(startgame, 0,0,600,700);
let c = color('#FF69B4');
else if (gameState == 'instructions')
//instruction page is an image I made from canva
image(instructionsImage,0,0,600,700);
else if (gameState == 'beingSet')
//this is just done once when the start button is clicked
faces = [card1, card2, card3,
card10]; //storing all images into array
// making an array of 2 cards and then randomising
possibleFaces = faces.slice(0);
for (let i = 0; i < (NUM_COLS * NUM_ROWS) / 2; i++)
// Randomly pick one from the array of remaining faces
var randomInd = floor(random(possibleFaces.length));
var face = possibleFaces[randomInd];
possibleFaces.splice(randomInd, 1);
shuffleArray(selected); //shuffling the array
// Creating a Tile object for each card
for (var i = 0; i < NUM_COLS; i++)
for (var j = 0; j < NUM_ROWS; j++)
var tileX = (i * 100) + 55+ (i*30);
var tileY = (j * 100) + 70 +(j*17);
var tileFace = selected.pop();
tiles.push(new Tile(tileX, tileY, tileFace));
// Changing the game state to 'playing'
else if (gameState == 'playing')
// Displaying the background image
image(backgroundImage, 0,0,600,700);
// Checking if enough time has passed since the last card flip
if (delayStartFC && (frameCount - delayStartFC) > 25)
// Hiding the flipped cards that are not matches
for (let i = 0; i < tiles.length; i++)
tiles[i].isFaceUp = false;
// Resetting the flippedTiles array and delayStartFC variable
// Drawing each tile on the canvas
for (let j = 0; j < tiles.length; j++)
// Checking if the game is over
if (numMatches == tiles.length/2)
else if (gameState == 'end')
image(endImage, 0,0,600,700);
let c = color('#d90166');
text('Play Again', 266, 355);
//diplaying the tries as a score for game
text("You found them all in " + numTries + " tries!", 146,488);
mouseClicked = function()
//checking ditance between click and start button
let distance = dist(mouseX, mouseY, 302.5, 350);
// checking distance between click and instructions button
if (mouseX>220 && mouseX<380 && mouseY>500 && mouseY<540)
gameState = 'instructions';
else if (gameState == 'playing')
//loop through all the tiles
for (let i = 0; i < tiles.length; i++)
if (tile.isUnderMouse(mouseX, mouseY))
// Check if the tile can be flipped (only if less than 2 tiles are flipped and the tile is not already face-up)
if (flippedTiles.length < 2 && !tile.isFaceUp)
flippedTiles.push(tile); //adds to flippedtiles array
if (flippedTiles.length === 2)
numTries++; //increments the tries counter
if (flippedTiles[0].face === flippedTiles[1].face) //if they match
flippedTiles[0].isMatch = true;
flippedTiles[1].isMatch = true;
flippedTiles.length = 0; //empties flipped array because match found
delayStartFC = frameCount;
else if (gameState == 'instructions')
//clicking the startgame button
if (mouseX > 149 && mouseX < 449 &&
mouseY > 600 && mouseY < 615)
else if (gameState == 'end')
//clicking restart button
let distance = dist(mouseX, mouseY, 302.5, 350);
let numMatches = 0; //to see if all matches found and game ends
let numTries = 0; //counter to keep track of counts
let delayStartFC;
//10 card image variables
let card1;
let card2 ;
let card3;
let card4;
let card5;
let card6;
let card7;
let card8;
let card9;
let card10;
//array variables for image of cards and tiles etc
let faces = [];
let flippedTiles = [];
let tiles = [];
let selected = [];
let possibleFaces;
let startgame;
//variables for images and sounds
let backgroundImage;
let instructionsImage;
let endImage;
let mymusic;
let flipsound;
let clicksound;
let flipSide;
let gameState; //to enter different stages: instructions, game, end
let NUM_COLS = 4;
let NUM_ROWS = 5;
function preload()
{
//loading all images and sounds
flipSide = loadImage("back.jpg");
card1 = loadImage("card1.jpg");
card2 = loadImage("card2.jpg");
card3 = loadImage("card3.jpg");
card4 = loadImage("card4.jpg");
card5 = loadImage("card5.jpg");
card6 = loadImage("card6.jpg");
card7 = loadImage("card7.jpg");
card8 = loadImage("card8.jpg");
card9 = loadImage("card9.jpg");
card10 = loadImage("card10.jpg");
startgame = loadImage ("startgame.png");
backgroundImage = loadImage ("game.png");
instructionsImage = loadImage("instructions.png")
endImage = loadImage("end.jpg");
mymusic = loadSound('Baisara-Beera-Kalpana-Patowary-Papon (mp3cut.net).mp3');
flipsound = loadSound ('Card-flip-sound-effect-1.mp3');
clicksound = loadSound('mixkit-game-click-1114.wav');
}
function setup()
{
// Set up canvas and other initialization tasks
createCanvas(600, 700);
gameState = 'start';
mymusic.loop(); //stays on throughout
frameRate(120);
}
class Tile
{
constructor(x, y, face)
{
this.x = x;
this.y = y;
this.size = 100;
this.face = face;
this.isFaceUp = false;
this.isMatch = false;
}
draw ()
{
//draws back if not face up otherwise the image of food
if (this.isFaceUp)
image(this.face, this.x, this.y, this.size, this.size);
else
image(flipSide, this.x, this.y, this.size, this.size);
//black outline of the tiles
stroke(0);
strokeWeight(3);
noFill();
rect(this.x, this.y, this.size, this.size,5);
}
isUnderMouse(x,y)
{
//checks if when clicked the tile is under the mouse
return x >= this.x && x <= this.x + this.size && y >= this.y && y <= this.y + this.size;
}
}
//function to randomly shuffle the array of tiles with 2 copies each
function shuffleArray(array)
{
let counter = array.length;
// while there are elements in the array
while (counter > 0)
{
//selecting a random index
let index = Math.floor(Math.random() * counter);
// decrementing counter
counter--;
//swap last element with it
var temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
}
function draw()
{
if (gameState == 'start')
{
//displaying image for background of game
image(startgame, 0,0,600,700);
//start button
let c = color('#FF69B4');
fill(c);
noStroke();
circle(302.5, 350, 112);
fill('black');
textSize(20);
textStyle(BOLD);
text('Start', 280, 355);
}
else if (gameState == 'instructions')
{
//instruction page is an image I made from canva
image(instructionsImage,0,0,600,700);
}
else if (gameState == 'beingSet')
{
//this is just done once when the start button is clicked
faces = [card1, card2, card3,
card4, card5, card6,
card7, card8, card9,
card10]; //storing all images into array
// making an array of 2 cards and then randomising
possibleFaces = faces.slice(0);
for (let i = 0; i < (NUM_COLS * NUM_ROWS) / 2; i++)
{
// Randomly pick one from the array of remaining faces
var randomInd = floor(random(possibleFaces.length));
var face = possibleFaces[randomInd];
// Push twice onto array
selected.push(face);
selected.push(face);
// Remove from array
possibleFaces.splice(randomInd, 1);
}
shuffleArray(selected); //shuffling the array
// Creating a Tile object for each card
for (var i = 0; i < NUM_COLS; i++)
{
for (var j = 0; j < NUM_ROWS; j++)
{
var tileX = (i * 100) + 55+ (i*30);
var tileY = (j * 100) + 70 +(j*17);
var tileFace = selected.pop();
tiles.push(new Tile(tileX, tileY, tileFace));
}
}
// Changing the game state to 'playing'
gameState = 'playing';
}
else if (gameState == 'playing')
{
// Displaying the background image
image(backgroundImage, 0,0,600,700);
// Checking if enough time has passed since the last card flip
if (delayStartFC && (frameCount - delayStartFC) > 25)
{
// Hiding the flipped cards that are not matches
for (let i = 0; i < tiles.length; i++)
{
if (!tiles[i].isMatch)
tiles[i].isFaceUp = false;
}
// Resetting the flippedTiles array and delayStartFC variable
flippedTiles = [];
delayStartFC = null;
}
// Drawing each tile on the canvas
for (let j = 0; j < tiles.length; j++)
tiles[j].draw();
// Checking if the game is over
if (numMatches == tiles.length/2)
gameState = 'end';
}
else if (gameState == 'end')
{
image(endImage, 0,0,600,700);
let c = color('#d90166');
fill(c);
noStroke();
//play again button
circle(302.5, 350, 112);
fill('black');
textSize(15);
textStyle(BOLD);
text('Play Again', 266, 355);
textSize(20);
//diplaying the tries as a score for game
text("You found them all in " + numTries + " tries!", 146,488);
}
mouseClicked = function()
{
if(gameState == 'start')
{
//checking ditance between click and start button
let distance = dist(mouseX, mouseY, 302.5, 350);
if (distance < 112/2)
{
//play click sound
clicksound.play();
gameState = 'beingSet';
}
// checking distance between click and instructions button
if (mouseX>220 && mouseX<380 && mouseY>500 && mouseY<540)
{
clicksound.play();
gameState = 'instructions';
}
}
else if (gameState == 'playing')
{
//loop through all the tiles
for (let i = 0; i < tiles.length; i++)
{
let tile = tiles[i];
if (tile.isUnderMouse(mouseX, mouseY))
{
// Check if the tile can be flipped (only if less than 2 tiles are flipped and the tile is not already face-up)
if (flippedTiles.length < 2 && !tile.isFaceUp)
{
tile.isFaceUp = true;
flipsound.play();
flippedTiles.push(tile); //adds to flippedtiles array
if (flippedTiles.length === 2)
{
numTries++; //increments the tries counter
if (flippedTiles[0].face === flippedTiles[1].face) //if they match
{
flippedTiles[0].isMatch = true;
flippedTiles[1].isMatch = true;
flippedTiles.length = 0; //empties flipped array because match found
numMatches++;
}
delayStartFC = frameCount;
}
}
}
}
}
else if (gameState == 'instructions')
{
//clicking the startgame button
if (mouseX > 149 && mouseX < 449 &&
mouseY > 600 && mouseY < 615)
{
clicksound.play();
gameState = 'beingSet';
}
}
else if (gameState == 'end')
{
//clicking restart button
let distance = dist(mouseX, mouseY, 302.5, 350);
if (distance < 112/2)
{
clicksound.play();
gameState = 'start';
}
}
};
}