Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- 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;
- //The speed (pixel/second) of the ball in direction X and Y
- private float mBallSpeedX = 0;
- private float mBallSpeedY = 0;
- private Bitmap bleuBall;
- //The X and Y position of the Paddle
- private float bleuBallX = -100;
- private float bleuBallY = -100;
- //The Speed of the paddle in direction X and Y
- private float bleuBallSpeedX = 0;
- private float bleuBallSpeedY = 0;
- //Will store the image of the goal
- private Bitmap goal;
- private float goalX = -100;
- private float goalY = -100;
- //Will store the image of the redBall
- private Bitmap redBall;
- private float[] redBallX = {-100, 0};
- private float[] redBallY = new float[2];
- //The Speed of the redBall in direction X and Y
- private float redBallSpeedX = 0;
- private float mMinDistanceBetweenBleuBallAndBigBall = 0;
- //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.foot_ball);
- bleuBall = BitmapFactory.decodeResource
- (gameView.getContext().getResources(),
- R.drawable.blue_ball);
- //Prepare the image of the Goal
- goal = BitmapFactory.decodeResource
- (gameView.getContext().getResources(),
- R.drawable.goal);
- //Prepare the image of the redBall
- redBall = BitmapFactory.decodeResource
- (gameView.getContext().getResources(),
- R.drawable.red_ball);
- }
- //This is run before a new game (also after an old game)
- @Override
- public void setupBeginning() {
- //Initialise speeds
- mBallSpeedX = mCanvasWidth /3;
- mBallSpeedY = mCanvasHeight /3;
- //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;
- bleuBallX = mCanvasWidth / 2;
- //Place SmileyBall in the top middle of the screen
- goalX = mCanvasWidth / 2;
- goalY = goal.getHeight()/2;
- //Place all RedBall
- redBallX[0] = mCanvasWidth /2;
- redBallY[0] = mCanvasHeight /2;
- redBallX[1] = (mCanvasWidth /2)-50;
- redBallY[1] = mCanvasHeight / 4;
- redBallSpeedX = mCanvasWidth / 4;
- //Get the minimum distance between a bleuBall and a Football
- //We leave out the square root to limit the calculations of the program
- //Remember to do that when testing the distance as well
- mMinDistanceBetweenBleuBallAndBigBall = (bleuBall.getWidth() / 2 + mBall.getWidth() / 2) * (bleuBall.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(bleuBall , bleuBallX - bleuBall.getWidth()/2,bleuBallY - bleuBall.getHeight() / 2, null);
- //Draw Goal
- canvas.drawBitmap(goal, goalX - goal.getWidth() / 2, goalY - goal.getHeight() / 2, null);
- //Loop through all RedBall
- for(int i=0; i < redBallX.length; i++){
- canvas.drawBitmap(redBall, redBallX[i] - redBall.getWidth()/2 ,redBallY[i] - redBall.getHeight() /2, null );
- }
- }
- //This is run whenever the phone is touched by the user
- @Override
- protected void actionOnTouch(float x, float y) {
- //Increase/decrease the speed of the ball making the ball move towards the touch
- bleuBallX = x - bleuBall.getWidth() /2 ;
- bleuBallY = y - bleuBall.getHeight() /2 ;
- }
- //This is run whenever the phone moves around its axises
- @Override
- protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
- //Increase/decrease the speed of the ball
- mBallSpeedX = mBallSpeedX - 3f * xDirection;
- mBallSpeedY = mBallSpeedY - 3f * yDirection;
- bleuBallSpeedX = bleuBallSpeedX - 3f * xDirection;
- bleuBallSpeedY = bleuBallSpeedY - 3f * yDirection;
- redBallSpeedX = redBallSpeedX - 3f * xDirection;
- if (bleuBallX >=0 && bleuBallX <= mCanvasWidth){
- bleuBallX = bleuBallX -xDirection;
- bleuBallY = bleuBallY -yDirection;
- if(bleuBallX < 0)bleuBallX=0;
- if(bleuBallX > mCanvasWidth) bleuBallX = mCanvasWidth;
- }
- }
- //This is run just before the game "scenario" is printed on the screen
- @Override
- protected void updateGame(float secondsElapsed) {
- float distanceBetweenBallAndBleuBall = 0;
- float distanceBetweenBallAndGoal = 0;
- float distanceBetweenBallAndRedBall = 0;
- //If the ball moves down on the screen perform potential paddle collision
- if(mBallSpeedY > 0) {
- //Get actual distance (without square root - remember?) between the mBall and the ball being checked
- distanceBetweenBallAndBleuBall = (bleuBallX - mBallX) * (bleuBallX - mBallX) + (mCanvasHeight - mBallY) *(mCanvasHeight - mBallY);
- //Check if the actual distance is lower than the allowed => collision
- if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndBleuBall) {
- //Get the present velocity (this should also be the velocity going away after the collision)
- float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- mBallSpeedX = mBallX - bleuBallX;
- mBallSpeedY = mBallY - bleuBallY;
- 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;
- //Move the bleuball's X and Y using the speed (pixel/sec)
- bleuBallX = bleuBallX + secondsElapsed * bleuBallSpeedX;
- bleuBallY = bleuBallY + secondsElapsed * bleuBallSpeedY;
- for (int i=0; i < redBallX.length;i++){
- distanceBetweenBallAndRedBall = (redBallX[i] - mBallX) * (redBallX[i] - mBallX) + (redBallY[i] - mBallY) *(redBallY[i] - mBallY);
- //Move the redball's X using the speed (pixel/sec)
- redBallX[i] = redBallX[i] + secondsElapsed * redBallSpeedX;
- //Check if the ball hits either the left side or the right side of the screen
- //But only do something if the ball is moving towards that side of the screen
- //If it does that => change the direction of the ball in the X direction
- if((mBallX <= mBall.getWidth() / 2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth() / 2 && mBallSpeedX > 0) ) {
- mBallSpeedX = -mBallSpeedX;
- }
- if ((redBallX[i] <= redBall.getWidth()/2 && redBallSpeedX < 0) || (redBallX[i] >= mCanvasWidth - redBall.getWidth()/2 && redBallSpeedX > 0 )){
- redBallSpeedX = -redBallSpeedX;
- }
- //Check if the actual distance is lower than the allowed => collision with RedBall
- if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndRedBall) {
- //Get the present velocity (this should also be the velocity going away after the collision)
- float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //Change the direction of the red ball
- mBallSpeedX = mBallX - redBallX[i];
- mBallSpeedY = mBallY - redBallY[i];
- //Get the velocity after the collision
- float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //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;
- }
- }
- distanceBetweenBallAndGoal = (goalX - mBallX) * (goalX - mBallX) + (goalY - mBallY) *(goalY - mBallY);
- distanceBetweenBallAndBleuBall = (bleuBallX - mBallX) * (bleuBallX - mBallX) + (bleuBallY - mBallY) *(bleuBallY - mBallY);
- if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndBleuBall) {
- //Get the present velocity (this should also be the velocity going away after the collision)
- float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //Change the direction of the ball
- mBallSpeedX = mBallX - bleuBallX;
- mBallSpeedY = mBallY - bleuBallY;
- //Get the velocity after the collision
- float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //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;
- }
- //Check if the actual distance is lower than the allowed => collision
- if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndGoal) {
- //Get the present velocity (this should also be the velocity going away after the collision)
- float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //Change the direction of the Goal
- mBallSpeedX = mBallX - goalX;
- mBallSpeedY = mBallY - goalY;
- //Increase score
- updateScore(1);
- //Get the velocity after the collision
- float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
- //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;
- }
- //If the ball goes out of the top of the screen and moves towards the top of the screen =>
- //change the direction of the ball in the Y direction
- if(mBallY <= mBall.getWidth() / 2 && mBallSpeedY < 0) {
- mBallSpeedY = -mBallSpeedY;
- }
- //If the ball goes out of the bottom of the screen => lose the game
- if(score==3) {
- setState(GameThread.STATE_WIN);
- }
- if((mBallY >= mCanvasHeight) && (score < 3 )) {
- 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/>.
Advertisement
Add Comment
Please, Sign In to add comment