# Need help with my project

**URL:** <https://discourse.processing.org/t/need-help-with-my-project/26210>\
**Category:** Coding Questions\
**Created:** [December 13, 2020, 7:50pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210 "2020-12-13T19:50:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cheshire1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cheshire1/32/11421_2.png) [@Cheshire1](https://discourse.processing.org/u/Cheshire1)\
**Post date:** [December 13, 2020, 7:50pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/1 "2020-12-13T19:50:56Z")

</div>

Hello I’m stuck on my project I just need to end my game after the timer is 0 and display a screen that says game over with the score but the timer just keeps going to negative and wouldn’t stop. I would also like if anyone can help me with a replay button that resets the game after clicking it. Can anyone help me please?

My code is here:

```auto
PImage hammer;
PImage meerkat;
PImage smashed;
int x;
int y;
int x1;
int y1;
int x2;
int y2;
int screenWidth = 800, screenHeight = 800;
int lastTime;
int counter;
int counterLastTime;
int delayAmount;
int diameter = 120;
int score;
boolean codeCrushed = false;
boolean code1Crushed = false;
boolean code2Crushed = false;
boolean clickForScore = false;

//Create window
void setup() 
{
  size(1350,850);
  //size(800, 600);
  counter = 30;
  counterLastTime = millis();
  //resetTimer();
  smooth();
  noStroke();
  noCursor();
  score = 0;
  
  hammer = loadImage ("hammer.png");
  hammer.resize(0,120);
  meerkat = loadImage ("meerkat.png");
  meerkat.resize(0,120);
  smashed = loadImage ("smashed.png");
  smashed.resize(0,1);
}//end

void draw() 
{
  background(240, 203, 38);
  stroke(0);
  fill(206, 169, 5);
  //First row of holes
  ellipse(180,300,174,40);
  ellipse(680,300,174,40);
  ellipse(1180,300,174,40);
  //Second row of holes
  ellipse(180,500,174,40);
  ellipse(680,500,174,40);
  ellipse(1180,500,174,40);
  //Third row of holes
  ellipse(180,700,174,40);
  ellipse(680,700,174,40);
  ellipse(1180,700,174,40);
  
  if (codeCrushed == true) {
    image(smashed, x, y);
  } 
  else {
    image(meerkat, x, y);
  }

  if (code1Crushed == true) {
    image(smashed, x1, y1);
  } 
  else {
    image(meerkat, x1, y1);
  }
  if (code2Crushed == true) {
    image(smashed, x2, y2);
  } 
  else { 
    image(meerkat, x2, y2);
  }
  
  //Hammer Time
  pushMatrix();
  if (mousePressed) {
    rotate(-25);
  }
  image(hammer, mouseX-50, mouseY-120);
  popMatrix();
  
  //Score & Time
  fill(0, 0, 0);
  textSize(26);
  text("Score: "+score, 800, 800);
  text("Time: "+counter, 400, 800);
  
  if ((mousePressed == true) && (dist(mouseX, mouseY, x, y) <= diameter)) {
    codeCrushed = true;
  }
  else {
    codeCrushed = false;
  }

  if ((mousePressed == true) && (dist(mouseX, mouseY, x1, y1) <= diameter)) {
    code1Crushed = true;
  }
  else {
    code1Crushed = false;
  }

  if ((mousePressed == true) && (dist(mouseX, mouseY, x2, y2) <= diameter)) {
    code2Crushed = true;
  }
  else {
    code2Crushed = false;
  }
   
  if (millis() - lastTime > delayAmount) {
    
    int randomMole= int (random(1, 10)); 

    if (randomMole == 1) 
    {
      x = 140;
      y = 175;

      x1 = 1140;
      y1 = 375;

      x2 = 1140;
      y2 = 175;
    }

    if (randomMole == 2) 
    {
      x = 640;
      y = 175;

      x1 = 1140;
      y1 = 575;
    }

    else if (randomMole == 3) 
    {
      x = 1140;
      y = 175;

      x1 = 1140;
      y1 = 375;
    }

    else if (randomMole == 4) 
    {
      x = 140;
      y = 375;
    }

    else if (randomMole == 5) 
    {
      x = 640;
      y = 375;
    }

    else if (randomMole == 6) 
    {
      x = 1140;
      y = 375;
    }
    else if (randomMole == 7) 
    {
      x = 140;
      y = 575;

      x1 = 140;
      y1 = 175;

      x2 = 1140;
      y2 = 575;
    }
    else if (randomMole == 8) 
    {
      x = 640;
      y = 575;
    }
    else if (randomMole == 9) 
    {
      x = 1140;
      y = 575;
    } 
    else if (randomMole == 10) 
    {
      x = 140;
      y = 175;

      x1 = 1140;
      y1 = 375;

      x2 = 1140;
      y2 = 175;
    }
    resetTimer();
    
  }//end of loop
  noStroke();
  fill(240, 203, 38);
  rect(0,0,100,150);
  
  if (millis() - counterLastTime > 1000) {
    counter = counter - 1;
    counterLastTime = millis();
  }
  
  if (counter == 0){
    background(0);
    cursor();
    textSize(30);
    text("Game Over", 590, 400);
    text("Score = " + score, 598, 450);
  } 
}//end

void reset() {
  score = 0;
  counter = 30;
}

void resetTimer() {
  lastTime = millis();
  delayAmount = int(random(1, 0) * 1000);
}

void mouseClicked() {
  clickForScore = true;

  if ((clickForScore == true) && (codeCrushed == true)) {
    score = score+ 1;
  }

  if ((clickForScore == true) && (code1Crushed == true)) {
    score = score+ 1;
  }

  if ((clickForScore == true) && (code2Crushed == true)) {
    score = score+ 1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 13, 2020, 8:32pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/2 "2020-12-13T20:32:17Z")

</div>

I would probably start to use classes at this point as it looks like your code could potentially grow further.

You need to wrap the required code in an if statement.

Ie

```auto
if(condition==true || condition) {
   runThisFinction();
}

```

So most likely whatever you put in your draw loop should be in this condition, meaning that whilst condition is not met, or in this case the counter is greater than zero then run draw code.

Please note the code above is meant as an example the two conditions are essentially the same. However you can test a boolean like this

If(someBool)

Or

if(someBool==true)

These mean exactly the same thing

---

<div class="post-metadata">

**Author:** ![Cheshire1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cheshire1/32/11421_2.png) [@Cheshire1](https://discourse.processing.org/u/Cheshire1)\
**Post date:** [December 13, 2020, 8:40pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/3 "2020-12-13T20:40:04Z")

</div>

I know this link here [https://www.openprocessing.org/sketch/112583/](https://www.openprocessing.org/sketch/112583/) had some example on what you’re talking about but I don’t think I’m able to grasp it yet like how exactly would i be able to apply to this my code. Sorry I’m just confused on how to have a timer countdown and stop.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 13, 2020, 8:48pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/4 "2020-12-13T20:48:40Z")

</div>

For a counter

```auto
//define this outside of draw
int counter = 100;

//in draw
if(counter>0){
   counter-=1;
}

```

Note

++ is the same as +=1  
– is the same ad -=1

+= -1 is the same as -= 1

Then

```auto
if(counter>0) dosomething();
else doSomeThingElse();

```

Brackets arent compulsary after conditions providing you only have one instruction.

---

<div class="post-metadata">

**Author:** ![Cheshire1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cheshire1/32/11421_2.png) [@Cheshire1](https://discourse.processing.org/u/Cheshire1)\
**Post date:** [December 13, 2020, 9:02pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/5 "2020-12-13T21:02:25Z")

</div>

I tried that yeah and it displayed the screen without the countdown that I wanted the “game over” and score for just a sec then the timer continued counting backwards. I definitely did something wrong and thats why it won’t display the screen after the timer ends

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 13, 2020, 9:09pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/6 "2020-12-13T21:09:17Z")

</div>

Post your code again please ill take a look.

---

<div class="post-metadata">

**Author:** ![Cheshire1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cheshire1/32/11421_2.png) [@Cheshire1](https://discourse.processing.org/u/Cheshire1)\
**Post date:** [December 13, 2020, 9:12pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/7 "2020-12-13T21:12:24Z")

</div>

Post the code before using the if(counter \> 0) right?

---

<div class="post-metadata">

**Author:** ![Cheshire1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cheshire1/32/11421_2.png) [@Cheshire1](https://discourse.processing.org/u/Cheshire1)\
**Post date:** [December 13, 2020, 9:13pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/8 "2020-12-13T21:13:30Z")

</div>

```auto
PImage hammer;
PImage meerkat;
PImage smashed;
int x;
int y;
int x1;
int y1;
int x2;
int y2;
int screenWidth = 800, screenHeight = 800;
int lastTime;
int counter = 30;
int counterLastTime;
int delayAmount;
int diameter = 120;
int score;
boolean codeCrushed = false;
boolean code1Crushed = false;
boolean code2Crushed = false;
boolean clickForScore = false;

//Create window
void setup() 
{
  size(1350,850);
  //size(800, 600);
  counterLastTime = millis();
  resetTimer();
  smooth();
  noStroke();
  noCursor();
  score = 0;
  
  hammer = loadImage ("hammer.png");
  hammer.resize(0,120);
  meerkat = loadImage ("meerkat.png");
  meerkat.resize(0,120);
  smashed = loadImage ("smashed.png");
  smashed.resize(0,1);
}//end

void draw() 
{
  background(240, 203, 38);
  stroke(0);
  fill(206, 169, 5);
  //First row of holes
  ellipse(180,300,174,40);
  ellipse(680,300,174,40);
  ellipse(1180,300,174,40);
  //Second row of holes
  ellipse(180,500,174,40);
  ellipse(680,500,174,40);
  ellipse(1180,500,174,40);
  //Third row of holes
  ellipse(180,700,174,40);
  ellipse(680,700,174,40);
  ellipse(1180,700,174,40);
  
  if (codeCrushed == true) {
    image(smashed, x, y);
  } 
  else {
    image(meerkat, x, y);
  }

  if (code1Crushed == true) {
    image(smashed, x1, y1);
  } 
  else {
    image(meerkat, x1, y1);
  }
  if (code2Crushed == true) {
    image(smashed, x2, y2);
  } 
  else { 
    image(meerkat, x2, y2);
  }
  
  //Hammer Time
  pushMatrix();
  if (mousePressed) {
    rotate(-25);
  }
  image(hammer, mouseX-50, mouseY-120);
  popMatrix();
  
  //Score & Time
  fill(0, 0, 0);
  textSize(26);
  text("Score: "+score, 800, 800);
  text("Time: "+counter, 400, 800);
  
  if ((mousePressed == true) && (dist(mouseX, mouseY, x, y) <= diameter)) {
    codeCrushed = true;
  }
  else {
    codeCrushed = false;
  }

  if ((mousePressed == true) && (dist(mouseX, mouseY, x1, y1) <= diameter)) {
    code1Crushed = true;
  }
  else {
    code1Crushed = false;
  }

  if ((mousePressed == true) && (dist(mouseX, mouseY, x2, y2) <= diameter)) {
    code2Crushed = true;
  }
  else {
    code2Crushed = false;
  }
   
  if (millis() - lastTime > delayAmount) {
    
    int randomMole= int (random(1, 10)); 

    if (randomMole == 1) 
    {
      x = 140;
      y = 175;

      x1 = 1140;
      y1 = 375;

      x2 = 1140;
      y2 = 175;
    }

    if (randomMole == 2) 
    {
      x = 640;
      y = 175;

      x1 = 1140;
      y1 = 575;
    }

    else if (randomMole == 3) 
    {
      x = 1140;
      y = 175;

      x1 = 1140;
      y1 = 375;
    }

    else if (randomMole == 4) 
    {
      x = 140;
      y = 375;
    }

    else if (randomMole == 5) 
    {
      x = 640;
      y = 375;
    }

    else if (randomMole == 6) 
    {
      x = 1140;
      y = 375;
    }
    else if (randomMole == 7) 
    {
      x = 140;
      y = 575;

      x1 = 140;
      y1 = 175;

      x2 = 1140;
      y2 = 575;
    }
    else if (randomMole == 8) 
    {
      x = 640;
      y = 575;
    }
    else if (randomMole == 9) 
    {
      x = 1140;
      y = 575;
    } 
    else if (randomMole == 10) 
    {
      x = 140;
      y = 175;

      x1 = 1140;
      y1 = 375;

      x2 = 1140;
      y2 = 175;
    }
    resetTimer();
    
  }//end of loop
  noStroke();
  fill(240, 203, 38);
  rect(0,0,100,150);
  
  if (millis() - counterLastTime > 1000) {
    counter = counter - 1;
    counterLastTime = millis();
  }
  
  if (counter == 0){
    background(0);
    cursor();
    textSize(30);
    text("Game Over", 590, 400);
    text("Score = " + score, 598, 450);
  } 
}//end

void reset() {
  score = 0;
  counter = 30;
}

void resetTimer() {
  lastTime = millis();
  delayAmount = int(random(1, 0) * 1000);
}

void mouseClicked() {
  clickForScore = true;

  if ((clickForScore == true) && (codeCrushed == true)) {
    score = score+ 1;
  }

  if ((clickForScore == true) && (code1Crushed == true)) {
    score = score+ 1;
  }

  if ((clickForScore == true) && (code2Crushed == true)) {
    score = score+ 1;
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 13, 2020, 9:17pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/9 "2020-12-13T21:17:16Z")

</div>

Instead of

```auto
if(counter==0){

}

```

Use

```auto
if(counter<=0){

}

```

---

<div class="post-metadata">

**Author:** ![Cheshire1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/cheshire1/32/11421_2.png) [@Cheshire1](https://discourse.processing.org/u/Cheshire1)\
**Post date:** [December 13, 2020, 9:19pm UTC](https://discourse.processing.org/t/need-help-with-my-project/26210/10 "2020-12-13T21:19:11Z")

</div>

Thank you it worked!!
