Created
March 12, 2014 21:33
-
-
Save robsbots/9516874 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
// # This is a copy of my current code at then end of week 3 | |
// # You can download some instructions and the sound files I used in a zip file from | |
// # http://www.robsbots.org.uk/files/mooc/sounds.zip | |
// # You will need to extract the contents into a folder on your computer, | |
// # then open the instructions.txt file to explain how to enable sound. | |
// # | |
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; | |
import android.media.MediaPlayer; | |
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; | |
//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; | |
//Create a media player object for each sound in the game | |
private MediaPlayer mLooseSound; | |
private MediaPlayer mHitPaddle; | |
private MediaPlayer mScoreSound; | |
private MediaPlayer mStartSound; | |
private MediaPlayer mBounceSound; | |
//This is run before anything else, so we can prepare things here | |
public TheGame(GameView gameView) { | |
//House keeping | |
super(gameView); | |
//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); | |
// Load sound clip for each media player | |
mLooseSound = MediaPlayer.create(gameView.getContext(), R.raw.sadtrombone); | |
mHitPaddle = MediaPlayer.create(gameView.getContext(), R.raw.click); | |
mScoreSound = MediaPlayer.create(gameView.getContext(), R.raw.score); | |
mStartSound = MediaPlayer.create(gameView.getContext(), R.raw.start); | |
mBounceSound = MediaPlayer.create(gameView.getContext(), R.raw.bounce); | |
} | |
//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 /6; | |
// 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); | |
// Play a sound when the game starts | |
mStartSound.start(); | |
} | |
@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); | |
} | |
//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; | |
// Play sound when paddle hit | |
mHitPaddle.start(); | |
} | |
} | |
// 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; | |
// Play sound when ball hits target | |
mScoreSound.start(); | |
// 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; | |
// Play sound when ball hits left or right side of screen | |
mBounceSound.start(); | |
} | |
// Has ball hit top of screen | |
if (mBallY <= mBall.getHeight()/2 && mBallSpeedY < 0){ | |
mBallSpeedY = -mBallSpeedY; | |
// Play sound when ball hits top of screen | |
mBounceSound.start(); | |
} | |
// Has ball dropped off bottom of screen | |
if (mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY > 0){ | |
// Play sound when paddle misses ball | |
mLooseSound.start(); | |
// 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