Ashraf_Shetwene

Untitled

Nov 25th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.97 KB | None | 0 0
  1. package uk.ac.reading.sis05kol.mooc;
  2.  
  3. //Other parts of the android libraries that we use
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7.  
  8. public class TheGame extends GameThread{
  9.  
  10.     //Will store the image of a ball
  11.     private Bitmap mBall;
  12.  
  13.    
  14.     //The X and Y position of the ball on the screen (middle of ball)
  15.     private float mBallX = 0;
  16.     private float mBallY = 0;
  17.    
  18.     //The speed (pixel/second) of the ball in direction X and Y
  19.     private float mBallSpeedX = 0;
  20.     private float mBallSpeedY = 0;
  21.    
  22.     private Bitmap bleuBall;
  23.    
  24.     //The X and Y position of the Paddle
  25.     private float bleuBallX = -100;
  26.     private float bleuBallY = -100;
  27.    
  28.     //The Speed of the paddle in direction X and Y
  29.     private float bleuBallSpeedX = 0;
  30.     private float bleuBallSpeedY = 0;
  31.    
  32.     //Will store the image of the goal
  33.     private Bitmap goal;
  34.    
  35.     private float goalX = -100;
  36.     private float goalY = -100;
  37.    
  38.     //Will store the image of the redBall
  39.     private Bitmap redBall;
  40.    
  41.     private float[] redBallX = {-100, 0};
  42.     private float[] redBallY = new float[2];
  43.    
  44.     //The Speed of the redBall in direction X and Y
  45.     private float redBallSpeedX = 0;
  46.    
  47.     private float mMinDistanceBetweenBleuBallAndBigBall = 0;
  48.  
  49.  
  50.     //This is run before anything else, so we can prepare things here
  51.     public TheGame(GameView gameView) {
  52.         //House keeping
  53.         super(gameView);
  54.        
  55.         //Prepare the image so we can draw it on the screen (using a canvas)
  56.         mBall = BitmapFactory.decodeResource
  57.                 (gameView.getContext().getResources(),
  58.                 R.drawable.foot_ball);
  59.         bleuBall = BitmapFactory.decodeResource
  60.                 (gameView.getContext().getResources(),
  61.                 R.drawable.blue_ball);
  62.         //Prepare the image of the Goal
  63.         goal =  BitmapFactory.decodeResource
  64.                 (gameView.getContext().getResources(),
  65.                 R.drawable.goal);
  66.         //Prepare the image of the redBall
  67.         redBall =  BitmapFactory.decodeResource
  68.                 (gameView.getContext().getResources(),
  69.                 R.drawable.red_ball);
  70.     }
  71.    
  72.     //This is run before a new game (also after an old game)
  73.     @Override
  74.     public void setupBeginning() {
  75.         //Initialise speeds
  76.         mBallSpeedX = mCanvasWidth /3;
  77.         mBallSpeedY = mCanvasHeight /3;
  78.        
  79.         //Place the ball in the middle of the screen.
  80.         //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
  81.         mBallX = mCanvasWidth / 2;
  82.         mBallY = mCanvasHeight / 2;
  83.        
  84.         bleuBallX = mCanvasWidth / 2;
  85.        
  86.        
  87.         //Place SmileyBall in the top middle of the screen
  88.         goalX = mCanvasWidth / 2;
  89.         goalY = goal.getHeight()/2;
  90.        
  91.         //Place all RedBall
  92.         redBallX[0] = mCanvasWidth /2;
  93.         redBallY[0] = mCanvasHeight /2;
  94.        
  95.        
  96.        
  97.         redBallX[1] = (mCanvasWidth /2)-50;
  98.         redBallY[1] = mCanvasHeight / 4;
  99.        
  100.         redBallSpeedX = mCanvasWidth / 4;
  101.        
  102.         //Get the minimum distance between a bleuBall and a Football
  103.         //We leave out the square root to limit the calculations of the program
  104.         //Remember to do that when testing the distance as well
  105.         mMinDistanceBetweenBleuBallAndBigBall = (bleuBall.getWidth() / 2 + mBall.getWidth() / 2) * (bleuBall.getWidth() / 2 + mBall.getWidth() / 2);
  106.        
  107.    
  108.     }
  109.        
  110.     @Override
  111.     protected void doDraw(Canvas canvas) {
  112.         //If there isn't a canvas to draw on do nothing
  113.         //It is ok not understanding what is happening here
  114.         if(canvas == null) return;
  115.        
  116.         super.doDraw(canvas);
  117.        
  118.         //draw the image of the ball using the X and Y of the ball
  119.         //drawBitmap uses top left corner as reference, we use middle of picture
  120.         //null means that we will use the image without any extra features (called Paint)
  121.         canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null);
  122.         canvas.drawBitmap(bleuBall , bleuBallX - bleuBall.getWidth()/2,bleuBallY - bleuBall.getHeight() / 2, null);
  123.         //Draw Goal
  124.         canvas.drawBitmap(goal, goalX - goal.getWidth() / 2, goalY - goal.getHeight() / 2, null);
  125.       //Loop through all RedBall
  126.         for(int i=0; i < redBallX.length; i++){
  127.             canvas.drawBitmap(redBall, redBallX[i] - redBall.getWidth()/2  ,redBallY[i] - redBall.getHeight() /2, null  );
  128.         }
  129.     }
  130.    
  131.     //This is run whenever the phone is touched by the user
  132.    
  133.     @Override
  134.     protected void actionOnTouch(float x, float y) {
  135.         //Increase/decrease the speed of the ball making the ball move towards the touch
  136.         bleuBallX = x - bleuBall.getWidth() /2 ;
  137.         bleuBallY = y - bleuBall.getHeight() /2 ;
  138.     }
  139.    
  140.     //This is run whenever the phone moves around its axises
  141.     @Override
  142.     protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
  143.         //Increase/decrease the speed of the ball
  144.         mBallSpeedX = mBallSpeedX - 3f * xDirection;
  145.         mBallSpeedY = mBallSpeedY - 3f * yDirection;
  146.        
  147.         bleuBallSpeedX = bleuBallSpeedX - 3f * xDirection;
  148.         bleuBallSpeedY = bleuBallSpeedY - 3f * yDirection;
  149.        
  150.         redBallSpeedX = redBallSpeedX - 3f * xDirection;
  151.        
  152.            
  153.         if (bleuBallX >=0 && bleuBallX <= mCanvasWidth){
  154.         bleuBallX = bleuBallX -xDirection;
  155.         bleuBallY = bleuBallY -yDirection;
  156.         if(bleuBallX < 0)bleuBallX=0;
  157.         if(bleuBallX > mCanvasWidth) bleuBallX = mCanvasWidth;
  158.  
  159.         }
  160.    
  161.    
  162.             }
  163.    
  164.     //This is run just before the game "scenario" is printed on the screen
  165.         @Override
  166.         protected void updateGame(float secondsElapsed) {
  167.             float distanceBetweenBallAndBleuBall = 0;
  168.             float distanceBetweenBallAndGoal = 0;
  169.             float distanceBetweenBallAndRedBall = 0;
  170.            
  171.             //If the ball moves down on the screen perform potential paddle collision
  172.             if(mBallSpeedY > 0) {
  173.                 //Get actual distance (without square root - remember?) between the mBall and the ball being checked
  174.                 distanceBetweenBallAndBleuBall = (bleuBallX - mBallX) * (bleuBallX - mBallX) + (mCanvasHeight - mBallY) *(mCanvasHeight - mBallY);
  175.            
  176.                 //Check if the actual distance is lower than the allowed => collision
  177.                 if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndBleuBall) {
  178.  
  179.                     //Get the present velocity (this should also be the velocity going away after the collision)
  180.                     float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  181.                    
  182.                     mBallSpeedX = mBallX - bleuBallX;
  183.                     mBallSpeedY = mBallY - bleuBallY;
  184.                    
  185.                     float
  186.                    
  187.                     newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  188.  
  189.                     mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
  190.                     mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
  191.                    
  192.                    
  193.                 }
  194.             }
  195.        
  196.  
  197.             //Move the ball's X and Y using the speed (pixel/sec)
  198.             mBallX = mBallX + secondsElapsed * mBallSpeedX;
  199.             mBallY = mBallY + secondsElapsed * mBallSpeedY;
  200.  
  201.             //Move the bleuball's X and Y using the speed (pixel/sec)
  202.             bleuBallX = bleuBallX + secondsElapsed * bleuBallSpeedX;
  203.             bleuBallY = bleuBallY + secondsElapsed * bleuBallSpeedY;
  204.  
  205.             for (int i=0; i < redBallX.length;i++){
  206.                 distanceBetweenBallAndRedBall = (redBallX[i] - mBallX) * (redBallX[i] - mBallX) + (redBallY[i] - mBallY) *(redBallY[i] - mBallY);
  207.                
  208.                 //Move the redball's X  using the speed (pixel/sec)
  209.                 redBallX[i] = redBallX[i] + secondsElapsed * redBallSpeedX;
  210.                
  211.                 //Check if the ball hits either the left side or the right side of the screen
  212.                 //But only do something if the ball is moving towards that side of the screen
  213.                 //If it does that => change the direction of the ball in the X direction
  214.                 if((mBallX <= mBall.getWidth() / 2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth() / 2 && mBallSpeedX > 0) ) {
  215.                     mBallSpeedX = -mBallSpeedX;
  216.                    
  217.                 }
  218.                
  219.                 if ((redBallX[i] <= redBall.getWidth()/2 && redBallSpeedX < 0) || (redBallX[i] >= mCanvasWidth - redBall.getWidth()/2 && redBallSpeedX > 0 )){
  220.                     redBallSpeedX = -redBallSpeedX;
  221.             }
  222.                 //Check if the actual distance is lower than the allowed => collision with RedBall
  223.                 if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndRedBall) {
  224.  
  225.                     //Get the present velocity (this should also be the velocity going away after the collision)
  226.                     float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  227.  
  228.                     //Change the direction of the red ball
  229.                    
  230.                         mBallSpeedX = mBallX - redBallX[i];
  231.                         mBallSpeedY = mBallY - redBallY[i];
  232.                    
  233.                            
  234.                    
  235.                     //Get the velocity after the collision
  236.                     float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  237.  
  238.                     //using the fraction between the original velocity and present velocity to calculate the needed
  239.                     //speeds in X and Y to get the original velocity but with the new angle.
  240.                     mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
  241.                     mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
  242.                    
  243.                 }
  244.                
  245.             }
  246.            
  247.             distanceBetweenBallAndGoal = (goalX - mBallX) * (goalX - mBallX) + (goalY - mBallY) *(goalY - mBallY);
  248.             distanceBetweenBallAndBleuBall = (bleuBallX - mBallX) * (bleuBallX - mBallX) + (bleuBallY - mBallY) *(bleuBallY - mBallY);
  249.            
  250.             if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndBleuBall) {
  251.  
  252.                 //Get the present velocity (this should also be the velocity going away after the collision)
  253.                 float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  254.  
  255.                 //Change the direction of the ball
  256.                 mBallSpeedX = mBallX - bleuBallX;
  257.                 mBallSpeedY = mBallY - bleuBallY;
  258.                
  259.            
  260.                
  261.                 //Get the velocity after the collision
  262.                 float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  263.  
  264.                 //using the fraction between the original velocity and present velocity to calculate the needed
  265.                 //speeds in X and Y to get the original velocity but with the new angle.
  266.                 mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
  267.                 mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
  268.                
  269.             }
  270.            
  271.            
  272.             //Check if the actual distance is lower than the allowed => collision
  273.             if(mMinDistanceBetweenBleuBallAndBigBall >= distanceBetweenBallAndGoal) {
  274.  
  275.                 //Get the present velocity (this should also be the velocity going away after the collision)
  276.                 float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  277.  
  278.                 //Change the direction of the Goal
  279.                 mBallSpeedX = mBallX - goalX;
  280.                 mBallSpeedY = mBallY - goalY;
  281.                 //Increase score
  282.                 updateScore(1);
  283.                
  284.                 //Get the velocity after the collision
  285.                 float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  286.  
  287.                 //using the fraction between the original velocity and present velocity to calculate the needed
  288.                 //speeds in X and Y to get the original velocity but with the new angle.
  289.                 mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
  290.                 mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
  291.                
  292.             }
  293.  
  294.            
  295.            
  296.        
  297.             //If the ball goes out of the top of the screen and moves towards the top of the screen =>
  298.             //change the direction of the ball in the Y direction
  299.             if(mBallY <= mBall.getWidth() / 2 && mBallSpeedY < 0) {
  300.                 mBallSpeedY = -mBallSpeedY;
  301.                
  302.             }
  303.  
  304.             //If the ball goes out of the bottom of the screen => lose the game
  305.             if(score==3) {
  306.                 setState(GameThread.STATE_WIN);
  307.             }
  308.        
  309.            
  310.             if((mBallY >= mCanvasHeight) && (score < 3 )) {
  311.                 setState(GameThread.STATE_LOSE);
  312.             }
  313.         }
  314.     }
  315.  
  316.     // This file is part of the course "Begin Programming: Build your first mobile game" from futurelearn.com
  317.     // Copyright: University of Reading and Karsten Lundqvist
  318.     // It is free software: you can redistribute it and/or modify
  319.     // it under the terms of the GNU General Public License as published by
  320.     // the Free Software Foundation, either version 3 of the License, or
  321.     // (at your option) any later version.
  322.     //
  323.     // It is is distributed in the hope that it will be useful,
  324.     // but WITHOUT ANY WARRANTY; without even the implied warranty of
  325.     // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  326.     // GNU General Public License for more details.
  327.     //
  328.     //
  329.     // You should have received a copy of the GNU General Public License
  330.     // along with it.  If not, see <http://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment