Condition and interruption problems for a newbie

Hello everyone thanks for the add.I’m totally newbie to Processing & I tried to create my first code who works as a magic trick.

I have several conditions in my code in order to help me to guess a number within the several pictures I show to my spectator one by one. If he says he can see the number he’s thinking about, I click the mouse’s Right bouton, otherwise I click the left one and i’m able to guess the number he thought about at the end…

My problem, how can I have my conditions executed one after the other , how Can I pause between each condition and at the end of my code to reveal the number and Get my "voilà "?

Thanks a lot for your help

int nombre=0;
PImage img1;
PImage img2;
PImage img4;
PImage img8;
PImage img16;
PImage img32;

void setup() {
size(640, 360);
img1 = loadImage(“clé1.jpg”);
img2 = loadImage(“clé2.jpg”);
img4 = loadImage(“clé4.jpg”);
img8 = loadImage(“clé8.jpg”);
img16 = loadImage(“clé16.jpg”);
img32 = loadImage(“clé32.jpg”);;// Load the image into the program
}

void draw() {
// Displays the image at its actual size at point (0,0)
image(img32, 0,0);print(nombre);

if (mouseButton == RIGHT) {loop ();nombre = nombre +32;}
else if (mouseButton == LEFT) {loop ();nombre = nombre +0;}

if (mouseButton == RIGHT) {clear();image(img16, 0,0);nombre= nombre+8;}
else if (mouseButton == LEFT) {clear();image(img16, 0,0);}

if (mouseButton == RIGHT) {clear();image(img4, 0,0);nombre= nombre+16;}
else if (mouseButton == LEFT) {clear();image(img4, 0,0);}

if (mouseButton == RIGHT) {clear();image(img2, 0,0);nombre= nombre+4;}
else if (mouseButton == LEFT) {clear();image(img2, 0,0);}

if (mouseButton == RIGHT) {clear();image(img1, 0,0);nombre= nombre+2;}
else if (mouseButton == LEFT) {clear();image(img1, 0,0);}

1 Like

What you have done is perfectly logical, and you’re asking the right question.

You need a state variable, to keep track of the current state. This can just be a single number. The first state will be state “0”, which is when you show the image32. The next state will be state “1”, which shows the image16. And so one.

But if we’re clever, we can use a few more tricks and a couple of arrays to simplify the code even more by using a state variable. Like so:

int guess;
PImage[] imgs = new PImage[6];
int[] nums = { 32, 16, 8, 4, 2, 1};
int state;

void setup() {
  size(640, 360);
  for( int i=0; i < imgs.length; i++){
    imgs[i] = loadImage("clé" + nums[i] + ".jpg");
  }
  reset();
}

void reset(){
  guess = 0;
  state = 0;
}

void draw() {
  image(imgs[state], 0, 0);
}

void mousePressed(){
  if( mouseButton == RIGHT ){
    guess += nums[state]
    state++;
  } else if ( mouseButton == LEFT ){
    state++;
  }
  if( state == 6  ){
    println( guess );
    reset();
  }
}

Very Nice!

more simple would be to have a state variable (indicates which screen where you are on) and a function mousePressed() but otherwise your code can stay almost the same…

I don’t know if this helps.

void draw() {
    
if(state==0) {
    // Displays the image at its actual size at point (0,0)
    image(img32, 0,0);print(nombre);
}

else if(state==1) {    
    if (mouseButton == RIGHT) {loop ();nombre = nombre +32;}
    else if (mouseButton == LEFT) {loop ();nombre = nombre +0;}
}

else if (state==2) {
    if (mouseButton == RIGHT) {clear();image(img16, 0,0);nombre= nombre+8;}
    else if (mouseButton == LEFT) {clear();image(img16, 0,0);}
}

etc.

instead of if..else if.... you can use switch(state) {......}

Full Sketch


//
//Global Variables  <------------------------------------------------------------->

int nombre=0;
PImage img1;
PImage img2;
PImage img4;
PImage img8;
PImage img16;
PImage img32;

int state = 0; // 

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

void setup() {

  size(640, 360);

  // Load the images into the program
  img1 = loadImage("img00 (0).JPG");
  img2 = loadImage("img00 (1).JPG");
  img4 = loadImage("img00 (2).JPG");
  img8 = loadImage("img00 (3).JPG");
  img16 = loadImage("img00 (4).JPG");
  img32 = loadImage("img00 (5).JPG"); // Load the images into the program

  background(#A59696);
  frameRate ( 100 );
}

// DRAW < ============================================================================ >

void draw() {
  background(#A59696);

  fill(255); 
  rect(width-43, 0, 50, 50);
  fill(0); 
  text(state, 
    width-23, 23); 

  switch (state) {
  case 0:
    // Displays the image at its actual size at point (0,0)
    image(img32, 0, 0);
    break; 

  case 1:
    image(img16, 0, 0);
    break;

  case 2:
    image(img8, 0, 0);
    break;

  case 3:
    image(img4, 0, 0);
    break;

  case 4:
    image(img2, 0, 0);
    break;

  case 5:
    image(img1, 0, 0);
    break;
    //
  default:
    println("unknown state in draw. It was "
      + state);
    exit(); 
    break;
    //
  }//switch
  //
}//func 

// Mouse Pressed  < ================================================ >

void mousePressed() {
  //
  switch (state) {
  case 0:
    if ( mouseButton == RIGHT ) {
      state++;
    } else if ( mouseButton == LEFT ) {
      state++;
    }
    break; 

  case 1:
    if ( mouseButton == RIGHT ) {
      state++;
    } else if ( mouseButton == LEFT ) {
      state++;
    }
    break;

  case 2:
    if ( mouseButton == RIGHT ) {
      state++;
    } else if ( mouseButton == LEFT ) {
      state++;
    }
    break;

  case 3:
    if ( mouseButton == RIGHT ) {
      state++;
    } else if ( mouseButton == LEFT ) {
      state++;
    }
    break;

  case 4:
    if ( mouseButton == RIGHT ) {
      state++;
    } else if ( mouseButton == LEFT ) {
      state++;
    }
    break;

  case 5:
    if ( mouseButton == RIGHT ) {
      state++;
    } else if ( mouseButton == LEFT ) {
      state++;
    }
    break;
    //
  default:
    println("unknown state in mousePressed. It was "
      + state);
    exit(); 
    break;
    //
  }//switch
}//func 
//

Wow !!! Thank you so much, I have a lot of new information and clever topics to explore now.First I need to translate everything if French some keywords 're still missing . But I’m starting to understand the logic and tricks you share with me.Thanks again

1 Like

Thank you very much, So much new ideas to figure out. In the next few days, I’ll try to understand each and every line you created.

that’s really great !! thanks again.

1 Like