Pong Game: Trying to make 4 different messages for when a different player wins: "You won", "bot won", "player 1 won" and "player 2 won". Could only make 2: "Player 1 won" and "Player 2 or Bot won"

Hello everyone!

Basically the question. I am making a Pong Game and it is actually finished, but I just want to make it more perfect. I am only lacking these particular messages for when a winner has to be announced. I could only make “Player 1 won” and “Player 2 or Bot won”. But I would actually like the winnerAnnouncer() function to recognize which play mode was being played: if the onePlayer() or the twoPlayers().

So here is part of my code:

int gameState = 0; // 0: home menu, 1: one player, 2: two players, 3: instructions, 4: winnerAnnouncer
int player1Score = 0;
int player2Score = 0;
int winningScore = 3;

boolean gameStarted = false;
boolean showInstructions = 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 == 3) {
    instructions();
  } else if (gameState == 4) {
    winnerAnnouncer();
  }
}

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

void onePlayer() {
  if (!gameStarted) {
    textAlign(CENTER);
    textSize(48);
    fill(255);
    text("SINGLE PLAYER", width/2, height/2 - 20);
    
    textSize(32);
    text("Press any key to start.", width/2, height/2 + 20);
  } else {
    if (isSinglePlayer) {
      player2.aiMove(ball);
    }
    updateGame();
  }
}

void twoPlayers() {
  if (!gameStarted) {
    textAlign(CENTER);
    textSize(48);
    fill(255);
    text("MULTIPLAYER", width/2, height/2 - 20);
    
    textSize(32);
    text("Press any key to start.", width/2, height/2 + 20);
  } else {
    updateGame();
  }
}

void winnerAnnouncer() {
  textAlign(CENTER);
  fill(255);
  textSize(48);
  if (player1Score == winningScore) {
    text("PLAYER 1 WON", width/2, height/2 - 20);
  } else {
    text("PLAYER 2 or BOT WON", width/2, height/2 - 20);
  }
  
  /*
  if (isOnePlayerMode) {
    if (player1Score == winningScore) {
      text("YOU WON!", width/2, height/2 - 20);
    } else if (player2Score == winningScore) {
      text("BOT WON!", width/2, height/2 - 20);
    }
  } else {
    if (player1Score == winningScore) {
      text("PLAYER 1 WON!", width/2, height/2 - 20);
    } else if (player2Score == winningScore) {
      text("PLAYER 2 WON!", width/2, height/2 - 20);
    }
  }
  */
  
  textSize(26);
  text("Press any key to return to the HOME MENU.", width/2, height/2 + 20);
}

I only sent the relevant code functions, but tell me if it is necessary for me to send the whole thing, as I believe it is not runnable.

Thank you in advance!

I think in the menu you evaluate a keyPressed: because you say:

  • so in keyPressed set a variable numOfPlayers to 1 or 2 in keyPressed()

  • before setup() say int numOfPlayers = 1;

  • Now we have stored the number of players in a clean way.

The next step

The next step is to evaluate numOfPlayers:

Your code:

is to change

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 - 20);
    } else {
      text("THE BOT WON", width/2, height/2 - 20);
    }

  }
  //---------------------
  else {
    // numOfPlayers == 2

    if (player1Score == winningScore) {
      text("PLAYER 1 WON", width/2, height/2 - 20);
    } else {
      text("PLAYER 2 WON", width/2, height/2 - 20);
    }//else 

  }//else 


2 Likes