Need help with my project

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:

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;
  }
}
1 Like

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

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

I know this link here 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.

For a counter

//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

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

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

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

Post your code again please ill take a look.

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

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;
  }
}

Instead of

if(counter==0){

}

Use

if(counter<=0){

}

Thank you it worked!!

1 Like