I am trying to make a timer that keeps the “GAME OVER” text on the screen after a few seconds before the game resets. Right now I have a timer more or less copied of the processing forum and I can’t figure out how it works. Once I added the timer the ball spawns in the wrong spot and the “GAME OVER” text only works after a few seconds and never on the second run through. I’m not really sure what to do and any help is appreciated. Below is the game code and under that is the timer code:
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=-random(10,12);
}
if (ballx<0){
xspeed=random(10,12);
}
if (bally<0){
yspeed=random(6,8);
}
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);
}
}
void reset() {
ballx=400;
bally=200;
xspeed = 7;
yspeed = 4;
}
Timer(int tempTotalTime) {
totalTime = tempTotalTime;
}
void start() {
savedTime = millis();
}
boolean isFinished() {
int passedTime = millis()- savedTime;
if (passedTime > totalTime) {
return true;
} else {
return false;
}
}
}