Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.31 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. import android.media.AudioManager;
  8. import android.media.SoundPool;
  9.  
  10. public class TheGame extends GameThread{
  11.  
  12.     //Will store the image of a ball
  13.     private Bitmap mBall;
  14.  
  15.     //The X and Y position of the ball on the screen
  16.     //The point is the top left corner, not middle, of the ball
  17.     //Initially at -100 to avoid them being drawn in 0,0 of the screen
  18.     private float mBallX = -100;
  19.     private float mBallY = -100;
  20.  
  21.     //The speed (pixel/second) of the ball in direction X and Y
  22.     private float mBallSpeedX = 0;
  23.     private float mBallSpeedY = 0;
  24.  
  25.  
  26.     //Will store the image of the Paddle used to hit the ball
  27.     private Bitmap mPaddle;
  28.    
  29.     //Paddle's x position. Y will always be the bottom of the screen
  30.     private float mPaddleX = 0;
  31.    
  32.     //Will store the image of the smiley ball (score ball)
  33.     private Bitmap mSmileyBall;
  34.  
  35.     //The X and Y position of the ball on the screen
  36.     //The point is the top left corner, not middle, of the ball
  37.     //Initially at -100 to avoid them being drawn in 0,0 of the screen
  38.     private float mSmileyBallX = -100;
  39.     private float mSmileyBallY = -100;
  40.    
  41.     //Will store the image of the smiley ball (score ball)
  42.     private Bitmap mSadBall;
  43.  
  44.     //The X and Y position of the SadBalls on the screen
  45.     //The point is the top left corner, not middle, of the balls
  46.     //Initially at -100 to avoid them being drawn in 0,0 of the screen
  47.     private float[] mSadBallX = {-100,-100,-100,-100,-100};
  48.     private float[] mSadBallY = new float[5];
  49.    
  50.     // this will hold values for removing sad balls and finally the Smiley Ball from the screen
  51.         private int[] mBallRemove={5,7,10,12,15,20};
  52.        
  53.         // this will be passed to the ball remove function as a parameter
  54.         private int mBallchk = 0;
  55.         private int mWinStreak = 0;
  56.  
  57.  
  58.     //This will store the min distance allowed between a big ball and the small red ball
  59.     //This is used to check collisions
  60.     private float mMinDistanceBetweenRedBallAndBigBall = 0;
  61.    
  62.     //Create a Soundpool object
  63.     private SoundPool mSoundPool;
  64.    
  65.  
  66.     // Create 1 int variable for each sound clip you wish to play in the game
  67.      private int mSoundLose;
  68.      private int mSoundMove;
  69.     private int mSoundExplode;
  70.     private int mSoundEnd;
  71.  
  72.     // used for logic removing sad balls
  73.     private int mExplodeCount = 0;
  74.  
  75.  
  76.     //This is run before anything else, so we can prepare things here
  77.     public TheGame(GameView gameView) {
  78.         //House keeping
  79.         super(gameView);
  80.  
  81.         //Prepare the image so we can draw it on the screen (using a canvas)
  82.         mBall = BitmapFactory.decodeResource
  83.                 (gameView.getContext().getResources(),
  84.                         R.drawable.small_red_ball);
  85.  
  86.         //Prepare the image of the paddle so we can draw it on the screen (using a canvas)
  87.         mPaddle = BitmapFactory.decodeResource
  88.                 (gameView.getContext().getResources(),
  89.                         R.drawable.yellow_ball);
  90.  
  91.         //Prepare the image of the SmileyBall so we can draw it on the screen (using a canvas)
  92.         mSmileyBall =  BitmapFactory.decodeResource
  93.                 (gameView.getContext().getResources(),
  94.                         R.drawable.smiley_ball);
  95.  
  96.         //Prepare the image of the SadBall(s) so we can draw it on the screen (using a canvas)
  97.         mSadBall =  BitmapFactory.decodeResource
  98.                 (gameView.getContext().getResources(),
  99.                         R.drawable.sad_ball);
  100.         // Load a sound clip for each sound event you want
  101.         // Setup the new sound pool object
  102.         // I have chosen 3 streams. This means you can play 3 sounds
  103.         // at any one time
  104.         mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
  105.  
  106.         // Load the sounds into soundpool and assign each to an int defined above
  107.         mSoundLose = mSoundPool.load(mContext, R.raw.woe, 1);
  108.         mSoundMove = mSoundPool.load(mContext, R.raw.duh, 2);
  109.         mSoundExplode = mSoundPool.load(mContext, R.raw.explose, 3);
  110.         mSoundEnd = mSoundPool.load(mContext, R.raw.htgot, 4);
  111.        
  112.     }
  113.  
  114.     //This is run before a new game (also after an old game)
  115.     @Override
  116.     public void setupBeginning() {
  117.         //Initialise speeds
  118.         //mCanvasWidth and mCanvasHeight are declared and managed elsewhere
  119.         mBallSpeedX = mCanvasWidth / 3;
  120.         mBallSpeedY = mCanvasHeight / 3;
  121.  
  122.         //Place the ball in the middle of the screen.
  123.         //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
  124.         mBallX = mCanvasWidth / 2;
  125.         mBallY = mCanvasHeight / 2;
  126.  
  127.         //Place Paddle in the middle of the screen
  128.         mPaddleX = mCanvasWidth / 2;
  129.        
  130.         //Place SmileyBall in the top middle of the screen
  131.         mSmileyBallX = mCanvasWidth / 2;
  132.         mSmileyBallY = mSmileyBall.getHeight()/2;
  133.        
  134.         //Place all SadBalls forming a pyramid underneath the SmileyBall
  135.         mSadBallX[0] = mCanvasWidth / 3;
  136.         mSadBallY[0] = mCanvasHeight / 3;
  137.        
  138.         mSadBallX[1] = mCanvasWidth - mCanvasWidth / 3;
  139.         mSadBallY[1] = mCanvasHeight / 3;
  140.  
  141.         mSadBallX[2] = mCanvasWidth / 2;
  142.         mSadBallY[2] = mCanvasHeight / 5;
  143.         if (mWinStreak == 1){
  144.             mSadBallX[3] = mCanvasWidth / 2 -20;
  145.             mSadBallY[3] = mCanvasHeight / 5;
  146.         }
  147.         if (mWinStreak  > 1){
  148.             mSadBallX[4] = mCanvasWidth / 2 +20;
  149.             mSadBallY[4] = mCanvasHeight / 5;
  150.         }
  151.        
  152.         //Get the minimum distance between a small ball and a bigball
  153.         //We leave out the square root to limit the calculations of the program
  154.         //Remember to do that when testing the distance as well
  155.         mMinDistanceBetweenRedBallAndBigBall = (mPaddle.getWidth() / 2 + mBall.getWidth() / 2) * (mPaddle.getWidth() / 2 + mBall.getWidth() / 2);
  156.         // playEnd();
  157.        
  158.     }
  159.  
  160.     @Override
  161.     protected void doDraw(Canvas canvas) {
  162.         //If there isn't a canvas to do nothing
  163.         //It is ok not understanding what is happening here
  164.         if(canvas == null) return;
  165.  
  166.         //House keeping
  167.         super.doDraw(canvas);
  168.  
  169.         //canvas.drawBitmap(bitmap, x, y, paint) uses top/left corner of bitmap as 0,0
  170.         //we use 0,0 in the middle of the bitmap, so negate half of the width and height of the ball to draw the ball as expected
  171.         //A paint of null means that we will use the image without any extra features (called Paint)
  172.        
  173.         //draw the image of the ball using the X and Y of the ball
  174.         canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null);
  175.  
  176.         //Draw Paddle using X of paddle and the bottom of the screen (top/left is 0,0)
  177.         canvas.drawBitmap(mPaddle, mPaddleX - mPaddle.getWidth() / 2, mCanvasHeight - mPaddle.getHeight() / 2, null);
  178.        
  179.         //Draw SmileyBall
  180.         canvas.drawBitmap(mSmileyBall, mSmileyBallX - mSmileyBall.getWidth() / 2, mSmileyBallY - mSmileyBall.getHeight() / 2, null);
  181.        
  182.         //Loop through all SadBall
  183.         for(int i = 0; i < mSadBallX.length; i++) {
  184.             //Draw SadBall in position i
  185.             canvas.drawBitmap(mSadBall, mSadBallX[i] - mSadBall.getWidth() / 2, mSadBallY[i] - mSadBall.getHeight() / 2, null);
  186.         }
  187.     }
  188.  
  189.    
  190.     //This is run whenever the phone is touched by the user
  191.     @Override
  192.     protected void actionOnTouch(float x, float y) {
  193.         //Increase/decrease the speed of the ball making the ball move towards the touch
  194.         mPaddleX = x - mPaddle.getWidth() / 2;
  195.     }
  196.  
  197.    
  198.     //This is run whenever the phone moves around its axises
  199.     @Override
  200.     protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
  201.         //Ensure the paddle stays on the screen
  202.         if(mPaddleX >= 0 && mPaddleX <= mCanvasWidth) {
  203.             //Change the X value according to the x direction of the phone
  204.             mPaddleX = mPaddleX - xDirection;
  205.            
  206.             //after movement ensure it is still on the screen
  207.             if(mPaddleX < 0) mPaddleX = 0;
  208.             if(mPaddleX > mCanvasWidth) mPaddleX = mCanvasWidth;           
  209.         }
  210.     }
  211.      
  212.  
  213.     //This is run just before the game "scenario" is printed on the screen
  214.     @Override
  215.     protected void updateGame(float secondsElapsed) {
  216.        
  217.         //If the ball moves down on the screen
  218.         if(mBallSpeedY > 0) {
  219.             //Check for a paddle collision
  220.             updateBallCollusion(mPaddleX, mCanvasHeight);
  221.         }
  222.        
  223.         //Move the ball's X and Y using the speed (pixel/sec)
  224.         mBallX = mBallX + secondsElapsed * mBallSpeedX;
  225.         mBallY = mBallY + secondsElapsed * mBallSpeedY;
  226.  
  227.  
  228.         //Check if the ball hits either the left side or the right side of the screen
  229.         //But only do something if the ball is moving towards that side of the screen
  230.         //If it does that => change the direction of the ball in the X direction
  231.         if((mBallX <= mBall.getWidth() / 2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth() / 2 && mBallSpeedX > 0) ) {
  232.             mBallSpeedX = -mBallSpeedX;
  233.         }
  234.     //  playEnd();
  235.         //Check for SmileyBall collision
  236.         if(updateBallCollusion(mSmileyBallX, mSmileyBallY)) {
  237.             //Increase score
  238.             // Play sounds & bring balls down
  239.              
  240.             // increment the explode count
  241.             mExplodeCount = mExplodeCount +1;
  242. // play sound
  243.             mSoundPool.play(mSoundExplode, 100, 100, 1, 0, 1.0f);
  244.      
  245.                                            
  246.                         for(int i = 0; i <2; i++) {
  247.                         mSoundPool.play(mSoundMove, 100, 100, 1, 0, 1.0f);
  248.                         }
  249.                        
  250.                         // move the sad balls after each hit on SmileyBall
  251.                         moveballs();
  252.                        
  253.                                                
  254.                                     updateScore(1);
  255.                                     if (mExplodeCount > 15){
  256.                                         randomSmile();}
  257.                                    
  258.                                    
  259.             //Place Paddle in the middle of the screen
  260.             setPaddle();
  261.             // check if explode count matches magic numbers & remove sad balls accordingly
  262.            
  263.            
  264.                         for(int i = 0; i < mBallRemove.length; i++) {
  265.                            
  266.                             // check if an entry in the magic numbers matches the explode count
  267.                             if (mBallRemove[i] == mExplodeCount) {
  268.                                 // if it does set the parameter to pass to the remove ball function
  269.                                 mBallchk = mBallRemove[i];
  270.                             }
  271.                         }
  272.                         // if the remove parameter is set
  273.                         if(mBallchk >0);{
  274.                             // call the remove function passing the magic value
  275.                             removeballs(mBallchk);
  276.                             // reset parameter once it is passed otherwise you would get errors
  277.                             mBallchk = 0;
  278.                            
  279.                         }
  280.                         // if last Sad ball randomise position of smile
  281.                         if (mExplodeCount > 10){
  282.                             randomSmile();}
  283.                        
  284.                     }
  285.  
  286.        
  287.        
  288.         //Loop through all SadBalls
  289.         for(int i = 0; i < mSadBallX.length; i++) {
  290.             //Perform collisions (if necessary) between SadBall in position i and the red ball
  291.             updateBallCollusion(mSadBallX[i], mSadBallY[i]);
  292.         }
  293.  
  294.         //If the ball goes out of the top of the screen and moves towards the top of the screen =>
  295.         //change the direction of the ball in the Y direction
  296.         if(mBallY <= mBall.getWidth() / 2 && mBallSpeedY < 0) {
  297.             mBallSpeedY = -mBallSpeedY;
  298.         }
  299.  
  300.         //If the ball goes out of the bottom of the screen => lose the game
  301.         if(mBallY >= mCanvasHeight) {
  302.             // Play a sound
  303.             mSoundPool.play(mSoundLose, 100, 100, 1, 0, 1.0f);
  304.             // reset variables and balls
  305.             loseReset();
  306.             // display loose message
  307.             setState(GameThread.STATE_LOSE);
  308.         }
  309.  
  310.     }
  311.    
  312.     //Collusion control between mBall and another big ball
  313.     private boolean updateBallCollusion(float x, float y) {
  314.         //Get actual distance (without square root - remember?) between the mBall and the ball being checked
  315.         float distanceBetweenBallAndPaddle = (x - mBallX) * (x - mBallX) + (y - mBallY) *(y - mBallY);
  316.        
  317.         //Check if the actual distance is lower than the allowed => collision
  318.         if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndPaddle) {
  319.  
  320.             //Get the present velocity (this should also be the velocity going away after the collision)
  321.             float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  322.  
  323.             //Change the direction of the ball
  324.             mBallSpeedX = mBallX - x;
  325.             mBallSpeedY = mBallY - y;
  326.  
  327.             //Get the velocity after the collision
  328.             float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  329.  
  330.             //using the fraction between the original velocity and present velocity to calculate the needed
  331.             //speeds in X and Y to get the original velocity but with the new angle.
  332.             mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
  333.             mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
  334.            
  335.             return true;
  336.         }
  337.        
  338.         return false;
  339.     }
  340.     public void moveballs(){
  341.          /* this should move sad balls
  342.         from SadBalls pyramid underneath the SmileyBall
  343.         down the screen
  344.         Note I also move Balls 4 & 5 even if they are not present as this save if statements
  345.             */
  346.         mSadBallY[0] = mSadBallY[0] +5;
  347.         mSadBallY[1] = mSadBallY[1] +5;
  348.         mSadBallY[2] = mSadBallY[2] +5;
  349.         mSadBallY[2] = mSadBallY[3] +5;
  350.         mSadBallY[2] = mSadBallY[4] +5;
  351.        
  352.          
  353.             }
  354.     public void removeballs(int mBallchk){
  355.          /* this should remove sad balls
  356.         from SadBalls pyramid underneath the SmileyBall
  357.         magic numbers are checked and depending on the value passed in
  358.         the correct sad ball is removed
  359.         Last check is to remove the s
  360.         Smiley ball which is end game
  361.         It also changes position of remaining sad balls
  362.         */
  363.          
  364.          
  365.          
  366.          if (mBallchk == 5 ){
  367.             mSadBallX[0] = -100;
  368.             mSadBallY[0] = -100;
  369.             mSadBallY[1] = mSadBallY[1] +10;
  370.             mSadBallY[2] = mSadBallY[2] +10;
  371.             mSadBallY[3] = mSadBallY[2] +10;
  372.             mSadBallY[4] = mSadBallY[2] +10;
  373.                        
  374.             }
  375.          
  376.          if (mBallchk == 7 ){
  377.                 mSadBallX[0] = -100;
  378.                 mSadBallY[0] = -100;
  379.                 mSadBallX[3] = -100;
  380.                 mSadBallY[3] = -100;
  381.                 mSadBallY[1] = mSadBallY[1] +10;
  382.                 mSadBallY[2] = mSadBallY[2] +10;
  383.                 mSadBallY[4] = mSadBallY[2] +10;
  384.                            
  385.                 }
  386.            
  387.          if (mBallchk == 10){
  388.             mSadBallX[1] = -100;
  389.             mSadBallY[1] = -100;
  390.             mSadBallY[2] = mSadBallY[2] +10;
  391.             mSadBallY[4] = mSadBallY[2] +10;
  392.                 }
  393.          
  394.          if (mBallchk == 15){
  395.             mSadBallX[2] = -100;
  396.             mSadBallY[2] = -100;
  397.             mSadBallX[4] = -100;
  398.             mSadBallY[4] = -100;
  399.                         }  
  400.          
  401.          if (mBallchk == 20){
  402.                 mSmileyBallX = -100;
  403.                 mSmileyBallY = -100;
  404.                 playEnd();
  405.                 setState(GameThread.STATE_WIN);
  406.                 // increment Win Streak
  407.                 mWinStreak = mWinStreak + 1;
  408.                
  409.                 }
  410.      }
  411.  
  412. public void setPaddle(){
  413.     //Place Paddle in the middle of the screen
  414.             mPaddleX = mCanvasWidth / 2;
  415. }
  416.  
  417. public void randomSmile(){
  418.    
  419.     int rn = (int) (Math.random() * 20) +1;
  420.     mSmileyBallX = mSmileyBallX + rn;
  421.     mSmileyBallY = mSmileyBallY + rn;
  422.     if (mSmileyBallX > mCanvasWidth){
  423.         mSmileyBallX = (mCanvasWidth / rn);
  424.     }
  425.     if (mSmileyBallY > mCanvasHeight){
  426.         mSmileyBallY = (mCanvasHeight / rn);
  427.     }
  428.     }
  429. public void loseReset(){
  430.     //called by lose game
  431.     // removes extra balls that will only reappear when win streak > 0
  432.    
  433.     // reset explode count         
  434.     mExplodeCount = 0;          
  435.    
  436.     // reset WinStreak
  437.      mWinStreak = 0;
  438.     // lose extra balls
  439.     mSadBallX[3] = -100;
  440.     mSadBallY[3] = -100;
  441.     mSadBallX[4] = -100;
  442.     mSadBallY[4] = -100;
  443.    
  444. }
  445.         public void playEnd(){
  446.            
  447.                 mSoundPool.play(mSoundEnd, 1, 1, 0, 0, 1.0f);
  448.                
  449.         }
  450.        
  451.    
  452.  
  453. }
  454.  
  455. // This file is part of the course "Begin Programming: Build your first mobile game" from futurelearn.com
  456. // Copyright: University of Reading and Karsten Lundqvist
  457. // It is free software: you can redistribute it and/or modify
  458. // it under the terms of the GNU General Public License as published by
  459. // the Free Software Foundation, either version 3 of the License, or
  460. // (at your option) any later version.
  461. //
  462. // It is is distributed in the hope that it will be useful,
  463. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  464. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  465. // GNU General Public License for more details.
  466. //
  467. //
  468. // You should have received a copy of the GNU General Public License
  469. // along with it.  If not, see <http://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement