# Game pause and return key issues

**URL:** https://discourse.processing.org/t/game-pause-and-return-key-issues/41702
**Category:** Coding Questions
**Created:** [April 12, 2023, 8:46pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702 "2023-04-12T20:46:37Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![hokuto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hokuto/32/2139_2.png) [@hokuto](https://discourse.processing.org/u/hokuto)
#### Post date: [April 12, 2023, 8:46pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/1 "2023-04-12T20:46:37Z")

</div>

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.

```auto
//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;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 12, 2023, 9:02pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/2 "2023-04-12T21:02:59Z")

</div>

**Enter Key**

> [@hokuto](#):
>
> I have the return key set to go from the title screen to the game screen but it doesn’t work.

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](https://processing.org/reference/key.html)

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

You can also name the states:

```auto
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.

```auto

//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;
    }
  }
}
//

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 12, 2023, 9:04pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/3 "2023-04-12T21:04:22Z")

</div>

> [@hokuto](#):
>
> 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.

when you have a variable boolean paused just say

```auto
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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 13, 2023, 11:10am UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/4 "2023-04-13T11:10:00Z")

</div>

for your approach:

also check out the new approach (!) here: [Scenes in Processing - #12 by Chrisir](https://discourse.processing.org/t/scenes-in-processing/40386/12)

It uses an interface

```auto

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

---

<div class="post-metadata">

### Author: ![hokuto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hokuto/32/2139_2.png) [@hokuto](https://discourse.processing.org/u/hokuto)
#### Post date: [April 13, 2023, 1:12pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/5 "2023-04-13T13:12:22Z")

</div>

@Chrisir Tell me if the pause code is correct.

```auto
//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;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 13, 2023, 1:23pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/6 "2023-04-13T13:23:29Z")

</div>

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`” :

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

```

Start screen: “STAR” or “START” ?

* * *

**Full Code**

```auto
// 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;
  }
}
//

```

---

<div class="post-metadata">

### Author: ![hokuto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hokuto/32/2139_2.png) [@hokuto](https://discourse.processing.org/u/hokuto)
#### Post date: [April 13, 2023, 7:04pm UTC](https://discourse.processing.org/t/game-pause-and-return-key-issues/41702/7 "2023-04-13T19:04:41Z")

</div>

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