Last active
August 29, 2015 13:57
-
-
Save robsbots/9611539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
################################################################################################ | |
# #FLMobiGame FutureLearn Begin Programming # | |
# End of week 4 code # | |
# # | |
# Sound added with the use of the SoundPool library # | |
################################################################################################ | |
*/ | |
package uk.ac.reading.sis05kol.mooc; | |
//Other parts of the android libraries that we use | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.view.MotionEvent; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// imports used for sound | |
import android.media.AudioManager; | |
import android.media.SoundPool; | |
public class TheGame extends GameThread{ | |
//Will store the image of a ball | |
private Bitmap mBall; | |
//Will store the image of the paddle | |
private Bitmap mPaddle; | |
//Will store the image of the smiley face | |
private Bitmap mSmiley; | |
// Sad face bitmap image | |
private Bitmap mSadBall; | |
private float[] mSadBallX = {-100,-100,-100}; | |
private float[] mSadBallY = new float[3]; | |
//The distance between red ball and paddle | |
private float mMinDistanceBetweenRedBallAndBigBall = 0; | |
private float mMinDistanceBetweenRedBallAndSmiley = 0; | |
//The X location of Paddle | |
private float mPaddleX = 0; | |
//The X and Y location of Smiley (bumper) | |
private float mSmileyX = 0; | |
private float mSmileyY = 0; | |
//The X and Y position of the ball on the screen (middle of ball) | |
private float mBallX = 0; | |
private float mBallY = 0; | |
//The speed (pixel/second) of the ball in direction X and Y | |
private float mBallSpeedX = 0; | |
private float mBallSpeedY = 0; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
//Create a Soundpool object and load each sound for the game | |
private SoundPool mSoundPool; | |
private int mSoundBounce; | |
private int mSoundLose; | |
private int mSoundScore; | |
private int mSoundPaddle; | |
private int mSoundNewgame; | |
//This is run before anything else, so we can prepare things here | |
public TheGame(GameView gameView) { | |
//House keeping | |
super(gameView); | |
//TheGame.context = getApplicationContext(); | |
//Prepare the image so we can draw it on the screen (using a canvas) | |
mBall = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.small_red_ball); | |
mPaddle = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.yellow_ball); | |
mSmiley = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.smiley_ball); | |
mSadBall = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.sad_ball); | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Create sound pool object with 4 channels. | |
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); | |
// The 4 channels means you can play at most 4 sounds at once, so if 1 sounds overlaps another it will still play | |
// but not cut off the first sound | |
// Load sound clip for each sound event | |
mSoundBounce = mSoundPool.load(mContext, R.raw.click, 1); | |
mSoundLose = mSoundPool.load(mContext, R.raw.sadtrombone, 1); | |
mSoundScore = mSoundPool.load(mContext, R.raw.score, 1); | |
mSoundPaddle = mSoundPool.load(mContext, R.raw.bounce, 1); | |
mSoundNewgame = mSoundPool.load(mContext, R.raw.start, 1); | |
} | |
//This is run before a new game (also after an old game) | |
@Override | |
public void setupBeginning() { | |
//Initialise speeds | |
mBallSpeedX = 200; | |
mBallSpeedY = 200; | |
//Place the ball in the middle of the screen. | |
mBallX = mCanvasWidth / 2; | |
mBallY = mCanvasHeight / 2; | |
//Place the Paddle in the middle of the screen | |
mPaddleX = mCanvasWidth /2; | |
//Place the Smiley (bouncer) near top of screen | |
mSmileyX = mCanvasWidth /2; | |
mSmileyY = mCanvasHeight / 15; | |
// Calculate min distance between ball and paddle to register a hit | |
mMinDistanceBetweenRedBallAndBigBall = (mPaddle.getWidth()/2 + mBall.getWidth()/2) * (mPaddle.getWidth()/2 + mBall.getWidth()/2); | |
// Calculate min distance between ball and target to register a hit | |
mMinDistanceBetweenRedBallAndSmiley = (mSmiley.getWidth()/2 + mBall.getWidth()/2) * (mSmiley.getWidth()/2 + mBall.getWidth()/2); | |
mSadBallX[0] = mCanvasWidth / 3; | |
mSadBallY[0] = mCanvasHeight / 3; | |
mSadBallX[1] = mCanvasWidth - mCanvasWidth / 3; | |
mSadBallY[1] = mCanvasHeight / 3; | |
mSadBallX[2] = mCanvasWidth / 2; | |
mSadBallY[2] = mCanvasHeight / 5; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play a sound when the game starts | |
mSoundPool.play(mSoundNewgame, 100, 100, 1, 0, 1.0f); | |
} | |
@Override | |
protected void doDraw(Canvas canvas) { | |
//If there isn't a canvas to draw on do nothing | |
//It is ok not understanding what is happening here | |
if(canvas == null) return; | |
super.doDraw(canvas); | |
//draw the image of the ball | |
canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null); | |
//draw the image of the paddle | |
canvas.drawBitmap(mPaddle, mPaddleX - mPaddle.getWidth()/2, mCanvasHeight - mPaddle.getHeight()/2, null); | |
//draw the image of the target | |
canvas.drawBitmap(mSmiley, mSmileyX - mSmiley.getWidth()/2, mSmileyY - mSmiley.getHeight()/2, null); | |
for(int i = 0; i < mSadBallX.length; i++) { | |
canvas.drawBitmap(mSadBall, mSadBallX[i] - mSadBall.getWidth()/2, mSadBallY[i] - mSadBall.getHeight()/2, null); | |
} | |
} | |
//This is run whenever the phone is touched by the user | |
@Override | |
protected void actionOnTouch(float x, float y) { | |
mPaddleX = x; | |
//mPaddleX = x - mPaddle.getWidth() / 2; | |
} | |
//This is run whenever the phone moves around its axises | |
@Override | |
protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) { | |
if(mPaddleX >= 0 && mPaddleX <= mCanvasWidth){ | |
mPaddleX = mPaddleX - xDirection; | |
if(mPaddleX < 0) mPaddleX = 0; | |
if(mPaddleX > mCanvasWidth) mPaddleX = mCanvasWidth; | |
} | |
} | |
//This is run just before the game "scenario" is printed on the screen | |
@Override | |
protected void updateGame(float secondsElapsed) { | |
float distanceBetweenBallAndPaddle; | |
float distanceBetweenBallAndSmiley; | |
// Have we hit the Paddle ? | |
if (mBallSpeedY > 0){ // Is the ball is moving down the screen | |
// Current distance between ball and paddle | |
distanceBetweenBallAndPaddle = (mPaddleX - mBallX) * (mPaddleX - mBallX) + (mCanvasHeight - mBallY) * (mCanvasHeight - mBallY); | |
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 for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play sound when paddle hit | |
mSoundPool.play(mSoundPaddle, 100, 100, 1, 0, 1.0f); | |
} | |
} | |
// Have we hit the Sadball target ? | |
for(int i = 0; i < mSadBallX.length; i++) { | |
// Get actual distance between the mBall and the sad ball | |
distanceBetweenBallAndPaddle = (mSadBallX[i] - mBallX) * (mSadBallX[i] - mBallX) + (mSadBallY[i] - mBallY) * (mSadBallY[i] - mBallY); | |
// Check if the actual distance is lower than the allowed => collision | |
if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndPaddle) { | |
// Get present velocity | |
float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY * mBallSpeedY); | |
// Change the direction of the ball | |
mBallSpeedX = mBallX - mSadBallX[i]; | |
mBallSpeedY = mBallY - mSadBallY[i]; | |
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
// Update new velocity | |
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity; | |
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
mSoundPool.play(mSoundPaddle, 100, 100, 1, 0, 1.0f); | |
} | |
} | |
// Have we hit the target | |
distanceBetweenBallAndSmiley = (mSmileyX - mBallX) * (mSmileyX - mBallX) + (mSmileyY - mBallY) * (mSmileyY - mBallY); | |
// Check if the Ball hits the smiley face. | |
if(mMinDistanceBetweenRedBallAndSmiley >= distanceBetweenBallAndSmiley){ | |
// 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; | |
// 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; | |
// Speed up ball by 1% after every score | |
mBallSpeedX = mBallSpeedX * 1.01f; | |
mBallSpeedY = mBallSpeedY * 1.01f; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play sound when ball hits target | |
mSoundPool.play(mSoundScore, 100, 100, 1, 0, 1.0f); | |
// Update score when ball hits target | |
updateScore(1); | |
} | |
//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 ball hits either side of the screen reflect it back | |
if ((mBallX <= mBall.getWidth()/2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2 && mBallSpeedX > 0)) { | |
mBallSpeedX = -mBallSpeedX; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play sound when ball hits left or right side of screen | |
mSoundPool.play(mSoundBounce, 100, 100, 1, 0, 1.0f); | |
} | |
// Has ball hit top of screen | |
if (mBallY <= mBall.getHeight()/2 && mBallSpeedY < 0){ | |
mBallSpeedY = -mBallSpeedY; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play sound when ball hits top of screen | |
mSoundPool.play(mSoundBounce, 100, 100, 1, 0, 1.0f); | |
} | |
/*// cheat code | |
// Has ball hit top of screen | |
if (mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY > 0){ | |
mBallSpeedY = -mBallSpeedY; | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play sound when ball hits top of screen | |
mSoundPool.play(mSoundBounce, 100, 100, 1, 0, 1.0f); | |
} | |
*/ | |
// correct code | |
// Has ball dropped off bottom of screen | |
if (mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY > 0){ | |
/* | |
################################################################################################ | |
# Added for sound with soundpool # | |
################################################################################################ | |
*/ | |
// Play sound when paddle misses ball | |
mSoundPool.play(mSoundLose, 100, 100, 1, 0, 1.0f); | |
// End game | |
setState(GameThread.STATE_LOSE); | |
} | |
} | |
} | |
// This file is part of the course "Begin Programming: Build your first mobile game" from futurelearn.com | |
// Copyright: University of Reading and Karsten Lundqvist | |
// It is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// It is is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with it. If not, see <http://www.gnu.org/licenses/>. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment