if(ballBottom>ScreenHeight()){//If the ball touches the bottom edge, reverse its Y direction.
ballYSpd*=-1;
bally=ScreenHeight()-ballRadius;
}
if(//If the ball touches the top edge, reverse its Y direction.
ballTop<0
){
ballYSpd*=-1;
bally=ballRadius;
}
if(ballRight>ScreenWidth()){//If the ball touches the right edge, award a point to the player.
playerScore+=1;
ballx=ScreenWidth()/2;
bally=ScreenHeight()/2;
ballXSpd=80;
ballYSpd=80;
intrandomNumber=rand()%2;//Get a random number that is 0 or 1.
if(randomNumber==0){
ballXSpd*=-1;
}
randomNumber=rand()%2;
if(randomNumber==0){
ballYSpd*=-1;
}
}
if(ballLeft<0){//If the ball touches the left edge, award a point to the enemy.
enemyScore+=1;
ballx=ScreenWidth()/2;
bally=ScreenHeight()/2;
ballXSpd=80;
ballYSpd=80;
intrandomNumber=rand()%2;//Get a random number that is 0 or 1.
if(randomNumber==0){
ballXSpd*=-1;
}
randomNumber=rand()%2;
if(randomNumber==0){
ballYSpd*=-1;
}
}
if(GetKey(olc::UP).bHeld){
paddley-=120*fElapsedTime;//move the paddle up at 120 pixels / s
}
if(GetKey(olc::DOWN).bHeld){
paddley+=120*fElapsedTime;//move the paddle down at 120 pixels / s
}
if(bally<enemyPaddleY){
enemyPaddleY-=100*fElapsedTime;
}
if(bally>enemyPaddleY){
enemyPaddleY+=100*fElapsedTime;
}
if(playerScore>=5){
GameState=WIN_GAME;
}
if(enemyScore>=5){
GameState=LOSE_GAME;
}
DrawCircle(ballx,bally,ballRadius,olc::YELLOW);
FillRect(paddlex-paddleWidth/2,paddley-paddleHeight/2,paddleWidth,paddleHeight,olc::GREEN);//Draw a paddle centered at the paddle position.
FillRect(enemyPaddleX-enemyPaddleWidth/2,enemyPaddleY-enemyPaddleHeight/2,enemyPaddleWidth,enemyPaddleHeight,olc::DARK_RED);//Draw a paddle centered at the paddle position.