# Pong Game: Trying to add a "Press Space"-key to play the game again in the same playermode in winnerAnnouncer() without going to the home menu again

**URL:** https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325
**Category:** Coding Questions
**Created:** [March 16, 2023, 5:53pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325 "2023-03-16T17:53:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sabisiiuuu](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sabisiiuuu](https://discourse.processing.org/u/sabisiiuuu)
#### Post date: [March 16, 2023, 5:53pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/1 "2023-03-16T17:53:31Z")

</div>

Hello everyone:

The game is complete btw, but I just wanted to add this little feature mentioned in the title before presenting it.  
So when the game’s over, it opens the _winnerAnnouncer()_ function saying who won and to press any key to return to the home menu. However I would like the user to press the Space key to play that same game again ( if they were playing in _onePlayer()_, they will play again that. The same for _twoPlayers()_ )

I only have “Press any key to return to the home menu”, but I am at a loss in how to add the Space key one.

This is part of my code:

```auto
int gameState = 0; // 0: home menu, 1: one player, 2: two players, 4: winner announcer
int player1Score = 0;
int player2Score = 0;
int winningScore = 3;
int numOfPlayers = 1;

boolean gameStarted = false;
boolean isSinglePlayer = true;

Paddle player1, player2;
Ball ball;

void setup() {
  
  size(600, 400);
  rectMode(CENTER);
  
  player1 = new Paddle(20, height/2);
  player2 = new Paddle(width-20, height/2);
  ball = new Ball();
    
}

void draw() {
  
  background(0);
  if (gameState == 0) {
    homeMenu();
  } else if (gameState == 1) {
    onePlayer();
  } else if (gameState == 2) {
    twoPlayers();
  } else if (gameState == 4) {
    winnerAnnouncer();
  }
  
}

void keyPressed() {
  
  if (gameState == 0) {
    if (key == '1') {
      numOfPlayers = 1;
      gameState = 1;
    } else if (key == '2') {
      numOfPlayers = 2;
      gameState = 2;
    }
  } else if (gameState == 1 || gameState == 2) {
    if (!gameStarted) {
      gameStarted = true;
      if (key == '0') {
        gameStarted = false;
        gameState = 0;
      }
    }
    if (key == 'w' || key == 'W') {
      player1.move(-20);
    } else if (key == 's' || key == 'S') {
      player1.move(20);
    }
    if (gameState == 2) {
      if (key == 'i' || key == 'I') {
        player2.move(-20);
      } else if (key == 'k' || key == 'K') {
        player2.move(20);
      }
    }
  } else {
    resetGame();
  }
}

void homeMenu() {
  textAlign(CENTER);
  textSize(64);
  fill(255);
  text("Pong", width/2, height/2 - 50);
  textSize(32);
  text("Press 1 for single player\nPress 2 for multiplayer", width/2, height/2);
}

void onePlayer() {
  if (!gameStarted) {
    textAlign(CENTER);
    textSize(48);
    fill(255);
    text("SINGLE PLAYER", width/2, 120);
    
    textSize(32);
    text("Press any key to start.", width/2, height - 90);
    
    textSize(24);
    text("Press 0 to HOME MENU", 110, 40);
    
  } else {
    if (isSinglePlayer) {
      player2.aiMove(ball);
    }
    updateGame();
  }
}

void twoPlayers() {
  if (!gameStarted) {
    textAlign(CENTER);
    textSize(48);
    fill(255);
    text("MULTIPLAYER", width/2, 100);
        
    textSize(36);
    text("Press any key to start.", width/2, height - 90);
    
    textSize(24);
    text("Press 0 to HOME MENU", 110, 40);
    
  } else {
    updateGame();
  }
}

void winnerAnnouncer() {
  textAlign(CENTER);
  fill(255);
  textSize(48);
  
  if (numOfPlayers == 1) {
    // numOfPlayers == 1 (human vs. bot)

    if (player1Score == winningScore) {
      text("YOU WON", width/2, height/2 - 40);
    } else {
      text("THE BOT WON", width/2, height/2 - 40);
    }

  } else {
    // numOfPlayers == 2

    if (player1Score == winningScore) {
      text("PLAYER 1 WON", width/2, height/2 - 40);
    } else {
      text("PLAYER 2 WON", width/2, height/2 - 40);
    } 
  } 
  textSize(26);
  text("Press Space to play again.", width/2, height/2 + 10);
  
  textSize(26);
  text("Or press any key to return to the HOME MENU.", width/2, height/2 + 50);
}

void updateGame() {
  
  player1.show();
  player2.show();
  
  ball.show();
  ball.update();
  
  ball.checkPaddleCollision(player1);
  
  if (gameState == 2) {
    ball.checkPaddleCollision(player2);
  }
  if (ball.checkWallCollision()) {
    if (ball.xSpeed < 0) {
      player2Score++;
      scoreSound.play();
    } else {
      player1Score++;
      scoreSound.play();
    }
    if (player1Score == winningScore || player2Score == winningScore) {
      gameState = 4;
      winnerSound.play(); 
    } else {
      ball.reset();
    }
  }
  
  ball.checkPaddleCollision(player1);
  if (isSinglePlayer) {
    ball.checkPaddleCollision(player2);
  }
  
}

void resetGame() {
  player1Score = 0;
  player2Score = 0;
  gameStarted = false;
  ball.reset();
  gameState = 0;
}

```

I have a feeling that the solution is very simple, but I no longer have the head for this :'v

Any help is greatly appreciated!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 16, 2023, 7:25pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/2 "2023-03-16T19:25:13Z")

</div>

We want to store the prev gameState and upon key `' '` restore it and set gameState to 1 or 2 (the prev state).

**Details**

> [@sabisiiuuu](#):
>
> `void keyPressed() {`

here you want

```auto
   else if (gameState == 4) {

```

and there say

```auto
if(key==' ') {
     gameState == prevGameState; // which is 1 or 2 (not 0) 
}

```

**Declaration**

declare int prevGameState=1; before setup()

**Store the prev state**

and where you say

```auto
 gameState = 1;

```

or

```auto
 gameState = 2;

```

say `prevGameState = gameState;` to store whether you play with one player or two players.

Chrisir

---

<div class="post-metadata">

### Author: ![sabisiiuuu](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sabisiiuuu](https://discourse.processing.org/u/sabisiiuuu)
#### Post date: [March 16, 2023, 9:24pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/3 "2023-03-16T21:24:00Z")

</div>

Thank you for the answer! It goes back as I wanted, but is there a way to have the scoreboard again to cero-cero ?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 16, 2023, 9:28pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/4 "2023-03-16T21:28:26Z")

</div>

```auto
player1Score = 0;
player2Score = 0;

```

---

<div class="post-metadata">

### Author: ![sabisiiuuu](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sabisiiuuu](https://discourse.processing.org/u/sabisiiuuu)
#### Post date: [March 16, 2023, 9:40pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/5 "2023-03-16T21:40:50Z")

</div>

I was supposed to put that in `else if (gameState == 4) {`.  
My bad

But now I have a small bug, it automatically scores for the second player in both `onePlayer()` and `twoPlayers()` when I go back. Any idea how to fix that?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 16, 2023, 9:57pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/6 "2023-03-16T21:57:33Z")

</div>

Well, I guess, when paddle and ball is still in the same position, the score is immediately counted again.

Consider resetting ball and paddles to some neutral position.

---

<div class="post-metadata">

### Author: ![sabisiiuuu](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@sabisiiuuu](https://discourse.processing.org/u/sabisiiuuu)
#### Post date: [March 16, 2023, 10:29pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/7 "2023-03-16T22:29:23Z")

</div>

I ended up adding `ball.reset();` to `else if (gameState == 4) {` to correct the bug and it worked!

I also added two more functions in the Paddle class to have the paddles again in the middle of the height.

Thank you so much for your help! You were a lifesaver! Idk what I would’ve done without you :''v

Ps. Btw here

```auto
if (key == ' ') {
     gameState == prevGameState; // which is 1 or 2 (not 0) 
}

```

is with one `=`, not `==`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 16, 2023, 10:33pm UTC](https://discourse.processing.org/t/pong-game-trying-to-add-a-press-space-key-to-play-the-game-again-in-the-same-playermode-in-winnerannouncer-without-going-to-the-home-menu-again/41325/8 "2023-03-16T22:33:34Z")

</div>

You are right, my bad.
