# Game Timer Question

**URL:** <https://discourse.processing.org/t/game-timer-question/38602>\
**Category:** Coding Questions\
**Created:** [September 1, 2022, 4:12pm UTC](https://discourse.processing.org/t/game-timer-question/38602 "2022-09-01T16:12:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![newprogrammer12](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@newprogrammer12](https://discourse.processing.org/u/newprogrammer12)\
**Post date:** [September 1, 2022, 4:12pm UTC](https://discourse.processing.org/t/game-timer-question/38602/1 "2022-09-01T16:12:52Z")

</div>

I have created a game similar to pong and at the end I have made a “Game Over” text appear for 5 seconds before the game resets. The problem is, when the game resets, the timer seems to not work anymore. Any ideas on how I can fix this. Below is the code: (the class for timer is separate if it is necessary I can attach it as well)

```auto
float ballx;
float bally;

float ballr = 20;

float xspeed = 7;
float yspeed = 4;

float recty = 750;
float rectw = 150;
float recth = 40;

Timer timer;

void setup() {
  size(800,800);
  timer = new Timer(5000);
  timer.start();
  ballx = width*0.5;
  bally = height*0.8;
 
 
}
   

void draw() {
  background(0);
  //ball
  fill(28,200,255);
  ellipse(ballx,bally,40,40);
  ballx=ballx+xspeed;
  bally=bally+yspeed;
  
  fill(255);
  rectMode(CENTER);
  rect(mouseX, recty, rectw, recth, 30);
  
  //boundaries

 if (ballx>width){
    xspeed=-7;
  }
  if (ballx<0){
    xspeed=7;
  }
  
  if (bally<0){
    yspeed=4;
  }
  
  
  if (bally-ballr/2>height) {
    textSize(130);
    text("GAME OVER", 70, 200);
    
    if (timer.isFinished()) {
    reset();
    }
  } else if (ballx>mouseX-rectw/2 && 
    ballx<mouseX+rectw/2 &&
    bally+ballr/2>recty-23 ) {
    yspeed = -random(8,10);
    xspeed = random(-12,14);
  }
}//func 

void reset() {

  ballx=400;
  bally=200;

  xspeed = 7;
  yspeed = 4;
}

```

---

<div class="post-metadata">

**Author:** ![newprogrammer12](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@newprogrammer12](https://discourse.processing.org/u/newprogrammer12)\
**Post date:** [September 1, 2022, 4:22pm UTC](https://discourse.processing.org/t/game-timer-question/38602/2 "2022-09-01T16:22:27Z")

</div>

New problem just surfaced. When the code first starts the ball spawns super low but after the first game over it spawns right and the game over timer doesn’t work. ☹

---

<div class="post-metadata">

**Author:** ![newprogrammer12](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@newprogrammer12](https://discourse.processing.org/u/newprogrammer12)\
**Post date:** [September 1, 2022, 4:24pm UTC](https://discourse.processing.org/t/game-timer-question/38602/3 "2022-09-01T16:24:05Z")

</div>

If you play last long enough the timer doesn’t work after the first round either

---

<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:** [September 2, 2022, 12:52am UTC](https://discourse.processing.org/t/game-timer-question/38602/4 "2022-09-02T00:52:07Z")

</div>

Please avoid duplicate posts

---

<div class="post-metadata">

**Author:** ![newprogrammer12](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@newprogrammer12](https://discourse.processing.org/u/newprogrammer12)\
**Post date:** [September 2, 2022, 1:18pm UTC](https://discourse.processing.org/t/game-timer-question/38602/5 "2022-09-02T13:18:53Z")

</div>

thats my bad i wont do it again. i didnt fell like i had stated my question out well or laid the code out clearly in the first one

---

<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:** [September 2, 2022, 1:20pm UTC](https://discourse.processing.org/t/game-timer-question/38602/6 "2022-09-02T13:20:25Z")

</div>

Don’t worry. Does it work now with the help in the other discussion?

---

<div class="post-metadata">

**Author:** ![newprogrammer12](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@newprogrammer12](https://discourse.processing.org/u/newprogrammer12)\
**Post date:** [September 2, 2022, 1:31pm UTC](https://discourse.processing.org/t/game-timer-question/38602/7 "2022-09-02T13:31:10Z")

</div>

I’m still working on the code but the response was super helpful and the concept is much clearer now.

---

<div class="post-metadata">

**Author:** ![newprogrammer12](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@newprogrammer12](https://discourse.processing.org/u/newprogrammer12)\
**Post date:** [September 2, 2022, 1:31pm UTC](https://discourse.processing.org/t/game-timer-question/38602/8 "2022-09-02T13:31:36Z")

</div>

So I’m 99% sure it will work once I finish it
