Holding the ball in pong

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


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

sorry forgot to post the whole code

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

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

how do i flag the paddles?

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

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

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

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

  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()

void draw()
{
  background(277);

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

    xpos += xspeed;
    ypos += yspeed;
  }

AND the reflection (only left paddle)


  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:


  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

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

if (hold && attachedToPaddle)

at the beginning of draw())

Chrisir

This is the hold function

void hold() {
  if (hold) {
    if (xpos == playerpos1) {
       xspeed = 0;
    }
  }
}

Thanks, i give it a shot.

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

the code is looks like this

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


Before setup ()

boolean hold=false;
boolean attachpaddle=false;

Did you forget to kill it?

yes, sorry i’ll do it

its still not working

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


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

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

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

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

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


The idea is set you click space bar briefly

Then the ball is attached

Click space bar again briefly - ball starts again