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

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

</div>

Hello im coding a pong game with a twist im trying to make it when the player holds the spacebar the bar holds the ball but im having trouble making the ball stop. thanks in advance

```auto

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos1)/5;
    }
  }
  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);
}

void menu() {
  background(255, 0, 0);
}

void keyPressed(){
   if (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 (keyPressed) {
    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 = true;
  }
  } 
}

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*1.5;
   yspeed = yspeed*1.5;
  }
  if(xpos < 0 ){
   scoreD += 1;
   xspeed = xspeed*1.5;
   yspeed = yspeed*1.5;
  }
  }
  
  void AI(){
    playerpos2 = xpos * easing;
  }
  
  void hold(){
    if (hold = true){
       if(xpos == playerpos1){
         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 23, 2019, 8:10pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/2 "2019-05-23T20:10:35Z")

</div>

sorry forgot to post the whole code

```auto
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0, 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;
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()
{
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  scores();
  hold();

switch (tela) {
    case "MENU":
      menu();
      break;
    case "TUTORIAL":
      tutorial();
      break;
}

  if (ypos > height-size/2 || ypos < 0+size/2) {
    yspeed *= -1;
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos1)/5;
    }
  }
  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);
}

void menu() {
  background(255, 0, 0);
}

void keyPressed(){
   if (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 (keyPressed) {
    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 = true;
  }
  } 
}

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*1.5;
   yspeed = yspeed*1.5;
  }
  if(xpos < 0 ){
   scoreD += 1;
   xspeed = xspeed*1.5;
   yspeed = yspeed*1.5;
  }
  }
  
  void AI(){
    playerpos2 = xpos * easing;
  }
  
  void hold(){
    if (hold = true){
       if(xpos == playerpos1){
         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 23, 2019, 8:51pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/3 "2019-05-23T20:51:31Z")

</div>

In an event of collision (plus space bar) set a flag attachedToPaddle

If this is true, don’t add speed to ball

**Remark**

You can also use another flag to monitor if the space bar is down (spaceBarDown)

set it to true in keyPressed and to false in keyReleased

---

<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 24, 2019, 3:44am UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/4 "2019-05-24T03:44:02Z")

</div>

how do i flag the paddles?

---

<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 24, 2019, 4:31am UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/5 "2019-05-24T04:31:52Z")

</div>

> [@somedude](#):
>
> xspeed \*= -1;

I think here you can set the global variable attachedToPaddle to true

---

<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 24, 2019, 3:51pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/6 "2019-05-24T15:51:53Z")

</div>

so i create a boolean for attachedtopaddle and then a void() for attachment and then if(xspeed \*= 1) true?

---

<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 24, 2019, 4:28pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/7 "2019-05-24T16:28:46Z")

</div>

ok i gave it a try, its holding but the ball keeps blinking and it keeps giving points to the left paddle  
` void hold(){  
xspeed = playerpos1;

```
}

```

if (ypos \> height-size/2 || ypos \< 0+size/2) {  
yspeed \*= -1;  
hold = true;  
}`

---

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

</div>

first of all, when you want to make the paddle glue the ball when the space bar is pressed once, you need

```auto
  if (key == ' ') {
    hold = ! hold;
  }

```

so hold gets false again eventually.

> so i create a boolean for attachedtopaddle

Yes, for each paddle? So attachedtopaddle1 and attachedtopaddle2.

> then a void() for attachment and then if(xspeed \*= 1) true?

NO.

What you want to achieve is that the ball doesn’t go on (bounces) but stays put on the paddle.

so start of draw()

```auto
void draw()
{
  background(277);

  if (hold && attachedToPaddle)
  {
    // ignore
  } else 
  {
    xspeed *= 1.001;

    xpos += xspeed;
    ypos += yspeed;
  }

```

AND the reflection (only left paddle)

```auto

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

```

**Remark**

“tela” is “screen” in portugese. You have this in draw().

It’s like state of a program, mostly referred to as _state_ in the forum

You are doing it wrong.

That’s the core idea of states:

```auto

  switch (tela) {
  case "MENU":
    menu();
    break;
  case "TUTORIAL":
    tutorial();
    break;
  }

```

You should have a state “GAME” as well and put your other draw() stuff into it

Then you can have a proper menu and game over screen

---

<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 24, 2019, 4:58pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/9 "2019-05-24T16:58:21Z")

</div>

and your hold() function should be killed

in my way of doing this, the advantage is  
that when player hits space bar again, the ball  
starts again with the old direction (that is stored in xspeed,  
so we want to keep the data. Instead we have this if-check

```auto
if (hold && attachedToPaddle)

```

at the beginning of draw())

Chrisir

**This is the hold function**

```auto
void hold() {
  if (hold) {
    if (xpos == playerpos1) {
       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 24, 2019, 5:25pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/10 "2019-05-24T17:25:45Z")

</div>

Thanks, i give it a shot.

---

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

</div>

ok i tried it out and it holds but the ball keeps teleporting past the paddle.

---

<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 24, 2019, 7:01pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/12 "2019-05-24T19:01:47Z")

</div>

the code is looks like this

```auto
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0, 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;
boolean move = true;
float easing = 0.05;
boolean attachpaddle;

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()
{
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  scores();
  hold();

  if (hold && attachpaddle)
  {
    // ignore
  } else 
  {
    xspeed *= 1.001;

    xpos += xspeed;
    ypos += yspeed;
  }

  switch (tela) {
  case "MENU":
    menu();
    break;
  case "TUTORIAL":
    tutorial();
    break;
  }

  if (ypos > height-size/2 || ypos < 0+size/2) {
    yspeed *= -1;
    hold = true;
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos1)/5;
    }
  }
  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));
    }
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      if ( ! (hold && attachpaddle)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos1)/5;
      }
      attachpaddle=true;
    }
  } else {
    attachpaddle=false;
  }

  ellipse(xpos, ypos, size, size);
  rect (p1x, playerpos1-50, 20, 100);
  rect (p2x, playerpos2-50, 20, 100);
}

void menu() {
  background(255, 0, 0);
}

void keyPressed() {
  if (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 (keyPressed) {
    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;
    }
  }
}

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*1.5;
    yspeed = yspeed*1.5;
  }
  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) {
      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 24, 2019, 8:57pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/13 "2019-05-24T20:57:01Z")

</div>

> [@somedude](#):
>
> boolean hold;

Before setup ()

boolean hold=false;  
boolean attachpaddle=false;

---

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

</div>

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

Did you forget to kill it?

---

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

</div>

yes, sorry i’ll do it

---

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

</div>

its still not working

```auto
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0, 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 move = true;
float easing = 0.05;
boolean attachpaddle = 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;
  hold();
}

void draw()
{
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  scores();

  if (hold && attachpaddle)
  {
    // ignore
  } else 
  {
    xspeed *= 1.001;

    xpos += xspeed;
    ypos += yspeed;
  }

  

  if (ypos > height-size/2 || ypos < 0+size/2) {
    yspeed *= -1;
    hold = true;
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos1)/5;
    }
  }
  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));
    }
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      if ( ! (hold && attachpaddle)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos1)/5;
      }
      attachpaddle=true;
    }
  } else {
    attachpaddle=false;
  }

  ellipse(xpos, ypos, size, size);
  rect (p1x, playerpos1-50, 20, 100);
  rect (p2x, playerpos2-50, 20, 100);
}

void menu() {
  background(255, 0, 0);
}

void keyPressed() {
  if (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 (keyPressed) {
    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;
    }
  }
}

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*1.5;
    yspeed = yspeed*1.5;
  }
  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) {
      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 24, 2019, 9:13pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/17 "2019-05-24T21:13:04Z")

</div>

> [@somedude](#):
>
> xspeed \*= 1.001; xpos += xspeed; ypos += yspeed;

Obviously kill this before the if-clause at the beginning of draw ()

It’s not allowed to execute when the if-clause is false

---

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

</div>

I had this in my post as well…

Anyway- when the ball is attached and you use wasd you might want to use the paddle together with the ball

---

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

</div>

ok the holding is working now, all thats left is throwijng the ball back

```auto
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0, 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 move = true;
float easing = 0.05;
boolean attachpaddle = 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;
  hold();
}

void draw()
{
  background(277);
  scores();

  if (hold && attachpaddle)
  {
    // ignore
  } else 
  {
    xspeed *= 1.001;

    xpos += xspeed;
    ypos += yspeed;
  }

  

  if (ypos > height-size/2 || ypos < 0+size/2) {
    yspeed *= -1;
    hold = true;
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos1)/5;
    }
  }
  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));
    }
  }

  if ( xpos <= 30+size/2) {
    if (ypos > playerpos1-50 && ypos < playerpos1+50) {
      if ( ! (hold && attachpaddle)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos1)/5;
      }
      attachpaddle=true;
    }
  } else {
    attachpaddle=false;
  }

  ellipse(xpos, ypos, size, size);
  rect (p1x, playerpos1-50, 20, 100);
  rect (p2x, playerpos2-50, 20, 100);
}

void menu() {
  background(255, 0, 0);
}

void keyPressed() {
  if (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 (keyPressed) {
    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;
    }
  }
}

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*1.5;
    yspeed = yspeed*1.5;
  }
  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) {
      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 24, 2019, 9:20pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/20 "2019-05-24T21:20:48Z")

</div>

The idea is set you click space bar briefly

Then the ball is attached

Click space bar again briefly - ball starts again

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