I attempted to reset the relevant variables back to it’s initial value whenever the space-bar button is pressed after the GAME OVER screen comes up, but something about it just isn’t right. After a second round of the game, the GAME OVER screen only appears for a split second and immediately restarts the game without me even having to press the space-bar. Why is this happening?
flake[] flake = new flake[30];
float speed = 10;
int b;
color b1 = color(0, 0, 0);
color b2 = color(141, 84, 216);
int Y_AXIS = 1;
int s = 0;
int m = 0;
int health = 3;
boolean gameOver = false;
void setup(){
size(1000,800);
for(int i = 0; i < flake.length; i++){
flake[i] = new flake();
}
}
void draw(){
setGradient(0, 0, width, height, b1, b2, Y_AXIS);
b = mouseY;
for(int i = 0; i < flake.length; i++){
flake[i].show();
flake[i].update();
}
display();
timer();
healthScore();
}
void display() {
fill(243, 142, 255);
noStroke();
triangle(917, b, 950, b-15, 950, b+15);
}
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {
float deltaR = red(c2)-red(c1);
float deltaG = green(c2)-green(c1);
float deltaB = blue(c2)-blue(c1);
if (axis == Y_AXIS) {
for (int i=x; i<=(x+w); i++) {
// row
for (int j = y; j<=(y+h); j++) {
color c = color(
(red(c1)+(j-y)*(deltaR/h)),
(green(c1)+(j-y)*(deltaG/h)),
(blue(c1)+(j-y)*(deltaB/h)));
set(i, j, c);
}
}
}
}
void timer(){
textAlign(100,100);
textSize(30);
if(s<=59){
text(m+":"+s,100,100);
s=s+1;
}
else{
m=m+1;
s=0;
text(m+":"+s,100,100);
}
}
void healthScore(){
textSize(32);
text("Health: "+health, 100, 50);
if(health == -1){
gameOver = true;
}
if (gameOver == true){
background(0);
text("GAME OVER.", 100,100);
text("YOU SURVIVED FOR: ", 100,200);
text(m+ " SECONDS", 100,300);
s=0;
textSize(20);
text("PRESS SPACEBAR TO PLAY AGAIN", 100,500);
textSize(30);
if(m<20){
text("YOU CAN DO BETTER!", 100,400);
}
if(20<m && 60>m){
text("NOT TOO BAD!", 100,400);
}
if(m>60){
text("EXCELLENT SCORE!", 100,400);
}
if (key == ' ') {
gameOver = false;
health = 3;
m=0;
s=0;
}
}
}
class flake {
float y=random(height);
float x=random(-width);
float z = random(20, 100);
float size=map(z, 1, 30, 3, 20);
float speed=map(z, 1, 30, 0.1, 5);
boolean active = true;
void show() {
if (active) {
stroke(248, 188, 255);
} else {
stroke(7, 255-188, 0);
}
strokeWeight(size);
point(x, y);
}
void update() {
x+=speed;
if (x>width) reset();
float x1 = 917;
float y1 = b;
float x2 = 950;
float y2 = b-15;
float x3 = 950;
float y3 = b+15;
if (active) {
if (triPoint(x1, y1, x2, y2, x3, y3, x, y)) {
println("hit");
health--;
active=false;
}
}
}
void reset() {
y = random(height);
x = random(-width, 0);
z = random(20, 100);
size=map(z, 1, 29, 3, 30);
speed=map(z, 1, 29, 0.1, 5);
active=true;
}
}
boolean triPoint(float x1, float y1, float x2, float y2, float x3, float y3, float x, float y) {
float areaOrig = abs( (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1) );
float area1 = abs( (x1-x)*(y2-y) - (x2-x)*(y1-y) );
float area2 = abs( (x2-x)*(y3-y) - (x3-x)*(y2-y) );
float area3 = abs( (x3-x)*(y1-y) - (x1-x)*(y3-y) );
if (area1 + area2 + area3 == areaOrig) {
return true;
}
return false;
}