Holding the ball in pong

something i noticed on the last version,of the code you posted and the ball doesnt seem to leave the paddle when the space bar is pressed again

oh yeah and the ball isnt following the paddle anymore

You‘ll solve this eventually

ok i fixed the space bar issue, i just had to switch the else if to just if

1 Like

ok i fixed both issues

1 Like

what did you meant on the line hold = !hold, why not hold + true?

this means:

hold = 
    ! hold;

The ! means not so, it makes hold become its opposite:

when it’s false, it becomes true and vice versa

so it’s toggle

so for the hold2 if just copy this replacing it with hold2 and attachedToPaddle2?

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

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

same thing with theese?

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 (hold && attachedToPaddle) {
      ypos = -8;
    }

You should try to understand the purpose of these lines and then implement the changes

so this one stops the ball when a player wins?

the if (! (winplayer1||winplayer2)) ?

} if (hold && attachedToPaddle) {
ypos = -8;
} this one makes the ball follow the paddle

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

holds the ball?

There are several things going on. This line says don’t execute when a player has won

1 Like

That’s right, ball follows paddle when it’s attached

1 Like

so by the logic this one

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

holds the ball

No.

it changes the direction of the ball on the paddle but only the first time when we have an attached situation

Then we set attached to true

Might help to hit ctrl-t and check the indents

so i copy that one and change it acordingly

im trying to write a function for the right paddle to hold, i still dont understand how the attache to paddle fdunction works