# 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:** 3

<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 25, 2019, 8:13pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/41 "2019-05-25T20:13:09Z")

</div>

No.

too complicate

finish what you started and don’t go something fancy

finish what you started

---

<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 25, 2019, 8:13pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/42 "2019-05-25T20:13:58Z")

</div>

the key involved is the space bar, the space bar makes the ball held/ attached

---

<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 25, 2019, 8:34pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/43 "2019-05-25T20:34:24Z")

</div>

i tried to put it in if (key == ’ ') and it didn worked.

---

<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 25, 2019, 8:35pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/44 "2019-05-25T20:35:50Z")

</div>

it’s where you move the paddle with the keys

(Remember the ball is already attached)

---

<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 25, 2019, 8:52pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/45 "2019-05-25T20:52:48Z")

</div>

Ok, i tried on both ‘w’ key and and ‘s’ keys but the ball instead goes back  
on the hold function and nothing happens

---

<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 25, 2019, 8:57pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/46 "2019-05-25T20:57:38Z")

</div>

Well show the code

Chrisir

---

<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 25, 2019, 9:00pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/47 "2019-05-25T21:00:18Z")

</div>

> [@Chrisir](#):
>
> xpos=xpos-8;

```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;
      xpos=xpos-8;
  }
  if (key == 's') {
    if (playerpos1 <=height-50)
      playerpos1 = playerpos1 + 8;
      xpos=xpos-8;
  }
  if (key =='a') { 
    p1x = -8;
  }
  if (key == 'd') {
    p1x = +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:** ![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 25, 2019, 9:15pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/48 "2019-05-25T21:15:07Z")

</div>

> [@somedude](#):
>
> playerpos1 = playerpos1 + 8; xpos=xpos-8;

what’s the error here??

---

<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 25, 2019, 9:17pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/49 "2019-05-25T21:17:00Z")

</div>

ah, and another error (actually my error!)

xpos=xpos-8;

do you really want to move the ball in **x** direction?

I gotta go

see you

Chrisir

---

<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 25, 2019, 9:23pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/50 "2019-05-25T21:23:49Z")

</div>

ok thank you, take care

---

<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, 2:06am UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/51 "2019-05-27T02:06:50Z")

</div>

what did you meant by where you move the paddle with the keys, should i put it somewhere in keypressed?

---

<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, 9:11am UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/52 "2019-05-27T09:11:49Z")

</div>

Are you asking about the movement of the ball together with the paddle in the situation where the ball is attached to the paddle?

Then the answer is yes.

Put it in keyPressed where you move the paddle (w etc.)

But have an if clause check the situation

---

<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, 5:35pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/53 "2019-05-27T17:35:18Z")

</div>

so if(hold = true){  
xpos = xpos-8  
}  
?

---

<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, 5:38pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/54 "2019-05-27T17:38:03Z")

</div>

i tried and it didnt held the ball while moving, when i stood still with the ball attached to the paddle it help but when i moved it i launched the ball.

---

<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:16pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/55 "2019-05-27T18:16:41Z")

</div>

> [@somedude](#):
>
> if (hold && attachedToPaddle)

I think this is the if clause you are looking for

---

<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:21pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/56 "2019-05-27T18:21:18Z")

</div>

> [@somedude](#):
>
> if(hold = true){

This is VERY dangerous

you want either if(hold == true){ with a double ==

or just if(hold){

(but with if(hold = true){ you assign true to hold prior to evaluating it with if so it’s always true. Bad.)

---

<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:23pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/57 "2019-05-27T18:23:11Z")

</div>

i tried this code and its still not working, maybe is the the xpos value?

```auto
if (hold && attachedToPaddle){
     xpos = xpos+8; 
    }

```

---

<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:25pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/58 "2019-05-27T18:25:48Z")

</div>

> [@somedude](#):
>
> if (key == ‘s’) { if (playerpos1 \<=height-50) playerpos1 = playerpos1 + 8; xpos=xpos-8; }

did you put it in this section?

post your entire code please

gotta go

---

<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:30pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/59 "2019-05-27T18:30:13Z")

</div>

```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 (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;
    }
    if (hold && attachedToPaddle){
     xpos = xpos+8; 
    }
  }
  
  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:** ![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:32pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/60 "2019-05-27T18:32:39Z")

</div>

??

the idea is that we move the ball together with the paddle when the ball is attached.

WHERE do we move the paddle in keyPressed??

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

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