Positive counter (Click game)

Hello,
I would like to create a counter which, each time I press correctly on an edge increases by 1, but that if I press next to it or if I take too long then the counter returns to 0. example if I press correctly on 57 square then it is displayed 57 and if I miss then 0.
currently my code looks like this :

int wait = 1000;
int lastTime = 0; 
int score = 0; 
int bonus = 1;
int square_size = 50;
float square_x;
float square_y;
final int menu      = 0;
final int jeux      = 1;
int state = menu;
PFont font;
PImage img;

void setup() {
  fullScreen();       //fullscreen
  smooth();
  img=loadImage("Sans titre-1.jpg");
  font = createFont("ARCARTER-78.vlw", 14);    //la police
  textFont(font);
}

//**********Changement de fenetre*********\\

void draw() {
  switch (state) {
  case menu:
    showMenu();       //developpement plus bas
    break;             //passer a l'instruction suivante 
  case jeux:
    handlejeux();    //developpement plus bas
    break;            //passer a l'instruction suivante 
  default:
    println ("Unknown state (in draw) "
      + state
      + " ++++++++++++++++++++++");
    exit();      //quitter le programme
    break;            //passer a l'instruction suivante 
  } 
}

//**********Multiplicateur de score (score de base + bonus)*********\\

void mousePressed() {
  if (state!=jeux)
    return; 

  if ( over_square() ) {     
    score += bonus;         //plus j'appuye plus je gagne du score
    bonus++;
  } else {
    score = 0;              //score de base
    bonus = 1;              //score +1 par combo
  }
  randomize_square_position();
}

void keyPressed() {
  switch (state) {
  case menu:
    keyPressedForMenu();   //developpement plus bas
    break;            //passer a l'instruction suivante 
  case jeux:
    keyPressedForjeux();   //developpement plus bas
    break;           //passer a l'instruction suivante 
  default:
    println ("Unknown state (in keypressed) "
      + state
      + " ++++++++++++++++++++++");
    exit();        //quitter le programme
    break;          //passer a l'instruction suivante 
  } 
} 

//**********Action si touche pressé (changement de background)*********\\

void keyPressedForMenu() {

  switch(key) {
  case '1':             //si j'appuye sur 1....
    state = jeux;        //changer ecran sur jeux
    break;         //passer a l'instruction suivante 
    
  case 'x':         //si j'appuye sur x....
  case 'X':         //ou X
    exit();         //quitter
    break;        //passer a l'instruction suivante 
  }
} 

void keyPressedForjeux() {
  switch(key) {
  default:
    key=0; 
    state = menu;
    break;       //passer a l'instruction suivante 
  }

} 

//**********Apparence visuel du menu (emplacement des textes...etc) **********\\

void showMenu() {
  image (img,0,0);
} 

//**********Mon jeux**********\\

void handlejeux() {
  background(0);          //couleur du fond noir 
  fill(255);                       //couleur blanche
  if (lastTime + wait < millis()) {             //si j'ai pas click au bout de X temps
    randomize_square_position();                //alors nouveau carré
  }
  rect(square_x, square_y, square_size, square_size);    //dessin du carré
  text("Score = ", 20, height-25);       //Afficher "Score =" a certaine coo
  textSize(32);           //taille du texte
  text(score, 150, height-25);     //coordonnée du score
}

void randomize_square_position() {                 //commande du random spawn
  square_x = random(width - square_size);          //Spawn random de carré sur les coo X
  square_y = random(height - square_size);         //Spawn random de carré sur les coo Y
  lastTime = millis();                             //depop si trop long
}

boolean over_square() {
  return(
    mouseX > square_x &&                    //si je click sur le carré, alors disparition  
    mouseX < square_x + square_size &&      
    mouseY > square_y && 
    mouseY < square_y + square_size
    );
}

this code from mousePressed() does this except that you say bonus++; so that first time it increases by 1, then by 2, then by 3

I don’t understand your question. Because what you ask you already have!

Does it work?

What happens and what do you want to happen instead?

Yes you are right I already have a counter but with a bonus, I would like a second one except that I cannot put two counter at the same time

in this section:

well you can use score2 additionally

  if ( over_square() ) {     
    score += bonus;         //plus j'appuye plus je gagne du score
    bonus++;

    score2 += 1;         
  } else {
    score = 0;              //score de base
    bonus = 1;              //score +1 par combo

    score2 = 0; 
  }
1 Like