How could i reset and restart game with a key or button after gameover is displayed?
float x = 0;
float y = 0;
float dim = 50.0;
float xim = 90.0;
float start = PI;
float stop = TWO_PI;
float angle = 0;
float s;
float t;
float radius1=60;
float radius2=100;
int npoints=50;
PFont font;
float suntimer = 0.2;
void setup() {
size(800, 800);
font = createFont(“Arial”, 20);
noStroke();
smooth();
drawBalloons(50); //recursion - calling 50 balloons in random places and looping them.
}
void draw() {
background(#03F9FF);
pushMatrix();
for(int k = 0; k < 2; k++){ //recursion - calling cloud function twice with help of for loop
translate(x, y);
drawClouds();
}
popMatrix();
pushMatrix();
fill(#FFCC33);
translate(width0.85, height0.13);
rotate(frameCount / 400.0);
star(s, t, radius1, radius2, npoints);
popMatrix();
if (radius1 > 850) { //control sun rays to display gamestart instructions in first 5-10 seconds
radius1 = 60;
} else {
radius1=radius1+suntimer;
{
if (radius1 > 60 && radius1 < 150) {
gameStart();
drawBird();
} else if (radius1 > 150 && radius1 < 800) { //kill the bird and only display bird eye when sun rays go out of screen size.
Balloons();
drawBird();
} else if (radius1 > 800 && radius1 < 850) {
background(#F53B51); //makes screen red to say game over
gameOver(); //displays gameOver text once the sunrays reach the screen edge
} else {
}
}
}
if (random(10) < 9) { //blinking eye of bird with help of random function and if condition
fill(0);
birdeye();
birdpupil();
} else {
fill(#08D121);
}
}
void mousePressed() {
balloonClicked(mouseX, mouseY); //balloon click to burst
}
ArrayList balloons = new ArrayList(); // An arraylist to hold all the balloons
void drawBalloons(int howmany) { //for loop to increment and add balloons on the screen
for (int i = 0; i < howmany; i++) {
balloons.add(new Balloon());
}
}
void Balloons() {
for (Balloon b : balloons) { //to update and draw balloons and randomize location
b.update();
b.drawBalloonbasket();
}
}
void balloonClicked(int _mx, int _my) { // Check all bubbles to see if they have been clicked on
for (Balloon b : balloons) {
b.balloonClicked(_mx, _my);
}
}Preformatted text
class Balloon {
float x = 50;
float y = 60;
float diam;
float diamBurst;
float fallingSpeed;
float R = random(255);
float G = random (255);
float B = random (255);
int state = 0;
Balloon() {
reset();
}
void reset() {
diam = random(40, 70);
x = random(diam, width - diam);
y = height + (diam/2);
fallingSpeed = random(0.5, 2);
state = 0;
}
void update() {
switch(state) {
case 0: // normal rising bubble
y = y - fallingSpeed;
if (y <= -diam/2) {
reset();
}
break;
case 1: // check if bubble has been clicked on
diam = lerp(diam, diamBurst, 0.1); // burst the balloon!
break;
}
}
void drawBalloonbasket() {
switch(state) {
case 0:
fill(R, G, B);
noStroke();
float xDiam = diam;
float yDiam = diam;
ellipse(x, y, xDiam, yDiam);
fill(#AD5803);
rectMode(CENTER);
noStroke();
rect(x, y+50, xDiam - 10, yDiam - 30);
stroke(0);
line(x+15, y+20, x+10, y+40);
line(x - 15, y + 20, x - 10, y + 40);
break;
}
}
void balloonClicked(int _mx, int _my) {
// check distance from centre of bubble
if (dist(x, y, _mx, _my) < diam/2) {
state = 1;
}
}
}
void drawBird() {
noStroke();
fill(#FAFF05);
ellipse(mouseX-85, mouseY+5, 70, 70); //bird face
fill(#FAFF05);
arc(mouseX-215, mouseY-15, 200, 200, angle+PI/350, angle+PI); //bird body
fill(#FF051A);
triangle(mouseX, mouseY, mouseX-50, mouseY, mouseX-50, mouseY+15); //beak
triangle(mouseX-175, mouseY+15, mouseX-235, mouseY+15, mouseX-235, mouseY-65); //leftwing
triangle(mouseX-215, mouseY-15, mouseX-165, mouseY-15,mouseX-165, mouseY-65); //rightwing
triangle(mouseX-355, mouseY-15, mouseX-315,mouseY-15, mouseX-315, mouseY-65); //tail
}
void birdeye() {
fill(255);
ellipse(mouseX-80, mouseY-5, 30, 30); //bird eye
}
void birdpupil() {
fill(0);
ellipse(mouseX-75, mouseY-5, 20, 20); //bird pupil
}
void drawClouds() {
x = x + 0.2;
if (x > width + dim) { //control cloud moving speed
x = -dim;
}
if (key == CODED) { //change cloud color with arrow keys
if (keyCode == UP) {
fill(#B2B1B0);
} else if (keyCode == DOWN) {
fill(#FABBD5);
} else {
fill(255);
}
}
noStroke();
ellipse(160, 110, 100, 100);
ellipse(10, 100, 80, 80);
ellipse(70, 130, 100, 100);
ellipse(70, 80, 150, 150);
ellipse(220, 100, 80, 80);
}
void star(float s, float t, float radius1, float radius2, int npoints) {
float angle = TWO_PI / npoints;
float halfAngle = angle/2.0;
beginShape();
noStroke();
for (float a = 0; a < TWO_PI; a += angle) {
float sx = s + cos(a) * radius2;
float sy = t + sin(a) * radius2;
vertex(sx, sy);
sx = s + cos(a+halfAngle) * radius1;
sy = t + sin(a+halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}
void gameOver() { //game over text
textFont(font);
fill(0);
textAlign(CENTER);
text(“GAME OVER”, height/2, width/2);
}Preformatted textPreformatted text