Pong game end screen buttons not working

Hi, for some reason my end game screen at the very end, when I press the respected keys doesn’t go to that stage. When I do it on the menu screen however it works. I have no idea why.

//all major variables
int x,y,w,h,speedX,speedY;//circle variables
int paddleX, paddleY,paddlew,paddleH,paddleS,paddleX2,paddleY2,paddlew2,paddleH2,paddleS2;//left and right paddles variables
boolean up,down;//up down for right paddle
boolean positive, negative;//up down for left paddle
boolean a_pressed=false;//pauses game
boolean q_pressed=false;//unpauses game
int scoreL=0;
int scoreR=0;
int stage=1;
void setup() {
  size(600,600);
  //circle variables values
  x=width/2;
  y=height/2;
  w=50;
  h=50;
  speedX=3;
  speedY=3;
  //paddle 1 values
  paddleX=570;
  paddleY=height/2;
  paddlew=30;
  paddleH=100;
  paddleS=5;
  //paddle 2 values
  paddleX2=10;
  paddleY2=height/2;
  paddlew2=30;
  paddleH2=100;
  paddleS2=5;
  //switching screens
  print("Stage 1=Menu","Stage 2=Game","Stage 3=Instructions","Stage 4=End game");
}
  void draw() {
  if(stage==1) {
    background(255,255,0);
    textSize(60);
    fill(0,255,0);
    text("Ping Pong Game",100,250);
    textSize(30);
    text("Press 1 to play game",150,300);
    text("Press z for instructions",150,350);
  }
   

if(stage==2) {
 background(0);
fill(224);
rect(paddleX,paddleY,paddlew,paddleH);
rect(paddleX2,paddleY2,paddlew2,paddleH2);
fill(255,0,0);
ellipse(x,y,w,h);
textSize(30);
fill(0,255,255);
text(scoreL,250,50);
text("L SCORE:",100,50);
text(scoreR,500,50);
text("R SCORE:",350,50);
  
//moving circle
x=x+speedX;
y=y+speedY;
//preventing it leaving screen
if(x>width-w/2){
  speedX=-speedX;
  noLoop();// left paddle ,stops game if ball hits screen and player misses to hit it(end game)
  delay(4000);
  stage=4;
}else if(x<0+w/2) {
  speedX=-speedX;
  noLoop();// right paddle ,stops game if ball hits screen and player misses to hit it(end game)
  delay(4000);
  stage=4;
}
if(y>height-h/2){
  speedY=-speedY;
}else if(y<0+h/20){
  speedY=-speedY;
}
//moving paddle
if(up) {
paddleY=paddleY+paddleS;
}
if(positive) {
  paddleY2=paddleY2+paddleS2;
}
if(down) {
  paddleY=paddleY-paddleS;
  }
if(negative) {
  paddleY2=paddleY2-paddleS2;
}
//preventing paddle from going off screen
if(paddleY-paddleH/2<0) {
  paddleY=paddleY+paddleS;
}
if(paddleY2-paddleH2/2<0) {
  paddleY2=paddleY2+paddleS2;
}
if(paddleY+paddleH/2>height) {
  paddleY=paddleY-paddleS;
}
if(paddleY2+paddleH2/2>height) {
  paddleY2=paddleY2-paddleS2;
}
//ball bouncing off paddle
if(x>paddleX - w/2 && x < paddleX+paddlew - w/2 && y < paddleY + paddleH + h/2 && y > paddleY - h/2) {
speedX=-speedX;
scoreR=scoreR+10;
}
if(x>paddleX2 + w/2 && x < paddleX2 + paddlew2 + w/2 && y < paddleY2 + paddleH2 + h/2 && y > paddleY2 - h/2) {
speedX=-speedX;
scoreL=scoreL+10;
}
}
//instructions
if(stage==3) {
 background(255);
  textSize(40);
  fill(0,255,255);
 String word="Game Instructions:   ";
 String line1= "\nThe goal of this pong game is to verse another player in a computer game of ping pong";
 String line2= "\nThe paddles are on the left and right side.To move the right  paddle press u to go up and press d to go down";
 String  line3="\nTo move the left paddle, press p to go up and press m to go down";
 String line4= "\nIf either player fails to hit the incoming ball and it hits the side, the game is over and";
 String line5= "\nwhoever has highest score wins";
 String line6= "\nThe balls can bounce off the bottom and top so that is covered";
 String line7= "\nIf you want to pause the game you can press a and to unpause you can press q";
 textSize(10);
 String line8= "\nPRESS 1 TO START GAME";
 //text coordinates
  text(word, 250, 25);
  text(line1, 25, 60);
  text(line2, 25, 110);
  text(line3, 25, 150);
  text(line4, 25, 200);
  text(line5, 25, 240);
  text(line6, 25, 280);
  text(line7, 25, 320);
  text(line8,25,500);
}
//end screen
if(stage==4) {
  background(255,0,127);
  textSize(80);
  textAlign(CENTER);
  fill(0,153,153);
  text( "END GAME",300,300);
  textSize(40);
  text("Press 0 for menu", 300,350);
  text("Press 1 to play again",300,400);
}
  }
// key pressed/released variables (refer to variable table)

void keyPressed() {
  if(key=='d') {
    up=true;
  }
  if(key=='u') {
    down=true;
  }
  if(key=='m') {
    positive=true;
  }
  if(key=='p') {
    negative=true;
  }
  if(key=='a'){
  a_pressed=true;
  noLoop();
}
if(key=='q') {
  q_pressed=true;
  loop();
}
if(key=='1') {
  stage=2;
  }
 if(key=='z') {
   stage=3;
 }
 if(key=='0') {
   stage=1;
}
}


void keyReleased() {
  if(key=='d') {
    up=false;
  }
  if(key=='u') {
    down=false;
  }
  if(key=='m') {
    positive=false;
  }
  if(key=='p') {
    negative=false;
  }
  if(key=='a'){
  a_pressed=true;
  noLoop();
}
if(key=='q') {
  q_pressed=true;
  loop();
}
if(key=='1') {
  stage=2;
} 
if(key=='z') {
   stage=3;
 }
  if(key=='0') {
   stage=1;
}
}


1 Like

You must activate the loop after a game has ended
If the loop is disabled, processing cannot detect pressed buttons. If you remove noLoop, processing will also recognize keyActions while delay


//preventing it leaving screen
if(x>width-w/2){
  speedX=-speedX;
  noLoop();// left paddle ,stops game if ball hits screen and player misses to hit it(end game)
  delay(4000);
  loop();
  stage=4;
}else if(x<0+w/2) {
  speedX=-speedX;
  noLoop();// right paddle ,stops game if ball hits screen and player misses to hit it(end game)
  delay(4000);
  loop();
  stage=4;
}
3 Likes

Ideally, do not use delay() at all – it causes your screen to freeze. Instead, a simple timer that allows your sketch to continue.

// something happened -- lets go into delay mode 
mydelay = frameCount + 240; // 4 seconds @ 60fps
void draw() {
  if(frameCount > mydelay) {
    // all the things you do when not delaying
  }
}
1 Like