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

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:

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!

1 Like

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

Details

here you want

   else if (gameState == 4) {

and there say

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

 gameState = 1;

or

 gameState = 2;

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

Chrisir

1 Like

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

player1Score = 0;
player2Score = 0;
1 Like

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?

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.

1 Like

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

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

is with one =, not ==

1 Like

You are right, my bad.

1 Like