Image matching

edited February 2015 in How To...

Hi there. I'm a little new to processing, only been using it for two months now and I got this basic code running and all. But there's one thing I want to do that I'm not sure of how to do.

I want to create a delay of some sort for the images. It's supposed to be like a memory game with cards and right now it's only one click one card but I want to keep one image open and have a second one revealed and then disappear (with redraw). Even if it's an artificial delay, anything will do. Here's my code that does run successfully with one card revealed at a time. The goal: Two cards revealed and then back to hiding. Thank you all so much.

PImage[] images = new PImage[8];
PFont f;

void setup() {
  size(1000, 600);
  noFill();
  smooth();
  f = createFont("Arial", 20, true);
  for (int i=0; i < images.length; i++) {
    images[0] = loadImage("Art.png");
    images[1] = loadImage("Brain.png");
    images[2] = loadImage("Cat.png");
    images[3] = loadImage("Dog.jpg");
    images[4] = loadImage("Movie.png");
    images[5] = loadImage("Music.png");
    images[6] = loadImage("Phone.png");
    images[7] = loadImage("Psych.gif");
    noLoop();
  }
}
void draw() {
  background(0);
  fill(255);
  rect(25, 10, 200, 250); //rectangle at 25
  pushMatrix();
  translate(200, 0);
  rect(50, 10, 200, 250); //rectangle at 250
  translate(200, 0);
  rect(75, 10, 200, 250); // rectangle at 475
  translate(200, 0);
  rect(100, 10, 200, 250); //rectangle at 700
  popMatrix();
  rect(25, 280, 200, 250); //rectangle at 25
  pushMatrix();
  translate(200, 0);
  rect(50, 280, 200, 250);
  translate(200, 0);
  rect(75, 280, 200, 250);
  translate(200, 0);
  rect(100, 280, 200, 250);
  popMatrix();
  if (mousePressed == mouseX < 225 && mouseX > 0 && mouseY > 0 && mouseY < 250) {
    image(images[(int)random(8)], 25, 10);
  }
  if (mousePressed == mouseX < 450 && mouseX > 250 && mouseY > 0 && mouseY < 250) {
    image(images[(int)random(8)], 250, 10);
  }
  if (mousePressed == mouseX < 675 && mouseX > 475 && mouseY > 0 && mouseY < 250) {
    image(images[(int)random(8)], 475, 10);
  }
  if (mousePressed == mouseX < 1000 && mouseX > 700 && mouseY > 0 && mouseY < 250) {
    image(images[(int)random(8)], 700, 10);
  }
  if (mousePressed == mouseX < 225 && mouseX > 0 && mouseY > 280 && mouseY < 500) {
    image(images[(int)random(8)], 25, 280);
  }
  if (mousePressed == mouseX < 450 && mouseX > 250 && mouseY > 280 && mouseY < 500) {
    image(images[(int)random(8)], 250, 280);
  }
  if (mousePressed == mouseX < 675 && mouseX > 475 && mouseY > 280 && mouseY < 500) {
    image(images[(int)random(8)], 475, 280);
  }
  if (mousePressed == mouseX < 1000 && mouseX > 700 && mouseY > 280 && mouseY < 500) {
    image(images[(int)random(8)], 700, 280);
  }
  textFont(f, 32);
  fill(255);
  text("Match The Pictures",325, 575); 
}
void mousePressed() {
  redraw();
}

Comments

  • edited February 2015

    try a variable that holds whether one image is already open or not

    isFirstImage = false;

    when mouse clicked 1st time

    isFirstImage = true;

    when mouse clicked 2nd time

    if (isFirstImage) { 
        // compare, delay etc.  
    }   
    
  • this is a timer

    // a timer
    
    int startTimer; 
    boolean timerRuns = false; 
    
    // ---------------------------------------------------------------
    
    void setup()
    {
      // init (runs only once)
      size(800, 600);
    } // func 
    
    void draw() 
    { 
      // runs on and on 
      background(255);
    
      if (timerRuns) {
        // when a key was hit 
        fill(2, 3, 255); // red
        text ("timer   "  + str(millis() - startTimer), 100, 130);
        // check timer
        if (startTimer + 2000 < millis() ) {
          timerRuns=false;
        }
      }
      else {
        // initial state 
        fill(244, 3, 3); // red 
        text ("Please hit a key", 100, 130);
      }
    } // func 
    //
    
    void keyPressed() {
      startTimer = millis(); 
      timerRuns = true;
    } // func
    
    // ==========================================
    
  • but you need a way to store where which image is, so you can check whether 2 are equal

Sign In or Register to comment.