# Holding the ball in pong

**URL:** <https://discourse.processing.org/t/holding-the-ball-in-pong/11520>\
**Category:** Coding Questions\
**Created:** [May 23, 2019, 8:09pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520 "2019-05-23T20:09:47Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 27, 2019, 6:40pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/61 "2019-05-27T18:40:20Z")

</div>

the w and s keys in keypreessed?

---

<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:** [May 27, 2019, 6:40pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/62 "2019-05-27T18:40:43Z")

</div>

> [@somedude](#):
>
> keypreessed

Yesssssssssssssssssss

---

<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:** [May 27, 2019, 6:41pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/63 "2019-05-27T18:41:25Z")

</div>

why not put it there!!!

Also as I said, not xpos but ypos

for questions post your entire code

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 27, 2019, 6:44pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/64 "2019-05-27T18:44:42Z")

</div>

it worked

```auto
  int size = 20;    
  float xpos, ypos;    
  float xspeed = random(5, 6);
  float yspeed = random(0.6, 4);
  
  float fehler1=0;
  float fehler2=0;
  
  float playerpos1;
  float p1x;
  float playerpos2;
  float p2x;
  
  String tela;
  
  boolean trocou;
  
  int scoreE = 0;
  int scoreD = 0;
  int scoreW = 10;
  
  boolean hold=false;
  boolean attachedToPaddle=false;
  boolean move = true;
  
  float easing = 0.05;
  
  void setup() {
    size(640, 400);
    noStroke();
    tela = "MENU";
    frameRate(30);
    smooth();
    playerpos1 =height/2;
    playerpos2 =height/2;
    xpos = 45;
    ypos = playerpos1;
    p1x = 10;
    p2x = width-30;
  }
  
  void draw() {
  
    switch (tela) {
  
      // ------------------------------------
    case "GAME":
  
      background(277);
  
      if (hold && attachedToPaddle)
      {
        // ignore
      } else 
      {
        xspeed *= 1.001;
  
        xpos += xspeed;
        ypos += yspeed;
      }
  
      scores();
      hold();
  
      if (ypos > height-size/2 || ypos < 0+size/2) {
        yspeed *= -1;
      }
  
      if ( xpos <= 30+size/2) {
        if (ypos > playerpos1-50 && ypos < playerpos1+50) {
          if ( ! (hold && attachedToPaddle)) {
            xspeed *= -1;
            yspeed += (ypos-playerpos1)/5;
          }
          attachedToPaddle=true;
        }
      } else {
        attachedToPaddle=false;
      }
  
  
      if (xpos <= 0) {
        fehler1 += 1;
        if (fehler1 <= 4) {
          xpos = 50;
          ypos = playerpos1;
          xspeed = random(5, 6);
          xspeed *= -1;
          yspeed = 0; 
          println("player 2: "+ str(fehler2)+":"+str(fehler1) );
        }
        if (fehler1 ==5) {
          println("player 2 win 5:"+ str(fehler2));
        }
      }
  
      if ( xpos >= width-30-size/2) {
        if (ypos > playerpos2-50 && ypos < playerpos2+50) {
          xspeed *= -1;
          yspeed += (ypos-playerpos2)/5;
        }
      }
      if (xpos >= width) {
        fehler2 +=1 ;
        if (fehler2 <= 4) {
          xpos = width-50;
          ypos = playerpos2;
          xspeed = -random(5, 6);
          xspeed *= -1;
          yspeed = 0; 
          println("ponto: "+ str(fehler2)+":"+str(fehler1) );
        }
        if (fehler2 ==5) {
          println("player 1 ganhou 5:"+ str(fehler1));
        }
      }
  
      ellipse(xpos, ypos, size, size);
      rect (p1x, playerpos1-50, 20, 100);
      rect (p2x, playerpos2-50, 20, 100);
  
      text(str(hold), 333, 33);
  
      break; 
  
      // ------------------------------------
    case "MENU":
      menu();
      break;
  
      // ------------------------------------
    case "TUTORIAL":
      tutorial();
      break;
  
    }
  }
  
  void menu() {
    background(255, 0, 0);
    fill(0, 0, 0); 
    text ("MENU", 
      111, 111);
  }
  
  // --------------------------------------------------------------------------
  
  void keyPressed() {
  
  
    if (key == 'w') {
      if (playerpos1 >=50)
        playerpos1 = playerpos1 - 8;
    }
    if (key == 's') {
      if (playerpos1 <=height-50)
        playerpos1 = playerpos1 + 8;
    }
    if (key =='a') { 
      p1x = -8;
    }
    if (key == 'd') {
      p1x = +8;
    }
    if (hold && attachedToPaddle){
     ypos = playerpos1+8; 
    }
  
    // -------------------
  
    if (key == '7')
    {
      if (playerpos2 >=50)
        playerpos2 = playerpos2 - 8;
    }
    if (key == '1') {
      if (playerpos2 <=height-50)
        playerpos2 = playerpos2 + 8;
    }
    /*if (key == '3') {
     xspeed = 0;
     yspeed = 0;
     move = false;
     }
     */
    if (key == ' ') {
      hold = ! hold;
    }
  
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
  }
  
  void keyReleased() {
    //hold = false;
    //attachedToPaddle=false;
  }
  
  void mousePressed() {
  
    switch (tela) {
  
      // ------------------------------------
    case "GAME":
      break; 
  
      // ------------------------------------
    case "MENU":
      tela="GAME"; 
      break;
  
      // ------------------------------------
    case "TUTORIAL":
      tela="tutorial";
      break;
    }
  }
  
  // --------------------------------------------------------------------------
  
  void tutorial() {
    background(277);
    xspeed *= 1.001;
    xpos += xspeed;
    ypos += yspeed;
    scores();
    tela = "menu";  
    background(0, 255, 0);
    trocou = true;
  }
  
  
  void scores() {
    fill(255);
    text(scoreE, 100, 50);
    text(scoreD, width-100, 50);
    if (xpos >640) {
      scoreE += 1;
      xspeed = xspeed*2;
      yspeed = yspeed*2;
    }
    if (xpos < 0 ) {
      scoreD += 1;
      xspeed = xspeed*1.5;
      yspeed = yspeed*1.5;
    }
  }
  
  void AI() {
    playerpos2 = xpos * easing;
  }
  
  void hold() {
    if (hold) {
      if (xpos == playerpos1) {
        xpos=xpos-8;
        // xspeed = 0;
      }
    }
  }
  //

```

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 27, 2019, 6:50pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/65 "2019-05-27T18:50:28Z")

</div>

now the last thing i need help on is switching on an ai that i made for the game

---

<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:** [May 27, 2019, 6:56pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/66 "2019-05-27T18:56:01Z")

</div>

that’s how would do it

```auto
  if (key == 'w') {
    if (playerpos1 >=50) {
      playerpos1 = playerpos1 - 8;
      if (hold && attachedToPaddle) {
        ypos = ypos-8;
      }
    }
  }

```

same for s of course

**ALSO**

in draw()

use this

```auto
    if (xpos <= 0) {
      attachedToPaddle=false;
      fehler1 += 1;
      if (fehler1 <= 4) {

```

otherwise you hold when ball passes the paddly by

---

<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:** [May 27, 2019, 7:31pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/67 "2019-05-27T19:31:11Z")

</div>

> [@somedude](#):
>
> now the last thing i need help on is switching on an ai that i made for the game

just call AI() from draw() in the right section of switch()

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 27, 2019, 7:33pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/68 "2019-05-27T19:33:04Z")

</div>

ok i’ll give it a try

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 28, 2019, 3:42pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/69 "2019-05-28T15:42:03Z")

</div>

how can i turn the AI() on and off?

---

<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:** [May 28, 2019, 3:45pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/70 "2019-05-28T15:45:57Z")

</div>

In the screen **menu** You could have a `text()` hit space bar to switch AI on / off (and have a global `boolean AIisOn` and display its value here)

And then evaluate space bar in keyPressed  
and change `AIisOn` accordingly

In draw() call AI() only when AIisOn: if(AIisOn) {AI();}

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 28, 2019, 3:49pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/71 "2019-05-28T15:49:08Z")

</div>

actually im gonna put a image over it

---

<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:** [May 28, 2019, 3:52pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/72 "2019-05-28T15:52:43Z")

</div>

please don’t write so short…

where do you put which image over what please?

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 28, 2019, 3:55pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/73 "2019-05-28T15:55:27Z")

</div>

over the switch case like in tutorial() , a credits funcion im writing and a menu(), all inside a data folder inside the program file

---

<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:** [May 28, 2019, 3:55pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/74 "2019-05-28T15:55:50Z")

</div>

You can have a button like a checkbox or 2 radio buttons that you can click with the mouse to set AIisOn

**2 radio buttons**

- [] Game of two players
- [X] Game of one player against the computer

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 28, 2019, 3:56pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/75 "2019-05-28T15:56:35Z")

</div>

using keys sounds easier to program rather then drawing boxes and progaming their collision.

---

<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:** [May 28, 2019, 3:58pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/76 "2019-05-28T15:58:01Z")

</div>

> [@somedude](#):
>
> over the switch case like in tutorial() , a credits funcion im writing and a menu(), all inside a data folder inside the program file

good luck!

Seriously, the switch in draw() is your friend.

When you use images, show them in tutorial and in menu but not in draw

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 28, 2019, 3:58pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/77 "2019-05-28T15:58:40Z")

</div>

ok thanks so much for the help in the code man.

---

<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:** [May 28, 2019, 3:59pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/78 "2019-05-28T15:59:18Z")

</div>

You are welcome!

Come back when you have more questions!

---

<div class="post-metadata">

**Author:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 28, 2019, 5:45pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/79 "2019-05-28T17:45:17Z")

</div>

hey i got a bug, when the player wins when the ai is on the score keeps increasing

```auto
  int size = 20;    
  float xpos, ypos;    
  float xspeed = random(5, 6);
  float yspeed = random(0.6, 4);
  
  float fehler1=0;
  float fehler2=0;
  
  float playerpos1;
  float p1x;
  float playerpos2;
  float p2x;
  
  String tela;
  
  boolean trocou;
  
  int scoreE = 0;
  int scoreD = 0;
  int scoreW = 10;
  
  boolean hold=false;
  boolean attachedToPaddle=false;
  boolean move = true;
  
  float easing = 0.50;
  
  boolean AIon = false;

  
  void setup() {
    size(640, 400);
    noStroke();
    tela = "MENU";
    frameRate(30);
    smooth();
    playerpos1 =height/2;
    playerpos2 =height/2;
    xpos = 45;
    ypos = playerpos1;
    p1x = 10;
    p2x = width-30;
  }
  
  void draw() {
  
    switch (tela) {
      // ------------------------------------
    case "GAME":
  
      background(277);
  
      if (hold && attachedToPaddle)
      {
        // ignore
      } else 
      {
        xspeed *= 1.001;
  
        xpos += xspeed;
        ypos += yspeed;
      }
  
      scores();
      hold();
      if(AIon == true){
       AI(); 
      }
  
      if (ypos > height-size/2 || ypos < 0+size/2) {
        yspeed *= -1;
      }
  
      if ( xpos <= 30+size/2) {
        if (ypos > playerpos1-50 && ypos < playerpos1+50) {
          if ( ! (hold && attachedToPaddle)) {
            xspeed *= -1;
            yspeed += (ypos-playerpos1)/5;
          }
          attachedToPaddle=true;
        }
      } else {
        attachedToPaddle=false;
      }
  
  
      if (xpos <= 0) {
        fehler1 += 1;
        if (fehler1 <= 4) {
          xpos = 50;
          ypos = playerpos1;
          xspeed = random(5, 6);
          xspeed *= -1;
          yspeed = 0; 
          println("player 2: "+ str(fehler2)+":"+str(fehler1) );
        }
        if (fehler1 ==5) {
          println("player 2 win 5:"+ str(fehler2));
        }
      }
  
      if ( xpos >= width-30-size/2) {
        if (ypos > playerpos2-50 && ypos < playerpos2+50) {
          xspeed *= -1;
          yspeed += (ypos-playerpos2)/5;
        }
      }
      if (xpos >= width) {
        fehler2 +=1 ;
        if (fehler2 <= 4) {
          xpos = width-50;
          ypos = playerpos2;
          xspeed = -random(5, 6);
          xspeed *= -1;
          yspeed = 0; 
          println("ponto: "+ str(fehler2)+":"+str(fehler1) );
          text(height/2, width/2, 50, 50);
        }
        if (fehler2 ==5) {
          println("player 1 ganhou 5:"+ str(fehler1));
        }
      }
  
      ellipse(xpos, ypos, size, size);
      rect (p1x, playerpos1-50, 20, 100);
      rect (p2x, playerpos2-50, 20, 100);
  
      break; 
  
      // ------------------------------------
    case "MENU":
      menu();
      break;
  
      // ------------------------------------
    case "TUTORIAL":
      tutorial();
      break;
      
      case"cred":
      cred();
      break;
  
    }
    if (xpos <= 0) {
      attachedToPaddle=false;
      fehler1 += 1;
      if (fehler1 <= 4) {
      }
    }
  }
  
  void menu() {
    background(255, 0, 0);
    fill(0, 0, 0); 
    text ("MENU", 
      111, 111);
  }
  
  // --------------------------------------------------------------------------
  
  void keyPressed() {
  
  
    if (key == 'e') {
      if (playerpos1 >=50)
        playerpos1 = playerpos1 - 8;
    }
    if (key == 'd') {
      if (playerpos1 <=height-50)
        playerpos1 = playerpos1 + 8;
    }
    if (hold && attachedToPaddle){
     ypos = playerpos1+8; 
    }
  
    // -------------------
  
    if (key == '9')
    {
      if (playerpos2 >=50)
        playerpos2 = playerpos2 - 8;
    }
    if (key == '3') {
      if (playerpos2 <=height-50)
        playerpos2 = playerpos2 + 8;
    }
    /*if (key == '3') {
     xspeed = 0;
     yspeed = 0;
     move = false;
     }
     */
    if (key == ' ') {
      hold = ! hold;
    }
  
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    
    if (key == 'a'){
     AIon = true; 
    }
    
    if (key == 67){
     tela="cred";
    }
  }
  
  void keyReleased() {
    //hold = false;
    //attachedToPaddle=false;
  }
  
  void mousePressed() {
  
    switch (tela) {
  
      // ------------------------------------
    case "GAME":
      break; 
  
      // ------------------------------------
    case "MENU":
      tela="GAME"; 
      break;
  
      // ------------------------------------
    case "TUTORIAL":
      tela="tutorial";
      break;
    }
  }
  
  // --------------------------------------------------------------------------
  
  void tutorial() {
    background(277);
    xspeed *= 1.001;
    xpos += xspeed;
    ypos += yspeed;
    scores();
    tela = "menu";  
    background(0, 255, 0);
    trocou = true;
  }
  
  
  void scores() {
    fill(255);
    text(scoreE, 100, 50);
    text(scoreD, width-100, 50);
    if (xpos >640) {
      scoreE += 1;
      xspeed = xspeed*2;
      yspeed = yspeed*2;
    }
    if (xpos < 0 ) {
      scoreD += 1;
      xspeed = xspeed+1.5;
      yspeed = yspeed+1.5;
    }
  }
  
  void AI() {
    playerpos2 = xpos * easing+10;
    
    if(fehler1==5){
      AIon = false;
    }
    
    if(fehler2 == 5){
     AIon = false; 
    }
  }
  
  void hold() {
    if (hold) {
      if (xpos == playerpos1) {
        xpos=xpos-8;
        // xspeed = 0;
      }
    }
  }
  
  void cred(){
   background(150,150,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:** [May 28, 2019, 7:08pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/80 "2019-05-28T19:08:57Z")

</div>

Which function is responsible for the score?

[Previous page](https://discourse.processing.org/t/holding-the-ball-in-pong/11520.md?page=3)

[Next page](https://discourse.processing.org/t/holding-the-ball-in-pong/11520.md?page=5)
