computer pong paddle

Started by
5 comments, last by phil67rpg 10 years, 8 months ago

well l able to get the computer controlled paddle in pong to move up the screen and stay there. I want it to move back down to the bottom of the screen and then back up again, basically I want the paddle to move up and down the screen on its own.

here is the code I am using.

if(computer_move >= screen_height)
{
computer_move-=0.1f;
}
else if(computer_move <= -screen_height)
{
computer_move+=0.1f;
}
computer_move+=0.1f;

Advertisement

float computerPaddleSpeed = 0.1f;
if (computer_move > screen_height) {
    computerPaddleSpeed *= -1;
    computer_move = screen_height;
}
if (computer_move < -screen_height) {
    computerPaddleSpeed *= -1;
    computer_move = -screen_height;
}

Something like that?

I'd go farther to make the computer's paddle track the ball. If the middle of the ball is under the middle of the paddle, move down. If the middle of the ball is above the middle of the paddle, move up. That way the middle of the paddle always tries to be at the same y-coordinate as the ball so the player has to be trickier with angles to beat the computer.

Basically make 2 functions that move the paddle at a certain speed


void movePaddleUP(Paddle &object)
{
     object.y -= 5;
}

void movePaddleDown(Paddle &object)
{
     object.xy += 5;
}

than call these function when you press the up or down key


void getinput()
{

    if(KEY_DOWN)
    {
       movePaddleDOwn(paddle);
    }
    if(KEY_UP)
    {
       movePaddleUp(paddle);
    }

       //check if it hits the screen
       if(paddle.y < SCREEN_HEIGHT)
         paddle.y = 0;
   I   if(paddle.y > SCREEN_HEIGHT - paddle.height)
          paddle.y = SCREEN_HEIGHT - paddle.height;
}

well I tried davids code but something is missing, the paddle still sticks to the bottom or top of the screen, I will work on it some more.

well I tried davids code but something is missing, the paddle still sticks to the bottom or top of the screen, I will work on it some more.

Just use my example and automate it so no keys have to be pressed for the paddle to move. remove <= and >= and change it to < and >. add an if statement telling the computer to move in the opposite direction once it reaches the position


add an if statement telling the computer to move in the opposite direction once it reaches the position

where should I put the if statement?

well I solved the problem. thanks for all the help.

This topic is closed to new replies.

Advertisement