Skip to content

Instantly share code, notes, and snippets.

@robsbots
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robsbots/9509545 to your computer and use it in GitHub Desktop.
Save robsbots/9509545 to your computer and use it in GitHub Desktop.
Week 3 Code update
//This is run just before the game "scenario" is printed on the screen
@Override
protected void updateGame(float secondsElapsed) {
float distanceBetweenBallAndPaddle;
float distanceBetweenBallAndSmiley;
if (mBallSpeedY > 0){ // If the ball is moving down the screen do this block
// Removed ball speed calculations to after the "if ball hits paddle" code
// Only need to change the speed/direction if a hit is detected. Otherwise leave it alone
distanceBetweenBallAndPaddle = (mPaddleX - mBallX) * (mPaddleX - mBallX) + (mCanvasHeight - mBallY) * (mCanvasHeight - mBallY);
// Removed distanceBetweenBallAndSmiley and moved it down the code, just above where it is checked
if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndPaddle){
// Calculate current speed/direction here if a hit is detected.
float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
mBallSpeedX = mBallX - mPaddleX;
mBallSpeedY = mBallY - mCanvasHeight;
// Calculate new speed/direction here if a hit is detected.
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
}
} // Added this bracket. This is the end of the if (mBallSpeedY > 0) code. This is the end of the block of code thats run
// if the ball is heading down.
// This line of code below was inside the "if (mBallSpeedY > 0) bock of code above,
// so was only run if the ball was moving down the screen.
distanceBetweenBallAndSmiley = (mSmileyX - mBallX) * (mSmileyX - mBallX) + (mSmileyY - mBallY) * (mSmileyY - mBallY);
// Check if the Ball hits the smiley face.
if(mMinDistanceBetweenRedBallAndSmiley >= distanceBetweenBallAndSmiley){
// copied this from above.
// This is to calculate the current speed/direction if the ball hits the target (Smiley face)
float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
mBallSpeedX = mBallX - mSmileyX;
mBallSpeedY = mBallY - mSmileyY;
// copied this from above.
// This is to calculate the new speed/direction if the ball hits the target (Smiley face)
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
updateScore(1);
}
// Moved the closing curly bracket from here and placed it hight up as noted in the comments.
//Move the ball's X and Y using the speed (pixel/sec)
mBallX = mBallX + secondsElapsed * mBallSpeedX;
mBallY = mBallY + secondsElapsed * mBallSpeedY;
//Check for Ball location on screen.
if ((mBallX <= mBall.getWidth()/2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2 && mBallSpeedX > 0)) {
mBallSpeedX = -mBallSpeedX;
// mBallBounceCount = mBallBounceCount + 1;
}
if (mBallY <= mBall.getHeight()/2 && mBallSpeedY < 0){
mBallSpeedY = -mBallSpeedY;
// mBallBounceCount = mBallBounceCount + 1;
}
if (mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY > 0){
setState(GameThread.STATE_LOSE);
}
} // Added this bracket to end the block of code run if the ball hit's the target.
// This block was previously inside the "if (mBallSpeedY > 0)" block of code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment