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

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

</div>

ok but now i got a bug on it the ball flies back to the paddle and doesnt return

---

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

</div>

Search for the error and fix it

show your code to get help

---

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

</div>

here

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

</div>

```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 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()
{
  background(277);

  if (hold && attachedToPaddle)
  {
    // ignore
  } else 
  {
    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) {
      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);
}

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

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

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

void keyReleased() {
  //hold = false;
  //attachedToPaddle=false;
}

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

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:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 25, 2019, 5:42pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/25 "2019-05-25T17:42:22Z")

</div>

> [@Chrisir](#):
>
> 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 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() { background(277); if (hold && attachedToPaddle) { // ignore } else { 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) { 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); } void menu() { background(255, 0, 0); } // -------------------------------------------------------------------------- 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; } } void keyReleased() { //hold = false; //attachedToPaddle=false; } // -------------------------------------------------------------------------- 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 = xspeed1.5; yspeed = yspeed1.5; } if (xpos \< 0 ) { scoreD += 1; xspeed = xspeed1.5; yspeed = yspeed_1.5; } } void AI() { playerpos2 = xpos \* easing; } void hold() { if (hold) { if (xpos == playerpos1) { // xspeed = 0; } } } //

It works. Thanks man you’re a saint, was having a lot of trouble with that code.

---

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

</div>

you can still make it so, when the ball is attached to the paddle and the paddle is moved, the ball would move together with 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 25, 2019, 5:44pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/27 "2019-05-25T17:44:54Z")

</div>

i’ll give it a try, all thats left is make it work to the other 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 25, 2019, 7:20pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/28 "2019-05-25T19:20:30Z")

</div>

and a way to turn off the game and make a title screen instructions and credits

---

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

</div>

I was commenting on menu already (it’s the tela variable)

in this sketch:

click mouse in menu and click **ESC** during game

(similar to function menu() put the game stuff from draw into a new function `playGame()`, so your draw() is nice and lean)

```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 ("MENUE", 
    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;
  }
}

void keyReleased() {
  //hold = false;
  //attachedToPaddle=false;
}

void mousePressed() {

  switch (tela) {

    // ------------------------------------
  case "GAME":
    break; 

    // ------------------------------------
  case "MENU":
    tela="GAME"; 
    break;

    // ------------------------------------
  case "TUTORIAL":
    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*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:** ![somedude](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@somedude](https://discourse.processing.org/u/somedude)\
**Post date:** [May 25, 2019, 7:49pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/30 "2019-05-25T19:49:23Z")

</div>

> [@Chrisir](#):
>
> 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 (“MENUE”, 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; } } void keyReleased() { //hold = false; //attachedToPaddle=false; } void mousePressed() { switch (tela) { // ------------------------------------ case “GAME”: break; // ------------------------------------ case “MENU”: tela=“GAME”; break; // ------------------------------------ case “TUTORIAL”: 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 = xspeed1.5; yspeed = yspeed1.5; } if (xpos \< 0 ) { scoreD += 1; xspeed = xspeed1.5; yspeed = yspeed_1.5; } } void AI() { playerpos2 = xpos \* easing; } void hold() { if (hold) { if (xpos == playerpos1) { // xspeed = 0; } } } //

thanks, any problem i got i’ll come back to you

---

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

</div>

oh and quick question how do i make the ball follow the paddle when attached

---

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

</div>

In keypressed when it’s attached just add to the ball like you do 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 25, 2019, 8:04pm UTC](https://discourse.processing.org/t/holding-the-ball-in-pong/11520/33 "2019-05-25T20:04:13Z")

</div>

so xpos = player1pos?

---

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

</div>

xpos=xpos-8;

I think

---

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

</div>

i put it in the ’ ’ keycode under the hold = !hold?

---

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

</div>

i think the ball not following the padle is a blessing in disguise

---

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

</div>

our goal here is:

ball is already attached

we move the paddle

ball moves with the paddle

which keys are involved?

---

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

</div>

> [@somedude](#):
>
> i think the ball not following the padle is a blessing in disguise

No. Or as you like it.

when the attached ball is moving with paddle it looks better.

And you can decide where you throw the ball by moving the ball prior to sending it again

---

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

</div>

i had an idea  
scrap right paddle holding the ball make it so only the left one holds and and put a breakout like like behind the right paddle making it a mix between pong and breakout.

---

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

</div>

the left paddle is an attacker while the right one is a defender

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

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