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!