Sign in to follow this  
phil67rpg

computer pong paddle

Recommended Posts

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;

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Edited by ISDCaptain01

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this