• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# 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.

6 replies to this topic

### #1phil67rpg  Members

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;


### #2David.M  Members

Posted 17 August 2013 - 06:48 PM

float computerPaddleSpeed = 0.1f;
if (computer_move > screen_height) {
computer_move = screen_height;
}
if (computer_move < -screen_height) {
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.

### #3ISDCaptain01  Members

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

{
object.xy += 5;
}


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

void getinput()
{

if(KEY_DOWN)
{
}
if(KEY_UP)
{
}

//check if it hits the screen
}


### #4phil67rpg  Members

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.

### #5ISDCaptain01  Members

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.

### #6phil67rpg  Members

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?

### #7phil67rpg  Members

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.