I created a simple Pong game for my program, currently when the score for one player reaches “10” the window just closes. I’m trying to make it so when it reaches 10 the program will instead just close and then restart, is there a simple way of doing this? Heres my current code (I’m pretty new sorry for the mess)
float ballX = 300; //this determines the location for the ball
float ballY = 300; //this determines the location for the ball
float direction = 2;
float direction2 = 2;
float speed= 1.001; // controls speed
float rightPaddleY1 =300; //controls the movement of right paddle
float rightPaddleY2 =350;
float leftPaddleY1 =40; //controls movement of left paddle
float leftPaddleY2 =90;
int score1 = 0;
int score2 = 0;
void setup() {
background(260, 20, 20);
frameRate(75); //another speed variable
size(600, 500);
textSize(30);
}
void draw() {
background(100, 80, 160);
text( score1, 30, 60 ); //Score counter display for P1
text( score2, 550, 60 ); //Score counter display for P2
text( "Player 1", 10, 30); //Player 1
text( "Player 2", 480, 30); //Player 2
strokeWeight(2); //Thickness of ball
ellipse(ballX, ballY, 20, 20); //ball
strokeWeight(5); // thickness of paddle
line(580, rightPaddleY1, 580, rightPaddleY2); // These are the paddles
line(20, leftPaddleY1, 20, leftPaddleY2);
if (score1 == 10) { //Sets player ones win condition (10 score)
text("Player 1 Wins", 2+0, 300);
}
if (score2 == 10) { // sets player twos win condition (10 score)
text("Player 2 Wins", 400, 300);
}
if (score1 == 11) { // These two lines close the game after win conditions have been met
exit();
}
if (score2 == 11) {
exit();
}
if (ballX < 15) { //this resets the ball on the left side
ballX = 300;
ballY = 300;
score2 ++;
direction = -1;
direction2 = -1;
}
if (ballX > 585) { //this resets the ball on the right side
ballX = 300;
ballY = 300;
score1 ++;
direction = -1;
direction2 = -1;
}
if (ballX < 32) { //Makes left paddle collide
if (ballY > leftPaddleY1 -10 && ballY < leftPaddleY1 +70) {
direction = direction * -1;
}
}
if (ballX > 568) { //Makes right paddle collide
if (ballY > rightPaddleY1 -15 && ballY < rightPaddleY1 +60) {
direction = direction * -1;
}
}
if (keyPressed) { //All of these control the left paddle
if (key == 's' || key == 'S') {
leftPaddleY1 = leftPaddleY1 +10;
}
}
if (keyPressed) {
if (key == 's' || key == 'S') {
leftPaddleY2 = leftPaddleY2 +10;
}
}
if (keyPressed) {
if (key == 'w' || key == 'W') {
leftPaddleY1 = leftPaddleY1 -10;
}
}
if (keyPressed) {
if (key == 'w' || key == 'W') { //Left paddle control end
leftPaddleY2 = leftPaddleY2 -10;
}
}
if (keyPressed) {
if (key == 'l' || key == 'L') { //All of these control the right paddle
rightPaddleY1 = rightPaddleY1 +10;
}
}
if (keyPressed) {
if (key == 'l' || key == 'L') {
rightPaddleY2 = rightPaddleY2 +10;
}
}
if (keyPressed) {
if (key == 'o' || key == 'O') {
rightPaddleY1 = rightPaddleY1 -10;
}
}
if (keyPressed) {
if (key == 'o' || key == 'O') { //Right paddle control end
rightPaddleY2 = rightPaddleY2 -10;
}
}
strokeWeight(0);
//This makes the ball collide with the bottom
if (ballY > 490) {
direction2 = direction2 * -1;
fill(#1DDA17);
}
//This makes the ball collide with the top
if (ballY < 10) {
direction2 = direction2 * -1;
fill(#1DDA17);
}
direction = direction * speed;
direction2 = direction2 * speed;
ballY = ballY + direction2;
ballX = ballX + direction;
}