Score and score multiplier for a clicker

Hello,
with this code I would like there to be a counter every time I press a square, but if I put too long or if I press next to it the counter goes back to 0
and even if it is possible a score multiplier, imagine that one square is worth 1 point and if I press correctly on 17 square then the next one will be worth 17 point and so on until I am wrong or are too long sorry for my poor English

int wait = 1000, lastTime = 0, score = 0, size = 50;     //temps, score et taille
PVector pos = new PVector(random(0,600-size),random(0,600-size));

void setup() {
  fullScreen();        //plein ecran
   }

void draw() {
  background(0);           //couleur du fond
  square(pos.x,pos.y,size);       //localisation carré
   
     if(mousePressed && mouseX > pos.x && mouseX < pos.x + size && mouseY > pos.y && mouseY < pos.y + size)   //si clique sur souris alors l'enlever
      {     
         score++;        
         pos = new PVector(random(width-size),random(height-size));      //localisation aleatoire
         lastTime = millis();        //timer
            }
  
     if(lastTime + wait < millis()) {pos = new PVector(random(width-size),random(height-size)); lastTime = millis(); }     //si trop de temps a cliquer alors enlever le carré
   }
1 Like

Cleaned up example code:

int wait = 1000;
int lastTime = 0; 
int score = 0; 
int bonus = 1;
int square_size = 50;
float square_x;
float square_y;

void setup() {
  //fullScreen();
  size(600, 400);
  randomize_square_position();
}

void draw() {
  background(0);
  fill(255);
  if (lastTime + wait < millis()) {
    randomize_square_position();
  }
  rect(square_x, square_y, square_size, square_size);
  text(score, 20, 20);
}

void mousePressed() {
  if ( over_square() ) {     
    score += bonus;
    bonus++;
  } else {
    score = 0;
    bonus = 1;
  }
  randomize_square_position();
}

void randomize_square_position() {
  square_x = random(width - square_size);
  square_y = random(height - square_size);
  lastTime = millis();
}

boolean over_square() {
  return(
    mouseX > square_x &&
    mouseX < square_x + square_size && 
    mouseY > square_y && 
    mouseY < square_y + square_size
    );
}
1 Like

Thank you very much that’s exactly what I wanted I was blocking it for a while but you really help me !

1 Like