Game pause and return key issues

I have an application that manages the title screen and the game screen. I would like to know how to do so that when it is in the game screen and the “p” key is pressed, the game is paused and when it is paused and it is pressed again “p” is unpaused.

One problem I have is that to pass the level I have the return key set to go from the title screen to the game screen but it doesn’t work.

//tittle screen---------------------------------------------
class Title_Screen{
  private float x1 = 180.0,y1 = 150.0;
  private float x2 = 250.0,y2 = 400.0;
  
  void draw(){
    background(85,106,206);
    textSize(100.0);
    text("PONG",x1,y1);
    textSize(50.0);
    text("STAR",x2,y2);
  }  
} //--------------------------------------------------------

//game screen-----------------------------------------------
class Game_Screen{
  private float x = 150,y =  150.0;
  Player player;
  
  Game_Screen(){
    player = new Player();
  }
  
  void draw(){
    background(0,0,0);
    textSize(75.0);
    text("GAMEPLAY",x,y);
    player.draw();
  }

} //--------------------------------------------------------

//player----------------------------------------------------
class Player{
  private float x = 320,y = 240,speed = 5;
  
  void draw(){
    if(keyPressed && key == CODED && keyCode == LEFT){
      x -= speed;
    }else if(keyPressed && key == CODED && keyCode == RIGHT){
      x += speed;
    }
    
    if(keyPressed && key == CODED && keyCode == UP){
      y -= speed;
    }else if(keyPressed && key == CODED && keyCode == DOWN){
      y += speed;
    }
    
    rect(x,y,50.0,50.0);
  }
} //--------------------------------------------------------


//---------------------------------------------------------
Title_Screen title;
Game_Screen game;
int scene = 0;

void settings(){
  size(640,480);
  noSmooth();
}

void setup(){
  frameRate(60);
  title = new Title_Screen(); 
  game = new Game_Screen();
}

void draw(){
  if(scene == 0){
    title.draw();
  }else if(scene == 1){
    game.draw();
  }
}

void keyPressed(){
  if(key == CODED && keyCode == RETURN){
    scene = 1;
  }else if(key == '0'){
    scene = 0;
  }
}
1 Like

Enter Key

ENTER is not coded

The keys included in the ASCII specification (BACKSPACE, TAB, ENTER, RETURN, ESC, and DELETE) do not require checking to see if they key is coded, and you should simply use the key variable instead of keyCode

and

Check for both ENTER and RETURN to make sure your program will work for all platforms.

see key / Reference / Processing.org

I also recommend to distinguish between states in keyPressed as you do in draw().

You can also name the states:

final int STATE_START =0;  // I haven't done this 
final int STATE_GAME  =1;

Full code

Here Enter works and p brings you to start screen again and back. I discussed the pause question below.



//tittle screen---------------------------------------------
class Title_Screen {
  private float x1 = 180.0, y1 = 150.0;
  private float x2 = 250.0, y2 = 400.0;

  void draw() {
    background(85, 106, 206);
    textSize(100.0);
    text("PONG", x1, y1);
    textSize(50.0);
    text("STAR", x2, y2);
  }
} //--------------------------------------------------------

//game screen-----------------------------------------------
class Game_Screen {
  private float x = 150, y =  150.0;
  Player player;

  Game_Screen() {
    player = new Player();
  }

  void draw() {
    background(0, 0, 0);
    textSize(75.0);
    text("GAMEPLAY", x, y);
    player.draw();
  }
} //--------------------------------------------------------

//player----------------------------------------------------
class Player {
  private float x = 320, y = 240, speed = 5;

  void draw() {
    if (keyPressed && key == CODED && keyCode == LEFT) {
      x -= speed;
    } else if (keyPressed && key == CODED && keyCode == RIGHT) {
      x += speed;
    }

    if (keyPressed && key == CODED && keyCode == UP) {
      y -= speed;
    } else if (keyPressed && key == CODED && keyCode == DOWN) {
      y += speed;
    }

    rect(x, y, 50.0, 50.0);
  }
} //--------------------------------------------------------


//---------------------------------------------------------
Title_Screen title;
Game_Screen game;
int scene = 0;

void settings() {
  size(640, 480);
  noSmooth();
}

void setup() {
  frameRate(60);
  title = new Title_Screen();
  game = new Game_Screen();
}

void draw() {
  if (scene == 0) {
    title.draw();
  } else if (scene == 1) {
    game.draw();
  }
}

void keyPressed() {

  if (scene == 0) {
    if ( key== RETURN  || key == ENTER ) {
      scene = 1;
    } else if (key == 'p') {
      scene = 1;
    }
  } else if (scene == 1) {
    if (key == '0') {
      scene = 0;
    } else if (key == 'p') {
      scene = 0;
    }
  }
}
//

1 Like

when you have a variable boolean paused just say

if(key=='p') {
    paused = !paused; // toggles 
}

then in the program where you move the ball just say if (! paused) ....

before setup() say boolean paused = false;

or make a 3rd state PAUSE

Chrisir

1 Like

for your approach:

also check out the new approach (!) here: Scenes in Processing - #12 by Chrisir

It uses an interface


interface StateInterface {

  // Let's abstract

  // Let's abstract display()
  void showState();
  void keyPressedState();
  void mousePressedState();
}
//

Therefore, you can have the states all using this interface

You can also make keyPressed part of the class

1 Like

@Chrisir Tell me if the pause code is correct.

//tittle screen---------------------------------------------
class Title_Screen{
  private float x1 = 180.0,y1 = 150.0;
  private float x2 = 250.0,y2 = 400.0;
  
  void draw(){
    background(85,106,206);
    textSize(100.0);
    text("PONG",x1,y1);
    textSize(50.0);
    text("STAR",x2,y2);
  }  
} //--------------------------------------------------------

//game screen-----------------------------------------------
class Game_Screen{
  private float x = 150,y =  150.0;
  Player player;
  
  Game_Screen(){
    player = new Player();
  }
  
  void draw(){
    if(!pause){
      background(0,0,0);
      textSize(75.0);
      text("GAMEPLAY",x,y);
      player.draw();
    }
    
    if(pause){
      textSize(50.0);
      text("PAUSE",200,300);
    }
  }

} //--------------------------------------------------------

//player----------------------------------------------------
class Player{
  private float x = 320,y = 240,speed = 5;
  
  void draw(){
    if(keyPressed && key == CODED && keyCode == LEFT){
      x -= speed;
    }else if(keyPressed && key == CODED && keyCode == RIGHT){
      x += speed;
    }
    
    if(keyPressed && key == CODED && keyCode == UP){
      y -= speed;
    }else if(keyPressed && key == CODED && keyCode == DOWN){
      y += speed;
    }
    
    rect(x,y,50.0,50.0);
  }
} //--------------------------------------------------------


//---------------------------------------------------------
Title_Screen title;
Game_Screen game;
boolean pause = false;
int scene; 
final int state_title = 0;
final int state_game = 1;

void settings(){
  size(640,480);
  noSmooth();
}

void setup(){
  frameRate(60);
  title = new Title_Screen(); 
  game = new Game_Screen();
}

void draw(){
  if(scene == state_title){
    title.draw();
  }else if(scene == state_game){
    game.draw();
  }
}

void keyPressed(){
  if(key == RETURN || key == ENTER){
    scene = state_game;
  }else if(key == '0'){
    scene = state_title;
  }
  
  if(key == 'p'){
    pause = !pause;
  }
}

Nicely Done! Congrats!

Remarks

not //tittle screen---------------------------------------------
it’s //title screen---------------------------------------------

inserted “else if” in keypressed

Why don’t you distinguish by state in keyPressed() as you do in draw()?

instead of if (!pause) { and if (pause) { use “else” :

if (!pause) {
    ....
} else {
    ....
}

Start screen: “STAR” or “START” ?


Full Code

// PONG Game

//title screen---------------------------------------------
class Title_Screen {
  private float x1 = 180.0, y1 = 150.0;
  private float x2 = 250.0, y2 = 400.0;

  void draw() {
    background(85, 106, 206);
    textSize(100.0);
    text("PONG", x1, y1);
    textSize(50.0);
    text("START", x2, y2);
  } // method
} // class --------------------------------------------------------

//game screen-----------------------------------------------
class Game_Screen {
  private float x = 150, y =  150.0;
  Player player;

  Game_Screen() {
    player = new Player();
  } // constr

  void draw() {
    if (!pause) {
      background(0, 0, 0);
      textSize(75.0);
      text("GAMEPLAY", x, y);
      player.draw();
    } else {  // using else here
      textSize(50.0);
      text("PAUSE", 200, 300);
    }
  }// method
} // class --------------------------------------------------------

//player----------------------------------------------------
class Player {
  private float x = 320, y = 240, speed = 5;

  void draw() {
    if (keyPressed && key == CODED && keyCode == LEFT) {
      x -= speed;
    } else if (keyPressed && key == CODED && keyCode == RIGHT) {
      x += speed;
    }

    if (keyPressed && key == CODED && keyCode == UP) {
      y -= speed;
    } else if (keyPressed && key == CODED && keyCode == DOWN) {
      y += speed;
    }

    rect(x, y, 50.0, 50.0);
    x++;
  } // method
} // class --------------------------------------------------------


//---------------------------------------------------------

Title_Screen title;
Game_Screen game;

boolean pause = false;

final int state_title = 0;
final int state_game = 1;
int scene = state_title;

void settings() {
  size(640, 480);
  noSmooth();
}

void setup() {
  frameRate(60);
  title = new Title_Screen();
  game = new Game_Screen();
}

void draw() {
  if (scene == state_title) {
    title.draw();
  } else if (scene == state_game) {
    game.draw();
  }
}

void keyPressed() {
  if (key == RETURN || key == ENTER) {
    scene = state_game;
  } else if (key == '0') {
    scene = state_title;
  } else if (key == 'p') { // inserted else here (else if)
    pause = !pause;
  }
}
//
2 Likes

@Chrisir Thank you very much for all the help and for your kindness. All the best :slightly_smiling_face:

1 Like