MOOC Archive

flmobimooc – week 5

I have added sound to the game. You can see the method used here.

I have also added lives, and the ball speeds up when ever you score.

To add lives I first declare a variable mLives. I then set mLives to 3 in setupBeginning()

To show the remaining lives, I use a for loop in doDraw and place a red ball in the top left corner of the screen in a row, 20 pixels from the top and spaced 30 pixels apart (with an offset of 5 pixels from the left)

In game update, instead of losing the game, I remove one life every time you miss the ball. I then check if the number of lives is 0. If it is, I use the same old setState(GameThread.STATE_PAUSE); code.If we still have some lives left, I set the state of the game to Paused. I then place the ball in the centre of the screen and reset the speed to the same as the initial speed when the game starts.

To increase the ball speed is very easy.

When you check to see if the ball has hit the smiley face in the updateGame method, add the following code to increase the ball speed 5%.

//using the fraction between the original velocity and present velocity to calculate the needed
//speeds in X and Y to get the original velocity but with the new angle.
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;

// Code added to speed up the ball.
// These are the only 2 lines required. Everything else is standard
// Speed up ball by 5% on every score
mBallSpeedX = mBallSpeedX * 1.05f;
mBallSpeedY = mBallSpeedY * 1.05f;

//Increase score

Take a look at my current file with all the updates so far. I have tried to add comments to explain what everything is doing.

Remember, if you want to add sound, follow the instructions on this page and download the file with the sounds from there, or add your own.

Files for week 5

These are the only files that differ from the originals supplied by Karsten. I have changed various parts for different features. Everthing should be commented. Right click to save the files or take a look at the Github Gists.

Please comment below if I have missed anything, or get it wrong.


Powered by WordPress 4.9.4 | Theme MxS2