Jump to content

  • Log In with Google      Sign In   
  • Create Account


computer pong paddle


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 17 August 2013 - 06:29 PM

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;

 



Sponsor:

#2 David.M   Members   -  Reputation: 731

Like
0Likes
Like

Posted 17 August 2013 - 06:48 PM

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.



#3 ISDCaptain01   Members   -  Reputation: 1359

Like
0Likes
Like

Posted 17 August 2013 - 07:38 PM

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


#4 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 17 August 2013 - 07:58 PM

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.



#5 ISDCaptain01   Members   -  Reputation: 1359

Like
0Likes
Like

Posted 17 August 2013 - 08:23 PM

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, 17 August 2013 - 08:24 PM.


#6 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 17 August 2013 - 08:54 PM


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?



#7 phil67rpg   Members   -  Reputation: 767

Like
0Likes
Like

Posted 17 August 2013 - 09:22 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS