// Update rightpaddle position based on directionif(mRightPaddleDir !=0){
mRightPaddlePos.y += mRightPaddleDir *300.0f* deltaTime;// Make sure paddle doesn't move off screen!if(mRightPaddlePos.y <(paddleH /2.0f+ thickness)){
mRightPaddlePos.y = paddleH /2.0f+ thickness;}elseif(mRightPaddlePos.y >(768.0f- paddleH /2.0f- thickness)){
mRightPaddlePos.y =768.0f- paddleH /2.0f- thickness;}}
更新ball碰到右paddle的行为
float rightDiff = mRightPaddlePos.y - mBallPos.y;// Take absolute value of difference
rightDiff =(rightDiff >0.0f)? rightDiff :-rightDiff ;if(// Our y-difference is small enough
rightDiff <= paddleH /2.0f&&// We are in the correct x-position
mBallPos.x >=999.0f&&// The ball is moving to the left
mBallVel.x >0.0f){
mBallVel.x *=-1.0f;}// Did the ball go off the screen? (if so, end game)elseif(mBallPos.x <=0.0f||mBallPos.x>=1024.0f){
mIsRunning =false;}
// Update balls position based on ball velocityfor(auto b =begin(Balls);b!=end(Balls);++b){
b->BallPos.x += b->BallVel.x*deltaTime;
b->BallPos.y += b->BallVel.y*deltaTime;// Bounce if needed// Did we intersect with the paddle?float diff = mPaddlePos.y - b->BallPos.y;// Take absolute value of difference
diff =(diff >0.0f)? diff :-diff;if(// Our y-difference is small enough
diff <= paddleH /2.0f&&// We are in the correct x-position
b->BallPos.x <=25.0f&& b->BallPos.x >=20.0f&&// The ball is moving to the left
b->BallVel.x <0.0f){
b->BallVel.x *=-1.0f;}float rightDiff = mRightPaddlePos.y - b->BallPos.y;// Take absolute value of difference
rightDiff =(rightDiff >0.0f)? rightDiff :-rightDiff;if(// Our y-difference is small enough
rightDiff <= paddleH /2.0f&&// We are in the correct x-position
b->BallPos.x >=999.0f&&// The ball is moving to the left
b->BallVel.x >0.0f){
b->BallVel.x *=-1.0f;}// Did the ball go off the screen? (if so, end game)elseif(b->BallPos.x <=0.0f|| b->BallPos.x >=(1024.0f-thickness)){
mIsRunning =false;}// Did the ball collide with the top wall?if(b->BallPos.y <= thickness && b->BallVel.y <0.0f){
b->BallVel.y *=-1;}// Did the ball collide with the bottofm wall?elseif(b->BallPos.y >=(768- thickness)&&
b->BallVel.y >0.0f){
b->BallVel.y *=-1;}}
处理多球的绘制
for(auto b =begin(Balls); b !=end(Balls);++b){// Draw ball
SDL_Rect ball{static_cast<int>(b->BallPos.x - thickness /2),static_cast<int>(b->BallPos.y - thickness /2),
thickness,
thickness
};SDL_RenderFillRect(mRenderer,&ball);}