# Help with a timer on a game to restart

**URL:** <https://discourse.processing.org/t/help-with-a-timer-on-a-game-to-restart/43349>\
**Category:** Coding Questions\
**Created:** [November 29, 2023, 10:26pm UTC](https://discourse.processing.org/t/help-with-a-timer-on-a-game-to-restart/43349 "2023-11-29T22:26:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![beginn](https://avatars.discourse-cdn.com/v4/letter/b/f9ae1b/32.png) [@beginn](https://discourse.processing.org/u/beginn)\
**Post date:** [November 29, 2023, 10:26pm UTC](https://discourse.processing.org/t/help-with-a-timer-on-a-game-to-restart/43349/1 "2023-11-29T22:26:18Z")

</div>

Ignore my notes on the code. But my timer just doesnt want to reset each time I play the game. I tried and I’m just not sure what to do.

```auto
float ballX;
float ballY;
float xspeed;
float yspeed;
float speed =10; //INITAL SPEED OF THE BALL 
int hittingtheground =0; //KEEPS TRACK OF HOW MANY TIMES THE BALL HAS HIT THE GROUND
int amtbeforegamerestarts =3; //AMOUNT BEFORE THE GAME RESTARTS 
boolean GameOver = false; //THE GAME IS OVER OR NOT 
boolean gameStart = false; 
int score = 0; 
float TIMER; //deleted player A & B since they were just width and height so its not needed

void setup() {
  size(900, 500);
  ballX = 0;
  ballY = 0;
  xspeed = speed; 
  yspeed = speed; 
}

void draw() {
  background(0);

  if (!GameOver) {
    if (gameStart) { 
      drawBall();
      drawPlayer();
      bouncewillya();
      gamerestart();
      scoreAMT(); 
      timeplaying(); 
    } else {
      startscreen ();
    }
  } else {
    GAMEOVER();
  }
  //CHECKS IF THE GAME IS OVER, THEN IT'LL DISPLAY THE GAMEOVER SCREEN, 
  //IF NOT THEN IT'LL DRAW THE OTHER 4
}

void startscreen () { 
  fill (255); 
  textSize (55); 
  text("BOUNCEY BY my name lol", 200, 150);
  fill (random(255), random(255), random (255)); 
  rect(350,325, 200, 60);
  textSize (45); 
  fill(255);
  text("CLICK TO START GAME", 250, 300);
}

 
void drawBall() {
  fill(255);
  ellipse(ballX,ballY,20,20);
  ballX = ballX + xspeed;
  ballY = ballY + yspeed;

  if (ballX>width) {
    xspeed= -speed; 
  } //THIS ALL DRAWS THE BALL AND CHECKS THE 
                         //SPEED WHEN BOUNCING OFF THE WALLS & GROUND
  if (ballX<0) {
    xspeed= speed;
  }
  if (ballY>height) {
    yspeed = -speed;  
    hittingtheground++;  
  }
  if (ballY<0) {
    yspeed = speed;
  }
}

void drawPlayer() { //DRAWS THE PADDLE OR BAR
  fill(235, 23, 54);
  rect(mouseX, height/2 + 140,100, 20); //MOUSEX SO I CAN MOVE IT BACK AND FORTH HORIZONTALLY 
}

void bouncewillya() { //CHECKS IF THE BALL COLLIDES WITH THE BALL/ THEN GOES THE OPPOSITE DIRECTION WHEN IT DOES
  if ((ballY+10>(height/2+140) && ballY-10<((height/2+140)+20)&& ballX> mouseX&&ballX<mouseX+100)) {
  //(ballY + 10>height/2 + 140 && ballX>mouseX && ballX<mouseX + 100) {//adding if the so the ball wouldnt count the score if it hit the bottom the the bar)
  //thats just the before code// scoring kept going crazy when it hit the bottom of the bar 
    yspeed = -yspeed; 
    score++; 
  }
  }

void gamerestart() { //CHECKS IF THE BALL HITS THE GROUND 3 TIMES AND GETS THE GAMEOVER TO TRUE IF IT HAPPENS
  if (hittingtheground >= amtbeforegamerestarts) {
    GameOver =true;
    hittingtheground = 0;
  }
}

void GAMEOVER() { //THIS IS WHERE THE GAME OVER SCREEN IS WRITTEN WHEN YOU DIE
  fill(255);
  textSize(75);
  text("Game OVERRRRR!!!!", 150, 150);
  fill (random(255), random(255), random (255)); 
  rect(350,325, 200, 60);
  textSize (45); 
  fill(255);
  text("CLICK TO RESTART GAME", 225, 300);
}

void scoreAMT () { 
  fill (255); 
  textSize (20); 
  text ("Score: " +score, 15, 20);
} 

void timeplaying() { 
  if (gameStart) { 
    TIMER = millis()-TIMER; //seconds kept restarting after a while 
    TIMER/=1000; //so milliseconds are now seconds (which I wish I could do seconds normal) 
  } 
  fill (255); 
  textSize (20); 
  text ("Time: " + nf(TIMER, 0,0), 15, 475); //i think its formatted right from research
} 

void mousePressed() { //CHECKS IF THE GAME IS OVER AND WHEN YOU CLICK THAT GAME OVER RECTANGLE. 
//RESETS BALL, SPEED, AMT OF TIMES YOU CAN HIT THE GROUND
  if (GameOver && mouseX > 350 && mouseX < 550 && mouseY > 325 && mouseY < 400) {
    ballX = 0;
    ballY = 0;
    xspeed = speed;
    yspeed = speed;
    hittingtheground = 0;
    GameOver = false;
    gameStart = false;
    score = 0; 
    TIMER=0;
  } else if (!gameStart && mouseX > 350 && mouseX < 550 && mouseY > 325 && mouseY < 400) {
    gameStart = true;
    TIMER=millis();
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 30, 2023, 12:27am UTC](https://discourse.processing.org/t/help-with-a-timer-on-a-game-to-restart/43349/2 "2023-11-30T00:27:02Z")

</div>

Hello @begin,

Simple timer that resets with _mousePressed()_:

```auto
int timeStart, timeElapsed;

void setup()
  {
  size(350, 150);  
  }

void draw() 
  {
  background(0);
  timeElapsed = millis()-timeStart;
  println(timeElapsed/1000.0);
  } 

void mousePressed() 
  { 
  timeStart = millis();
  }

```

That should get you started!

Reference:  
_[millis() / Reference / Processing.org](https://processing.org/reference/millis_.html)_

`:)`
