# Help with making a counter

**URL:** <https://discourse.processing.org/t/help-with-making-a-counter/40859>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [February 7, 2023, 12:38pm UTC](https://discourse.processing.org/t/help-with-making-a-counter/40859 "2023-02-07T12:38:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andprocessing](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@andprocessing](https://discourse.processing.org/u/andprocessing)\
**Post date:** [February 7, 2023, 12:38pm UTC](https://discourse.processing.org/t/help-with-making-a-counter/40859/1 "2023-02-07T12:38:46Z")

</div>

Hi! I’m making a game where you have to find wolves in a pack of sheep. There are 5 wolves made with an array, and as the code is now, you advance to the next page when you click on one wolf. However, I want it to advance when you’ve clicked on all the wolves on screen.  
I tried doing it with a counter, but it’s not really working, and i can’t really think of any other way to do it… I would really appreciate some help 🙂

Here’s the code (with my try at making the counter)

```auto
PImage lobo;
PImage oveja;
PImage fondo;
PImage caja;
PImage gameover;

int GameOver=1; //cuándo termina la partida
int Restart=1; //tiene que empezar en 1 para que se inicie el juego
int Puntos=0;

//variables lobo
int nlobo=5;

int loboX[]=new int[nlobo];
int loboY[]=new int[nlobo];

//variables ovejas
//aumenta el valor de n para subir la dificultad
int n=30;
int ovejasX[]=new int[n];
int ovejasY[]=new int[n];

int ms=10; //milisegundos

//cambiar tiempo aquí
int s=90; //segundos

void setup(){
  size(800,800);

  //gráficos
  lobo = loadImage("lobo.png");
  oveja = loadImage("oveja.png");
  fondo = loadImage("fondo.png");  
  caja = loadImage("fondo2.png");
  gameover = loadImage("gameover.png");

  
  for(int contador=0;contador<n;contador++){
    ovejasX[contador]=int(random(0,677));
    ovejasY[contador]=int(random(100,654)); //el random es así para que no se salgan de la pantalla
}

for(int contadorL=0;contadorL<nlobo;contadorL++){
   loboX[contadorL]=int(random(0,677));
   loboY[contadorL]=int(random(100,677));
}

}
    

  
void draw(){
  
  //tiempo //lo pongo aquí porque se solapaban los números
  
  image(caja, 0, 0);
  textAlign(CENTER);
  textSize(30);
  text(s,630,50);

   if(ms<=100 && ms>0){
    ms=ms-1;
   }
   
   if(ms==0){

     s=s-1;
     ms=100;
     
     if(s<=0){
    
      noLoop();
      fill(255,0,0);
      GameOver=0;
    
      //desaparecen las ovejas y se revela la posición del lobo
      background(255,255,255);
    
      textAlign(CENTER);
      textSize(100);
      image(gameover, 0, 0);
      textSize(30);
      text("¡Se acabó el tiempo!",400,500);
      text("Lobos cazados: " + (Puntos),400,550);
      }
   }  

  
  
  //juego
  
  if((GameOver==1) && (Restart==1)){
    DibujarFormas();
  }
}
  
  
void DibujarFormas(){
  
  image(fondo, 0, 0);
  
  //caja de texto
  fill(0,0,0);
  rect(0,0,800,70);
  
 //objeto principal (lobo). Lo pongo delante para que los las ovejas lo pisen. Porque se está escondiendo
  
  for(int contadorL=0;contadorL<nlobo;contadorL++){
   image(lobo, loboX[contadorL], loboY[contadorL]);
  
 //ovejas
 fill(255,255,255);
  for(int contador=0;contador<n;contador++){
   image(oveja, ovejasX[contador], ovejasY[contador]);

  }
  
 //tiempo
  fill(255,255,255);
  textAlign(CENTER);
  textSize(30);
  text("Tiempo: ",560,50);
  
  
 //puntos
  fill(255,255,255);
  textAlign(CENTER);
  textSize(30);
  text("Puntos: " + (Puntos),230,50);
   
  
 //nueva pantalla
  Restart=0;
    
}
}

//para que salga la mano sobre el lobo
void mouseMoved(){
  
cursor(ARROW); // !!!!!!!!!!!

  for (int contadorL=0; contadorL<nlobo; contadorL++) {
    
    if ((mouseX > loboX[contadorL]) &&
      (mouseX < loboX[contadorL]+123) &&
      (mouseY > loboY[contadorL]) &&
      (mouseY < loboY[contadorL]+160))
    {
      cursor(HAND);
    }
  }
}

  
 void mouseClicked() {
   
   int loboscazados=0;

  if (GameOver==1) //si la partida está iniciada
  {
    if (Restart==0) //y no está reiniciado
    {
      if(loboscazados==5)
      {
        Restart=1; //se reinicia cuando le das click a todos los lobos
      }
    }

    int counter = 0;

    for (int contadorL=0; contadorL<nlobo; contadorL++) {
      
      
      if ((mouseX > loboX[contadorL]) &&
        (mouseX < loboX[contadorL]+123) &&
        (mouseY > loboY[contadorL]) &&
        (mouseY < loboY[contadorL]+160))
        //si haces click en el lobo ganas puntos
      {
        
        //lobos
         loboX[contadorL]=int(random(0,677));
         loboY[contadorL]=int(random(100,677));
        
        for(int contador2=0;contador2<nlobo;contador2++){
         loboX[contador2]=int(random(0,677));
         loboY[contador2]=int(random(100,677));
         }
         
         //ovejas       
         for(int contador=0;contador<n;contador++){
           
           ovejasX[contador]=int(random(0,677));
           ovejasY[contador]=int(random(100,654));
         }
           
        
        Puntos++;
        counter++;
        loboscazados++;
        
      }
    }

    if (counter==0)
    {
      //si haces click en cualquier otro sitio, y la partida está iniciada, game over

      fill(255, 0, 0);
      GameOver=0;

      //desaparecen las ovejas y se revela la posición del lobo
      background(255, 255, 255);

      for (int contadorLI=0; contadorLI<nlobo; contadorLI++) {

        image(lobo, loboX[contadorLI], loboY[contadorLI]);
      }

      textAlign(CENTER);
      textSize(30);
      image(gameover, 0, 0);
      text("¡El lobo se ha escapado!", 400, 500);
      text("Lobos cazados: " + (Puntos), 400, 550);
      

    }
  }

}

```

---

<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 7, 2023, 6:12pm UTC](https://discourse.processing.org/t/help-with-making-a-counter/40859/2 "2023-02-07T18:12:42Z")

</div>

> [@andprocessing](#):
>
> `int loboscazados=0;`

I this your counter?

Then this line belongs before setup()

---

<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 7, 2023, 6:24pm UTC](https://discourse.processing.org/t/help-with-making-a-counter/40859/3 "2023-02-07T18:24:50Z")

</div>

> [@andprocessing](#):
>
> `int counter = 0;`

Same goes for counter: move the line before setup ()

Do not copy the line but move the line

---

<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 7, 2023, 6:57pm UTC](https://discourse.processing.org/t/help-with-making-a-counter/40859/4 "2023-02-07T18:57:43Z")

</div>

> [@andprocessing](#):
>
> ```auto
> if (counter==0)
> {
> 
> ```

Since it’s a counter I suggest to say 5 here !!!

When you start with 0 and use ++ you end with 5, not with 0
