For this project, I decided to make a memory card game where the user would be given a set of cards and would have to match two cards in order to get rid of them, until the game ends of course.
I originally got this idea from Meri Engel, so I will leave a link to her youtube channel below.
https://www.youtube.com/channel/UCjBGQA6hPD3SY4G5feqMDFw
So, now on to the procedure.
To do this I first created a class called Cards that would have values of its x-coords, y- coords, FaceValue, etc. Moreover with the certain attributes, the class would also have functions such as faceDown() or displayFront() which would flip the card back and forth.
In the main part of my code, interactions with the cards began. I would have an array that would store two flipped cards, and then check if they were the same. Before that, I had a shuffle function that would randomly shuffle the cards in different order, since without shuffling they would be placed in same order every time. Finally, the mouseClicked() function would serve as where I would click in terms of coordinates and if so what would happen, which of course would be to flip the cards. But even here, I didn’t want to flip more than two cards so I have a switch that flips back the cards.
Below is my code, and below that is the youtube video of me explaining how it works.
Cards[] myCard = new Cards[6]; int[] x = new int[6]; //x coordinate array int[] y = new int[6]; //y coordinate array int[] fv = new int[6]; //facevalue array int[] checkFv = new int[2]; //check face value int[] cardUp = new int[2]; //check card up boolean[] clicked = new boolean[8]; PImage flip; PFont myFont; int flipped = 0; int win = 0; void setup(){ int myX = 15; // x coord int myY = 15; // y coord int myFv = 0; int count = 1; size(1000,1000); myFont =createFont("Verdana",40,true); flip = loadImage("flip.png"); // flip button for(int i =0; i<6;i++){ //spacing out the cards clicked[i] = false; //can't click card again y[i] = myY; x[i] = myX; fv[i] = count; count += 1; if(count == 4){ // number of replicates count = 1; } if(myX <345){ myX += 250; } else if (myX >345){ myX = 15; myY += 400; } } shuffle(); for(int i=0;i<6;i++){ myCard[i] = new Cards(x[i],y[i],fv[i]); //each card is assigned coordinate value } } void draw(){ textFont(myFont); fill(0); background(255); for(int i=0;i<6;i++){ myCard[i].display(); } image(flip,500,750); if(mousePressed){ if(mouseX>500 && mouseX<600 && mouseY > 770 && mouseY<880){ for(int i = 0; i<6;i++){ myCard[i].faceDown(); clicked[i]=false; flipped=0; } } } if(win == 3){ text("You Win!",500,500); } } /* void flipme(){//flipperino for(int i=0;i<6;i++){ if(mouseX>x[i] && mouseX<(x[i]+305) && mouseY > y[i] && mouseY<(y[i]+350)){ myCard[i].displayFront(); } } } */ void mouseClicked(){ for(int i=0;i<6;i++){ if(mouseX>x[i] && mouseX<(x[i]+250) && mouseY > y[i] && mouseY<(y[i]+300) && (clicked[i]==false) ){ myCard[i].displayFront(); clicked[i] =true; cardUp[flipped] = i; flipped++; println(flipped); if(flipped == 2){ flipped = 0; if(fv[cardUp[0]] == fv[cardUp[1]]){ myCard[cardUp[0]].matched(); myCard[cardUp[1]].matched(); win += 1; } /*else if(fv[cardUp[0]] != fv[cardUp[1]]){ myCard[cardUp[0]].display(); myCard[cardUp[1]].display(); myCard[cardUp[0]].faceDown(); myCard[cardUp[1]].faceDown(); clicked[i] = false; clicked[i+1] = false; } */ } } } } void shuffle(){//shufflerino int temp = 0; int rand = 0; for(int i=0; i<6;i++){ rand = int(random(0,6)); temp = fv[i]; fv[i] = fv[rand]; fv[rand] = temp; } }
class Cards{ PImage cardImage; int show = 0; //int cWidth = 105; //int cHeight = 130; int cX = 0; int cY = 0; int faceValue = 0; String[] cardName ={"0.png","creep.png","isabelle.png","kk.png","psycho.png","digby.png","tomnook.png"}; Cards(int x, int y, int fv){ cX = x; cY = y; faceValue = fv; } void display(){ cardImage = loadImage(cardName[show]); image(cardImage,cX,cY); } void displayFront(){ show = faceValue; } void faceDown(){ show = 0; } void matched(){ cX = -300; } }