Matching memory Game

i want two players but i dont know how it works

Hello and welcome to the forum!

Nice to have you here.

You could make an integer variable situation that indicates whose move it is: Player 1 or Player 2.

Each move has two clicks, for the 1st and 2nd memory CARD.

So :

  • situation 0 means Player 1, Card 1
  • situation 1 means Player 1, Card 2 (then check if they match or not!! When they do add 1 to the score of Player 1)
  • situation 2 means Player 2, Card 1
  • situation 3 means Player 2, Card 2 (then check if they match or not!! When they do add 1 to the score of Player 2)

(This is similar to the variable state (for different screens of the game) that you often see here in the forum but different.)

  • The function showSituationText() shows whose move it is (and which Card) depending on the situation.

  • The function mousePressed() registers mouse pressings depending on the situation.

Of course, now you need a grid with the memory game with the images and data which are the pairs and which cards are open or closed and so on.

Regards, Chrisir

Example for two players:



// current situation 
int situation = 0;

// String with data of mouse pressings 
String text=""; 

// -------------------------------------------------

void setup() {
  // init (runs only once)
  size(800, 600);
  background(111);
} // func 

void draw() { 
  // draw() runs on and on 

  background(111); 

  // Show String with data of mouse pressings 
  fill(0); 
  text(text, width-210, 30); 

  // text whose move it is
  showSituationText(); 
  //
} // func 

// ------------------------------------------------

void showSituationText() {
  switch (situation) {

  case 0: 
    // 
    fill(255, 0, 0); 
    text("Player 1 make your 1st move", 67, 67); 
    break; 

  case 1:
    //
    fill(255, 0, 0); 
    text("Player 1 make your 2nd move", 67, 67);
    break;

  case 2: 
    // 
    fill(0, 0, 255); 
    text("Player 2 make your 1st move", 67, 67); 
    break; 

  case 3:
    //
    fill(0, 0, 255);
    text("Player 2 make your 2nd move", 67, 67);
    break;

  default:
    // error
    println("Error number 939; unknown situation : " 
      + situation + ".");
    exit();
    break;
  } // switch
}

// ------------------------------------------------

void mousePressed() {

  switch (situation) {

  case 0: 
    // 
    text+="Player 1: "+ mouseX+","+mouseY+" and "; 
    situation++; 
    break; 

  case 1:
    //
    text+=mouseX+","+mouseY+"\n"; 
    situation++;
    break;

  case 2: 
    // 
    text+="Player 2: "+ mouseX+","+mouseY+" and "; 
    situation++; 
    break; 

  case 3:
    // 
    text+=mouseX+","+mouseY+"\n"; 
    situation = 0; // reset 
    break;

  default:
    // error
    println("Error number 940; unknown situation : " 
      + situation + ".");
    exit();
    break;
  } // switch
  //
}//func 
// ------------------------------------------------
2 Likes