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.
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();
}
}