# Write my text in red when I take too long to press the square or if I click next to it (click game)

**URL:** <https://discourse.processing.org/t/write-my-text-in-red-when-i-take-too-long-to-press-the-square-or-if-i-click-next-to-it-click-game/28036>\
**Category:** Processing\
**Created:** [February 23, 2021, 8:31pm UTC](https://discourse.processing.org/t/write-my-text-in-red-when-i-take-too-long-to-press-the-square-or-if-i-click-next-to-it-click-game/28036 "2021-02-23T20:31:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torikusu](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Torikusu](https://discourse.processing.org/u/Torikusu)\
**Post date:** [February 23, 2021, 8:31pm UTC](https://discourse.processing.org/t/write-my-text-in-red-when-i-take-too-long-to-press-the-square-or-if-i-click-next-to-it-click-game/28036/1 "2021-02-23T20:31:44Z")

</div>

Hello,  
I would like to do as the title indicates put the text ‘’ x0 ‘’ (and not the “score = 0”) in red when I click next to a square.

```auto
int wait = 1000;
int lastTime = 0; 
int score = 0; 
int score2 = 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() {
  size(900,600); //feunetre 900x600
  smooth(); //dessine la geometrie avec des arretes lisses
  img=loadImage("Sans titre-67.jpg"); //quel image je dois charger (au prealable mis dans data)
}

// **********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++;

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

    score2 = 0; //autre score mais avance de 1
  }
  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() { //Mon ecran menu
  image (img,0,0); //joli photo fais pas mes soins ;)
} 

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

void handlejeux() { //mon ecran jeux
  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 = ", 700,50, height-25); //Afficher "Score =" a certaine coo
  textSize(32); //taille du texte
  text(score, 830,50, height-25); //coordonnée du score
  text("X ", 30,570, height-25);
  text(score2, 60,570, 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
    );
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 23, 2021, 8:53pm UTC](https://discourse.processing.org/t/write-my-text-in-red-when-i-take-too-long-to-press-the-square-or-if-i-click-next-to-it-click-game/28036/2 "2021-02-23T20:53:53Z")

</div>

if I understand correctly, this should be it:

```auto
int timer = 0, wait = 1000, score = 0;
boolean lost = false, hover = false;
float x = 300, y = 300, a = 20;
void setup() {
  size(600,600);
  rectMode(CENTER);
}
void draw() {
  background(0);
  if(timer + wait < millis() && score != 0) {
    background(255,0,0);
    lost = true;
  }
  hover = (mouseX > x - a && mouseX < x + a && mouseY > y - a && mouseY < y + a);
  if(hover) fill(0,255,0);
  else fill(255);
  square(x,y,a*2);
  if(mousePressed && hover) {
    x = random(a,width-a);
    y = random(a,height-a);
    score++;
    timer = millis();
  }
}

```

---

<div class="post-metadata">

**Author:** ![Torikusu](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Torikusu](https://discourse.processing.org/u/Torikusu)\
**Post date:** [February 24, 2021, 6:43am UTC](https://discourse.processing.org/t/write-my-text-in-red-when-i-take-too-long-to-press-the-square-or-if-i-click-next-to-it-click-game/28036/3 "2021-02-24T06:43:20Z")

</div>

Thank you for your answer but it’s not exactly that, your command puts the background in red but if possible I would just like the text which is at the bottom left of the game when we play.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 24, 2021, 8:35am UTC](https://discourse.processing.org/t/write-my-text-in-red-when-i-take-too-long-to-press-the-square-or-if-i-click-next-to-it-click-game/28036/4 "2021-02-24T08:35:45Z")

</div>

Just change your code accordingly

It’s not really hard
