Last active
August 29, 2015 13:57
-
-
Save robsbots/9711754 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
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.media.MediaPlayer; | |
public class TheGame extends GameThread{ | |
//Will store the image of a ball | |
private Bitmap mBall; | |
//The X and Y position of the ball on the screen (middle of ball) | |
private float mBallX = 0; | |
private float mBallY = 0; | |
private float mSmileY = 0; | |
private float mSmileX = 0; | |
private float mPaddleX = 0; | |
private float mSquareX = 0; | |
private Bitmap mSquaredestroy; | |
// ########################################################################################################################### | |
// New variable to describe the current state of the block | |
private Boolean mDestroySquare; | |
private Boolean mSmileBallSmile; | |
private Bitmap mSmileBallImage; | |
private Bitmap mPaddle; | |
private Bitmap mSquare; | |
private Bitmap mSad; | |
private Bitmap mSmile; | |
private MediaPlayer mHitPaddle; | |
//The speed (pixel/second) of the ball in direction X and Y | |
private float mBallSpeedX = 0; | |
private float mBallSpeedY = 0; | |
private float mSmileSpeedX = 0; | |
private float mSmileSpeedY = 0; | |
private float mSquareSpeedX = 0; | |
private float mMinDistanceBetweenRedBallAndSmile = 0; | |
private float mMinDistanceBetweenRedBallAndSquare = 0; | |
private float mMinDistanceBetweenRedBallAndBigBall; | |
private float mSquareY; | |
//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); | |
mSmileBallImage = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.smiley_ball); | |
mSmile = mSmileBallImage; | |
mSmileBallSmile = true; | |
mSad = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.sad_ball); | |
mSquare = BitmapFactory.decodeResource | |
(gameView.getContext().getResources(), | |
R.drawable.block); | |
// ########################################################################################################################### | |
// set mDestroySquare to false. The square has not been destroyed. | |
mDestroySquare = false; | |
mHitPaddle = 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 = 25; | |
mBallSpeedY = -202; | |
mSquareSpeedX = -150; | |
mSmileSpeedX = 150; | |
mSmileSpeedY = 0; | |
//Place the ball in the middle of the screen. | |
//mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball | |
mBallX = mCanvasWidth / 2; | |
mBallY = mCanvasHeight / 2; | |
mSmileX = mCanvasWidth / 2; | |
mSquareX = mCanvasWidth /2; | |
mSmileX = mCanvasWidth / 2; | |
mPaddleX = mCanvasWidth / 2; | |
mSquareY = mCanvasHeight /4; | |
mSmileY = mSmile.getHeight()/2; | |
mMinDistanceBetweenRedBallAndSmile = (mBall.getWidth() / 2 + mSmile.getWidth() / 2) * (mBall.getWidth() / 2 + mSmile.getWidth() / 2); | |
mMinDistanceBetweenRedBallAndSquare = (mSquare.getWidth() / 2 + mBall.getWidth() / 2) * (mSquare.getWidth() / 2 + mBall.getWidth() / 2); | |
mMinDistanceBetweenRedBallAndBigBall = (mPaddle.getWidth() / 2 + mBall.getWidth() / 2) * (mPaddle.getWidth() / 2 + mBall.getWidth() / 2); | |
} | |
@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 using the X and Y of the ball | |
//drawBitmap uses top left corner as reference, we use middle of picture | |
//null means that we will use the image without any extra features (called Paint) | |
canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null); | |
canvas.drawBitmap(mPaddle, mPaddleX - mPaddle.getWidth()/2, mCanvasHeight - mPaddle.getHeight()/2, null); | |
canvas.drawBitmap(mSmile, mSmileX - mSmile.getWidth()/2, mSmileY - mSmile.getHeight()/2, null); | |
// ########################################################################################################################### | |
// Added this check to see if the square has been destroyed. | |
// If the sqaure has not been destroyed (mDestroySquare is equal to false) | |
// draw the sqaure | |
if(mDestroySquare==false) { | |
canvas.drawBitmap(mSquare, mSquareX - mSquare.getWidth()/2, mSquareY - mSquare.getHeight()/2, null); | |
} | |
} | |
//This is run whenever the phone is touched by the user | |
@Override | |
protected void actionOnTouch(float x, float y) { | |
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) { | |
if (mDestroySquare==false) { | |
float distanceBetweenBallAndSquare; | |
distanceBetweenBallAndSquare = (mBallX - mSquareX) * (mBallX - mSquareX) + (mBallY - mSquareY) *(mBallY - mSquareY); | |
if(mMinDistanceBetweenRedBallAndSquare>= distanceBetweenBallAndSquare) { | |
float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
mBallSpeedX = mBallX - mSquareX; | |
mBallSpeedY = mBallY - mSquareY; | |
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity; | |
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity; | |
//########################################################################################################################### | |
//set mDestroySquare to false. The square has not been destroyed. | |
mDestroySquare = true; | |
} | |
} | |
float distanceBetweenBallAndPaddle; | |
if(mBallSpeedY >= 0) { | |
distanceBetweenBallAndPaddle = (mPaddleX - mBallX) * (mPaddleX - mBallX) + (mCanvasHeight - mBallY) * (mCanvasHeight - mBallY); | |
if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndPaddle) { | |
float velocityofBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
mBallSpeedX = mBallX - mPaddleX; | |
mBallSpeedY = mBallY - mCanvasHeight; | |
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
mBallSpeedX = mBallSpeedX * velocityofBall / newVelocity; | |
mBallSpeedY = mBallSpeedY * velocityofBall / newVelocity; | |
mHitPaddle.start(); | |
} | |
} | |
float distanceBetweenBallAndSmile; | |
//c sq = a sq + b sq | |
distanceBetweenBallAndSmile = (mBallX - mSmileX) * (mBallX - mSmileX) + (mBallY - mSmileY) *(mBallY - mSmileY); | |
if(mMinDistanceBetweenRedBallAndSmile>= distanceBetweenBallAndSmile) { | |
float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
mBallSpeedX = mBallX - mSmileX; | |
mBallSpeedY = mBallY - mSmileY; | |
float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY); | |
mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity; | |
mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity; | |
//Move the ball's X and Y using the speed (pixel/sec) | |
mBallX = mBallX + secondsElapsed * mBallSpeedX; | |
mBallY = mBallY + secondsElapsed * mBallSpeedY; | |
//Set Imagery | |
if (mSmileBallSmile){ | |
mSmile = mSad; | |
mSmileBallSmile = false; | |
} | |
else{ | |
mSmile = mSmileBallImage; | |
mSmileBallSmile = true; | |
} | |
//there is a function (previously created) to update the score 'updateScore' | |
updateScore(1); | |
if(score >= 20) { | |
setState(GameThread.STATE_WIN); | |
} | |
} | |
if((mSquareX <= mBall.getWidth()* | |
2.3 && mSquareSpeedX <0) || (mSquareX >= mCanvasWidth - mSquare.getWidth()*0.8f)) { | |
mSquareSpeedX = -mSquareSpeedX; | |
} | |
if((mBallX <= mBall.getWidth()/2 && mBallSpeedX <0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2)) { | |
mBallSpeedX = -mBallSpeedX; | |
} | |
if((mBallY <= mBall.getHeight()/2) && mBallSpeedY < 0 ) { | |
mBallSpeedY = -mBallSpeedY; | |
} | |
if((mSmileX <= mSmile.getWidth()/2 && mSmileSpeedX <0) || (mSmileX >= mCanvasWidth - mSmile.getWidth()/2)) { | |
mSmileSpeedX = -mSmileSpeedX; | |
} | |
if((mSmileY <= mSmile.getHeight()/2 && mSmileSpeedY <0) || (mSmileY >= mCanvasHeight - mSmile.getHeight()/2)) { | |
mSmileSpeedY = -mSmileSpeedY; | |
} | |
if(mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY >0) { | |
setState(GameThread.STATE_LOSE); | |
} | |
//Move the ball's X and Y using the speed (pixel/sec) | |
mBallX = mBallX + secondsElapsed * mBallSpeedX; | |
mBallY = mBallY + secondsElapsed * mBallSpeedY; | |
mSmileX = mSmileX + secondsElapsed * mSmileSpeedX; | |
mSmileY = mSmileY + secondsElapsed * mSmileSpeedY; | |
mSquareX = mSquareX + secondsElapsed * mSquareSpeedX; | |
} | |
} | |
// 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