Holding the ball in pong

fehler and score(), also i gotta make the paddles emit a sound when the ball bounces off

for sound look at library sound

https://www.processing.org/reference/libraries/sound/index.html

I havenā€™t done the sound.

Remark

I changed quite a few things. Your draw() is now much leaner.

I added

boolean winplayer1=false; 
boolean winplayer2=false;

and used them throughout to stop the errors (fehler1 and fehler2) to be increased

also please check usage of fehler1 and fehler2 in println() and text() statements

Would be nice to have a tela (screen) for someoneHasOne

Chrisir


// ball data 
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0.6, 4);

float fehler1=0;
float fehler2=0;

// paddle data 
float playerpos1;
float p1x;
float playerpos2;
float p2x;

// screen / state 
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;

boolean winplayer1=false; 
boolean winplayer2=false;

// --------------------------------------------------------------------------
// two core functions  

void setup() {
  size(640, 400);
  noStroke();
  tela = "MENU";
  // frameRate(30);
  smooth();
  // paddles
  playerpos1 = height/2;
  playerpos2 = height/2;
  // ball 
  xpos = 45;
  ypos = playerpos1;

  p1x = 10;
  p2x = width-30;
}

void draw() {

  switch (tela) {
    // ------------------------------------
  case "GAME":
    gamePlay(); 
    break; 

    // ------------------------------------
  case "MENU":
    menu();
    break;

    // ------------------------------------
  case "TUTORIAL":
    tutorial();
    break;

    // ------------------------------------
  case"CRED":
    cred();
    break;
  }//switch
  //
}//draw

// --------------------------------------------------------------------------
// main functions called by draw() (the different screens)

void gamePlay() {
  background(0);

  if (! (winplayer1||winplayer2)) {
    if (hold && attachedToPaddle)
    {
      // ignore
    } else 
    {
      // move ball
      //xspeed *= 1.001;
      xspeed += 0.001;

      xpos += xspeed;
      ypos += yspeed;
    }
  }

  scores();
  // hold();
  if (AIon == true) {
    AI();
  }

  // bounce 
  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 (! (winplayer1||winplayer2)) {
    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(fehler1));
        winplayer2=true;
      }
    }
  }

  if ( xpos >= width-30-size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos2)/5;
    }
  }
  if (! (winplayer1||winplayer2)) {
    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 win 5:"+ str(fehler2));
        winplayer1=true;
      }
    }
  }

  // ball and paddles 
  fill(255, 0, 0);
  ellipse(xpos, ypos, size, size);
  fill(0, 255, 0);
  rect (p1x, playerpos1-50, 20, 100);
  fill(0, 0, 255);
  rect (p2x, playerpos2-50, 20, 100);

  // ---

  if (winplayer1) {
    textAlign(CENTER); 
    text( "player 1 win 5:"+ str(fehler1), 
      width/2, height/2);
    textAlign(LEFT);
  } else if (winplayer2) {
    textAlign(CENTER); 
    text( "player 2 win 5:"+ str(fehler2), 
      width/2, height/2);
    textAlign(LEFT);
  }
  // ---
}//func

void menu() {
  background(255, 0, 0);
  fill(0, 0, 0); 
  text ("MENU", 
    111, 111);
}

void tutorial() { // ????????????????????????????
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  //scores();
  //tela = "menu";  
  background(0, 255, 0);
  trocou = true;
}

void cred() {
  background(150, 150, 0);
}

// --------------------------------------------------------------------------
// Inputs

void keyPressed() {

  switch (tela) {
    // ------------------------------------
  case "GAME":

    if (key == 'e') {
      if (playerpos1 >=50)
        playerpos1 = playerpos1 - 8;
    } else if (key == 'd') {
      if (playerpos1 <=height-50)
        playerpos1 = playerpos1 + 8;
    } else if (hold && attachedToPaddle) {
      ypos = playerpos1+8;
    }

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

    else if (key == '9')
    {
      if (playerpos2 >=50)
        playerpos2 = playerpos2 - 8;
    } else if (key == '3') {
      if (playerpos2 <=height-50)
        playerpos2 = playerpos2 + 8;
    }
    /*if (key == '3') {
     xspeed = 0;
     yspeed = 0;
     move = false;
     }
     */
    else if (key == ' ') {
      hold = ! hold;
    } else if (key == ESC) {
      tela="MENU";
      key=0;
    } else if (key == 'a') {
      AIon = true;
    } else if (key == 67) {
      tela="CRED";
    }
    break; // end of case "GAME":

    // ------------------------------------
  case "MENU":
    //
    if (key == ESC) {
      key=0;
    }
    break; // end of case "MENU":

    // ------------------------------------
  case "TUTORIAL":
    //
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "TUTORIAL":

  case"CRED":
    // 
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "CRED":
  }//switch
}

void mousePressed() {
  switch (tela) {
    // ------------------------------------
  case "GAME":
    //
    break; 

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

    // ------------------------------------
  case "TUTORIAL":
    //
    break;

    // ------------------------------------
  case "CRED":
    //
    break;
  }//switch
}

// --------------------------------------------------------------------------
// Tools 

void scores() {
  //
  fill(255); // WHITE 
  text(scoreE, 100, 50);
  text(scoreD, width-100, 50);

  if (winplayer1||winplayer2) {
    return; // leave here
  }

  if (xpos > 640) {
    scoreE += 1;
    xspeed = xspeed+1.2; // was *2 
    yspeed = yspeed+1.2;
  }
  if (xpos < 0 ) {
    scoreD += 1;
    xspeed = xspeed+1.2; // was + 1.5
    yspeed = yspeed+1.2;
  }
}

void AI() {
  //
  // playerpos2 += (ypos-playerpos2) * easing;
  playerpos2 += (ypos-playerpos2) * .1;

  if (fehler1==5) {
    AIon = false;
  }

  if (fehler2 == 5) {
    AIon = false;
  }
}

void hold222222222222222() {  // ??????????????
  if (hold) {
    if (xpos == playerpos1) {
      //  xpos=xpos-8;
      //      xspeed = 0;
    }
  }
}
//

thanks, now to make the right paddle hold the ball i just copy the hold() function to it?

the hold() function is dead.

We only use the booleans hold and the other involved

but yeah you have to copy those and the logic with all the ifs working with them

1 Like

the move boolean is related to the movement of the ball or the paddle?

what does the key = 0 does?

it just kills ESC so we donā€™t leave the sketch but stay inside

I donā€™t know, I didnā€™t invent it

boolean hold=false;
boolean attachedToPaddle=false;

these are relevant for ball attaching to paddle

to make the hold function 2 i gotta copy all the lines that there is a hold involved or it must be a specific one?

the hold function is dead

for the hold functionality (ball attaching to paddle) just copy all where hold and attachedToPaddle is involved.

And adapt it, so it applies to the right paddle

1 Like

actually depending on your skill level you want to turn to object oriented programming

see tutorials on objects

https://www.processing.org/tutorials/objects/

it would allow you to have one object ball and 2 objects paddle

Remark

also some of your variables are English, some German, some Swedish

Please make all English

ok, sorry iā€™ll change it

1 Like

hey i tried doing as you said but i ran into some issue, can you help me out

// ball data 
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0.6, 4);

float P1Points=0;
float P2Points=0;

// paddle data 
float playerpos1;
float p1x;
float playerpos2;
float p2x;

// screen / state 
String tela;

boolean trocou;

int scoreE = 0;
int scoreD = 0;
// int scoreW = 10;

boolean hold=false;
boolean attachedToPaddle=false;
boolean move = true;

boolean hold2=false;
boolean attachedToPaddle2=false;

float easing = 0.50;

boolean AIon = false;

boolean winplayer1=false; 
boolean winplayer2=false;

// --------------------------------------------------------------------------
// two core functions  

void setup() {
  size(640, 400);
  noStroke();
  tela = "MENU";
  // frameRate(30);
  smooth();
  // paddles
  playerpos1 = height/2;
  playerpos2 = height/2;
  // ball 
  xpos = 45;
  ypos = playerpos1;

  p1x = 10;
  p2x = width-30;
}

void draw() {

  switch (tela) {
    // ------------------------------------
  case "GAME":
    gamePlay(); 
    break; 

    // ------------------------------------
  case "MENU":
    menu();
    break;

    // ------------------------------------
  case "TUTORIAL":
    tutorial();
    break;

    // ------------------------------------
  case"CRED":
    cred();
    break;
  }//switch
  //
}//draw

// --------------------------------------------------------------------------
// main functions called by draw() (the different screens)

void gamePlay() {
  background(0);

  if (! (winplayer1||winplayer2)) {
    if (hold && attachedToPaddle)
    {
      // ignore
    } else 
    {
      // move ball
      //xspeed *= 1.001;
      xspeed += 0.001;

      xpos += xspeed;
      ypos += yspeed;
    }
  }

  scores();
  // hold();
  if (AIon == true) {
    AI();
  }

  // bounce 
  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 >= 30+size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      if ( ! (hold && attachedToPaddle)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos1)/5;
      }
      attachedToPaddle2=true;
    }
  } else {
    attachedToPaddle2=false;
  }

  if (! (winplayer1||winplayer2)) {
    if (xpos <= 0) {
      P1Points += 1;
      if (P1Points <= 4) {
        xpos = 50;
        ypos = playerpos1;
        xspeed = random(5, 6);
        xspeed *= -1;
        yspeed = 0; 
        println("player 2: "+ str(P2Points)+":"+str(P1Points) );
      }
      if (P1Points == 5) {
        println("player 2 win 5:"+ str(P1Points));
        winplayer2=true;
      }
    }
  }

  if ( xpos >= width-30-size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos2)/5;
    }
  }
  if (! (winplayer1||winplayer2)) {
    if (xpos >= width) {
      P2Points +=1 ;
      if (P2Points <= 4) {
        xpos = width-50;
        ypos = playerpos2;
        xspeed = -random(5, 6);
        xspeed *= -1;
        yspeed = 0; 
        println("ponto: "+ str(P2Points)+":"+str(P1Points) );
        text(height/2, width/2, 50, 50);
      }
      if (P2Points ==5 ) {
        println("player 1 win 5:"+ str(P2Points));
        winplayer1=true;
      }
    }
  }

  // ball and paddles 
  fill(255, 0, 0);
  ellipse(xpos, ypos, size, size);
  fill(0, 255, 0);
  rect (p1x, playerpos1-50, 20, 100);
  fill(0, 0, 255);
  rect (p2x, playerpos2-50, 20, 100);

  // ---

  if (winplayer1) {
    textAlign(CENTER); 
    text( "player 1 win 5:"+ str(P1Points), 
      width/2, height/2);
    textAlign(LEFT);
  } else if (winplayer2) {
    textAlign(CENTER); 
    text( "player 2 win 5:"+ str(P2Points), 
      width/2, height/2);
    textAlign(LEFT);
  }
  // ---
}//func

void menu() {
  background(255, 0, 0);
  fill(0, 0, 0); 
  text ("MENU", 
    111, 111);
}

void tutorial() { // ????????????????????????????
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  //scores();
  //tela = "menu";  
  background(0, 255, 0);
  trocou = true;
}

void cred() {
  background(150, 150, 0);
}

// --------------------------------------------------------------------------
// Inputs

void keyPressed() {

  switch (tela) {
    // ------------------------------------
  case "GAME":

    if (key == 'e') {
      if (playerpos1 >=50)
        playerpos1 = playerpos1 - 8;
    } else if (key == 'd') {
      if (playerpos1 <=height-50)
        playerpos1 = playerpos1 + 8;
    } else if (hold && attachedToPaddle) {
      ypos = playerpos1+8;
    }

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

    else if (key == '9')
    {
      if (playerpos2 >=50)
        playerpos2 = playerpos2 - 8;
    } else if (key == '3') {
      if (playerpos2 <=height-50)
        playerpos2 = playerpos2 + 8;
    } else if(hold2 && attachedToPaddle2){
     ypos = playerpos2+8; 
    }
    /*if (key == '3') {
     xspeed = 0;
     yspeed = 0;
     move = false;
     }
     */
    else if (key == ' ') {
      hold = ! hold;
    } else if (key == ESC) {
      tela="MENU";
      key=0;
    } else if (key == 'a') {
      AIon = true;
    } else if (key == 67) {
      tela="CRED";
    } else if (key == '5'){
     hold2 = !hold2; 
    }
    break; // end of case "GAME":

    // ------------------------------------
  case "MENU":
    //
    if (key == ESC) {
      key=0;
    }
    break; // end of case "MENU":

    // ------------------------------------
  case "TUTORIAL":
    //
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "TUTORIAL":

  case"CRED":
    // 
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "CRED":
  }//switch
}

void mousePressed() {
  switch (tela) {
    // ------------------------------------
  case "GAME":
    //
    break; 

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

    // ------------------------------------
  case "TUTORIAL":
    //
    break;

    // ------------------------------------
  case "CRED":
    //
    break;
  }//switch
}

// --------------------------------------------------------------------------
// Tools 

void scores() {
  //
  fill(255); // WHITE 
  text(scoreE, 100, 50);
  text(scoreD, width-100, 50);

  if (winplayer1||winplayer2) {
    return; // leave here
  }

  if (xpos > 640) {
    scoreE += 1;
    xspeed = xspeed+1.2; // was *2 
    yspeed = yspeed+1.2;
  }
  if (xpos < 0 ) {
    scoreD += 1;
    xspeed = xspeed+1.2; // was + 1.5
    yspeed = yspeed+1.2;
  }
}

void AI() {
  //
  // playerpos2 += (ypos-playerpos2) * easing;
  playerpos2 += (ypos-playerpos2) * .1;

  if (P1Points==5) {
    AIon = false;
  }

  if (P2Points == 5) {
    AIon = false;
  }
}

void hold2() {  // ??????????????
  if (hold) {
    if (xpos == playerpos1) {
      //  xpos=xpos-8;
      //      xspeed = 0;
    }
  }
}
//

here you need hold 2 and ```
attachedToPaddle2


and also ```
playerpos2 
``` instead of playerpos1

its still not working

// ball data 
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0.6, 4);

float P1Points=0;
float P2Points=0;

// paddle data 
float playerpos1;
float p1x;
float playerpos2;
float p2x;

// screen / state 
String tela;

boolean trocou;

int scoreE = 0;
int scoreD = 0;
// int scoreW = 10;

boolean hold=false;
boolean attachedToPaddle=false;
boolean move = true;

boolean hold2=false;
boolean attachedToPaddle2=false;

float easing = 0.50;

boolean AIon = false;

boolean winplayer1=false; 
boolean winplayer2=false;

// --------------------------------------------------------------------------
// two core functions  

void setup() {
  size(640, 400);
  noStroke();
  tela = "MENU";
  // frameRate(30);
  smooth();
  // paddles
  playerpos1 = height/2;
  playerpos2 = height/2;
  // ball 
  xpos = 45;
  ypos = playerpos1;

  p1x = 10;
  p2x = width-30;
}

void draw() {

  switch (tela) {
    // ------------------------------------
  case "GAME":
    gamePlay(); 
    break; 

    // ------------------------------------
  case "MENU":
    menu();
    break;

    // ------------------------------------
  case "TUTORIAL":
    tutorial();
    break;

    // ------------------------------------
  case"CRED":
    cred();
    break;
  }//switch
  //
}//draw

// --------------------------------------------------------------------------
// main functions called by draw() (the different screens)

void gamePlay() {
  background(0);

  if (! (winplayer1||winplayer2)) {
    if (hold && attachedToPaddle)
    {
      // ignore
    } else 
    {
      // move ball
      //xspeed *= 1.001;
      xspeed += 0.001;

      xpos += xspeed;
      ypos += yspeed;
    }
  }

  scores();
  // hold();
  if (AIon == true) {
    AI();
  }

  // bounce 
  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 >= 30+size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      if ( ! (hold2 && attachedToPaddle2)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos2)/5;
      }
      attachedToPaddle2=true;
    }
  } else {
    attachedToPaddle2=false;
  }

  if (! (winplayer1||winplayer2)) {
    if (xpos <= 0) {
      P1Points += 1;
      if (P1Points <= 4) {
        xpos = 50;
        ypos = playerpos1;
        xspeed = random(5, 6);
        xspeed *= -1;
        yspeed = 0; 
        println("player 2: "+ str(P2Points)+":"+str(P1Points) );
      }
      if (P1Points == 5) {
        println("player 2 win 5:"+ str(P1Points));
        winplayer2=true;
      }
    }
  }

  if ( xpos >= width-30-size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos2)/5;
    }
  }
  if (! (winplayer1||winplayer2)) {
    if (xpos >= width) {
      P2Points +=1 ;
      if (P2Points <= 4) {
        xpos = width-50;
        ypos = playerpos2;
        xspeed = -random(5, 6);
        xspeed *= -1;
        yspeed = 0; 
        println("ponto: "+ str(P2Points)+":"+str(P1Points) );
        text(height/2, width/2, 50, 50);
      }
      if (P2Points ==5 ) {
        println("player 1 win 5:"+ str(P2Points));
        winplayer1=true;
      }
    }
  }

  // ball and paddles 
  fill(255, 0, 0);
  ellipse(xpos, ypos, size, size);
  fill(0, 255, 0);
  rect (p1x, playerpos1-50, 20, 100);
  fill(0, 0, 255);
  rect (p2x, playerpos2-50, 20, 100);

  // ---

  if (winplayer1) {
    textAlign(CENTER); 
    text( "player 1 win 5:"+ str(P1Points), 
      width/2, height/2);
    textAlign(LEFT);
  } else if (winplayer2) {
    textAlign(CENTER); 
    text( "player 2 win 5:"+ str(P2Points), 
      width/2, height/2);
    textAlign(LEFT);
  }
  // ---
}//func

void menu() {
  background(255, 0, 0);
  fill(0, 0, 0); 
  text ("MENU", 
    111, 111);
}

void tutorial() { // ????????????????????????????
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  //scores();
  //tela = "menu";  
  background(0, 255, 0);
  trocou = true;
}

void cred() {
  background(150, 150, 0);
}

// --------------------------------------------------------------------------
// Inputs

void keyPressed() {

  switch (tela) {
    // ------------------------------------
  case "GAME":

    if (key == 'e') {
      if (playerpos1 >=50)
        playerpos1 = playerpos1 - 8;
    } else if (key == 'd') {
      if (playerpos1 <=height-50)
        playerpos1 = playerpos1 + 8;
    } else if (hold && attachedToPaddle) {
      ypos = playerpos1+8;
    }

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

    else if (key == '9')
    {
      if (playerpos2 >=50)
        playerpos2 = playerpos2 - 8;
    } else if (key == '3') {
      if (playerpos2 <=height-50)
        playerpos2 = playerpos2 + 8;
    } else if(hold2 && attachedToPaddle2){
     ypos = playerpos2-8; 
    }
    /*if (key == '3') {
     xspeed = 0;
     yspeed = 0;
     move = false;
     }
     */
    else if (key == ' ') {
      hold = ! hold;
    } else if (key == ESC) {
      tela="MENU";
      key=0;
    } else if (key == 'a') {
      AIon = true;
    } else if (key == 67) {
      tela="CRED";
    } else if (key == '5'){
     hold2 = !hold2; 
    }
    break; // end of case "GAME":

    // ------------------------------------
  case "MENU":
    //
    if (key == ESC) {
      key=0;
    }
    break; // end of case "MENU":

    // ------------------------------------
  case "TUTORIAL":
    //
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "TUTORIAL":

  case"CRED":
    // 
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "CRED":
  }//switch
}

void mousePressed() {
  switch (tela) {
    // ------------------------------------
  case "GAME":
    //
    break; 

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

    // ------------------------------------
  case "TUTORIAL":
    //
    break;

    // ------------------------------------
  case "CRED":
    //
    break;
  }//switch
}

// --------------------------------------------------------------------------
// Tools 

void scores() {
  //
  fill(255); // WHITE 
  text(scoreE, 100, 50);
  text(scoreD, width-100, 50);

  if (winplayer1||winplayer2) {
    return; // leave here
  }

  if (xpos > 640) {
    scoreE += 1;
    xspeed = xspeed+1.2; // was *2 
    yspeed = yspeed+1.2;
  }
  if (xpos < 0 ) {
    scoreD += 1;
    xspeed = xspeed+1.2; // was + 1.5
    yspeed = yspeed+1.2;
  }
}

void AI() {
  //
  // playerpos2 += (ypos-playerpos2) * easing;
  playerpos2 += (ypos-playerpos2) * .1;

  if (P1Points==5) {
    AIon = false;
  }

  if (P2Points == 5) {
    AIon = false;
  }
}

void hold2() {  // ??????????????
  if (hold) {
    if (xpos == playerpos1) {
      //  xpos=xpos-8;
      //      xspeed = 0;
    }
  }
}
//
  if (! (winplayer1||winplayer2)) {
    if (hold && attachedToPaddle)
    {
      // ignore
    } else 
    {
      // move ball
      //xspeed *= 1.001;
      xspeed += 0.001;

      xpos += xspeed;
      ypos += yspeed;
    }
  }

in this section hold2 && attachedToPaddle2 are missing

so i add if(hold2 && attachedToPaddle2){
}
?

no.

if ( (hold && attachedToPaddle) || (hold2 && attachedToPaddle2) ) 

i tried that, adn the ball stopped moving, it just sticks to the left paddle

// ball data 
int size = 20;    
float xpos, ypos;    
float xspeed = random(5, 6);
float yspeed = random(0.6, 4);

float P1Points=0;
float P2Points=0;

// paddle data 
float playerpos1;
float p1x;
float playerpos2;
float p2x;

// screen / state 
String tela;

boolean trocou;

int scoreE = 0;
int scoreD = 0;
// int scoreW = 10;

boolean hold=false;
boolean attachedToPaddle=false;
boolean move = true;

boolean hold2=false;
boolean attachedToPaddle2=false;

float easing = 0.50;

boolean AIon = false;

boolean winplayer1=false; 
boolean winplayer2=false;

// --------------------------------------------------------------------------
// two core functions  

void setup() {
  size(640, 400);
  noStroke();
  tela = "MENU";
  // frameRate(30);
  smooth();
  // paddles
  playerpos1 = height/2;
  playerpos2 = height/2;
  // ball 
  xpos = 45;
  ypos = playerpos1;

  p1x = 10;
  p2x = width-30;
}

void draw() {

  switch (tela) {
    // ------------------------------------
  case "GAME":
    gamePlay(); 
    break; 

    // ------------------------------------
  case "MENU":
    menu();
    break;

    // ------------------------------------
  case "TUTORIAL":
    tutorial();
    break;

    // ------------------------------------
  case"CRED":
    cred();
    break;
  }//switch
  //
}//draw

// --------------------------------------------------------------------------
// main functions called by draw() (the different screens)

void gamePlay() {
  background(0);

  if (!( (hold && attachedToPaddle) || (hold2 && attachedToPaddle2) ))  {
    if (hold && attachedToPaddle)
    {
      // ignore
    } else 
    {
      // move ball
      //xspeed *= 1.001;
      xspeed += 0.001;

      xpos += xspeed;
      ypos += yspeed;
    }
  }

  scores();
  // hold();
  if (AIon == true) {
    AI();
  }

  // bounce 
  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 >= 30+size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      if ( ! (hold2 && attachedToPaddle2)) {
        xspeed *= -1;
        yspeed += (ypos-playerpos2)/5;
      }
      attachedToPaddle2=true;
    }
  } else {
    attachedToPaddle2=false;
  }

  if (! (winplayer1||winplayer2)) {
    if (xpos <= 0) {
      P1Points += 1;
      if (P1Points <= 4) {
        xpos = 50;
        ypos = playerpos1;
        xspeed = random(5, 6);
        xspeed *= -1;
        yspeed = 0; 
        println("player 2: "+ str(P2Points)+":"+str(P1Points) );
      }
      if (P1Points == 5) {
        println("player 2 win 5:"+ str(P1Points));
        winplayer2=true;
      }
    }
  }

  if ( xpos >= width-30-size/2) {
    if (ypos > playerpos2-50 && ypos < playerpos2+50) {
      xspeed *= -1;
      yspeed += (ypos-playerpos2)/5;
    }
  }
  if (! (winplayer1||winplayer2)) {
    if (xpos >= width) {
      P2Points +=1 ;
      if (P2Points <= 4) {
        xpos = width-50;
        ypos = playerpos2;
        xspeed = -random(5, 6);
        xspeed *= -1;
        yspeed = 0; 
        println("ponto: "+ str(P2Points)+":"+str(P1Points) );
        text(height/2, width/2, 50, 50);
      }
      if (P2Points ==5 ) {
        println("player 1 win 5:"+ str(P2Points));
        winplayer1=true;
      }
    }
  }

  // ball and paddles 
  fill(255, 0, 0);
  ellipse(xpos, ypos, size, size);
  fill(0, 255, 0);
  rect (p1x, playerpos1-50, 20, 100);
  fill(0, 0, 255);
  rect (p2x, playerpos2-50, 20, 100);

  // ---

  if (winplayer1) {
    textAlign(CENTER); 
    text( "player 1 win 5:"+ str(P1Points), 
      width/2, height/2);
    textAlign(LEFT);
  } else if (winplayer2) {
    textAlign(CENTER); 
    text( "player 2 win 5:"+ str(P2Points), 
      width/2, height/2);
    textAlign(LEFT);
  }
  // ---
}//func

void menu() {
  background(255, 0, 0);
  fill(0, 0, 0); 
  text ("MENU", 
    111, 111);
}

void tutorial() { // ????????????????????????????
  background(277);
  xspeed *= 1.001;
  xpos += xspeed;
  ypos += yspeed;
  //scores();
  //tela = "menu";  
  background(0, 255, 0);
  trocou = true;
}

void cred() {
  background(150, 150, 0);
}

// --------------------------------------------------------------------------
// Inputs

void keyPressed() {

  switch (tela) {
    // ------------------------------------
  case "GAME":

    if (key == 'e') {
      if (playerpos1 >=50)
        playerpos1 = playerpos1 - 8;
    } else if (key == 'd') {
      if (playerpos1 <=height-50)
        playerpos1 = playerpos1 + 8;
    } else if (hold && attachedToPaddle) {
      ypos = playerpos1+8;
    }

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

    else if (key == '9')
    {
      if (playerpos2 >=50)
        playerpos2 = playerpos2 - 8;
    } else if (key == '3') {
      if (playerpos2 <=height-50)
        playerpos2 = playerpos2 + 8;
    } else if(hold2 && attachedToPaddle2){
     ypos = playerpos2-8; 
    }
    /*if (key == '3') {
     xspeed = 0;
     yspeed = 0;
     move = false;
     }
     */
    else if (key == ' ') {
      hold = ! hold;
    } else if (key == ESC) {
      tela="MENU";
      key=0;
    } else if (key == 'a') {
      AIon = true;
    } else if (key == 67) {
      tela="CRED";
    } else if (key == '5'){
     hold2 = !hold2; 
    }
    break; // end of case "GAME":

    // ------------------------------------
  case "MENU":
    //
    if (key == ESC) {
      key=0;
    }
    break; // end of case "MENU":

    // ------------------------------------
  case "TUTORIAL":
    //
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "TUTORIAL":

  case"CRED":
    // 
    if (key == ESC) {
      tela="MENU";
      key=0;
    }
    break; // end of case "CRED":
  }//switch
}

void mousePressed() {
  switch (tela) {
    // ------------------------------------
  case "GAME":
    //
    break; 

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

    // ------------------------------------
  case "TUTORIAL":
    //
    break;

    // ------------------------------------
  case "CRED":
    //
    break;
  }//switch
}

// --------------------------------------------------------------------------
// Tools 

void scores() {
  //
  fill(255); // WHITE 
  text(scoreE, 100, 50);
  text(scoreD, width-100, 50);

  if (winplayer1||winplayer2) {
    return; // leave here
  }

  if (xpos > 640) {
    scoreE += 1;
    xspeed = xspeed+1.2; // was *2 
    yspeed = yspeed+1.2;
  }
  if (xpos < 0 ) {
    scoreD += 1;
    xspeed = xspeed+1.2; // was + 1.5
    yspeed = yspeed+1.2;
  }
}

void AI() {
  //
  // playerpos2 += (ypos-playerpos2) * easing;
  playerpos2 += (ypos-playerpos2) * .1;

  if (P1Points==5) {
    AIon = false;
  }

  if (P2Points == 5) {
    AIon = false;
  }
}

void hold2() {  // ??????????????
  if (hold) {
    if (xpos == playerpos1) {
      //  xpos=xpos-8;
      //      xspeed = 0;
    }
  }
}
//

you replaced the wrong lineā€¦

listen, I canā€™t help you here.

You are doing so many micro mistakes because you just donā€™t read carefully.

Pactice more. Concentrate more.