How to put a "replay" button on my game?

Hello everyone, i made a small game with processing (coded in JAVA) for my exams. But i still don’t know how to replay the game when it’s lost. I already searched on the net and tried few things but it doesn’t work. So I wonder, could you help me ?

That’s my code : https://pastebin.com/hUJqAYKa

Thank’s if you could help me.
I don’t have any idea, i’m pretty bad at coding, our teacher made most of this code.

1 Like

The idea will be to create a function that reset all the variables who change during your game. Like this :

void resetGame(){
  point = 0;
  // and so on
}

And then call it when you want, for example when the player press space :

void keyPressed() {
  
  if (keyCode==32)  resetGame();
  
}

Have fun finding which variable have to be initialized :slightly_smiling_face:

3 Likes

If you are planning on “setting up” state in a sketch more than once, you can also make your resetGame code that you call from setup – so everything that you want to do on any setup, including the very first one – not just resets.

int score;
void setup(){
  size(200,200);
  setupGame();
}
void draw(){
}
void keyReleased(){
  if(key=' '){
    setupGame();
  }
}
void setupGame(){
  score=0;
}
1 Like