/** Reset ball to an initial state */ private void serveBall() { mBall.x = getWidth() / 2; mBall.y = getHeight() / 2; mBall.speed = Ball.SPEED + mBallSpeedModifier; mBall.randomAngle(); mBall.pause(); }
/** * All of the game's logic (per game iteration) is in this function. Given some initial game * state, it computes the next game state. */ private void doGameLogic() { float px = mBall.x; float py = mBall.y; mBall.move(); // Shake it up if it appears to not be moving vertically if (py == mBall.y && mBall.serving() == false) { mBall.randomAngle(); } // Do some basic paddle AI if (!mRed.player) doAI(mRed, mBlue); else mRed.move(); if (!mBlue.player) doAI(mBlue, mRed); else mBlue.move(); handleBounces(px, py); // See if all is lost if (mBall.y >= getHeight()) { mNewRound = true; mBlue.loseLife(); if (mBlue.living()) playSound(mMissSFX); else playSound(mWinSFX); } else if (mBall.y <= 0) { mNewRound = true; mRed.loseLife(); if (mRed.living()) playSound(mMissSFX); else playSound(mWinSFX); } }