Reset the game of bursting ballons

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

Hi,

Can you please format your code by using the </> button in the message editor or use backticks around your code like this : ``` code ``` → code.

You can edit your post to do that.

Pro tip : you can also press Ctrl+T in the Processing editor to auto format your code! :wink:

Could you also give more details :

  • Do you have a way to reset the game?
  • Do you know how to detect if a key was pressed?
  • How do you know if it’s a gameover?
1 Like

Hi, I pressed the </> it did not do anything.

I dont have a way to reset game and i dont know how to do that.

I can do keypressed command, i know how to do that to detect keypressed.

The game is over when the star on top’s radius covers the screen. Thats when game over function screen shows up.

To reset your game, all you need to do is to put it back into the state that it was in when it started. That means you need to reset the values in all your variables to their initial state. The best way to do this is with a reset function. For example, if your game looks like this:

int score = 0;
int lives = 3;

void setup(){
  size(600,400);
  // Load things here, perhaps fonts or images.
}

void draw(){
  // Game code here.
}

Then you need to add the reset function and change it to this:

int score;
int lives;

void setup(){
  size(600,400);
  // Load things here, perhaps fonts or images.
  reset();
}

void reset(){
  score = 0;
  lives = 3;
}

void draw(){
  // Game code here.
  if( reset_time_is_now ){
    reset();
  }
}

Please realize that this is a demonstration of the CONCEPTS involved in doing a reset. You will need to apply this concept to your own code (because there is a lot of it and I’m not doing it for you).

1 Like

how would i possibly reset something that has been shot via collision detection and was generated via a for loop. Because like resetting a for loop is what i want.

The for loop that generates it should be in the reset() function! That way, when the reset() functions runs, the for loop runs again, which puts the objects generated by the for loop back to their original state too.

int score;
int lives;
Enemy[] foes = new Enemy[20]

void setup(){
  size(600,400);
  // Load things here, perhaps fonts or images.
  reset();
}

void reset(){
  score = 0;
  lives = 3;
  for( int i = 0; i < foes.length; i++){
    foes[i] = new Enemy(i);
  }
}

void draw(){
  // Game code here.
  if( reset_time_is_now ){
    reset();
  }
}