Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.94 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.view.MotionEvent;
  8.  
  9. public class TheGame extends GameThread {
  10.  
  11.     //Will store the image of a ball
  12.     private Bitmap mBall;
  13.  
  14.     //The X and Y position of the ball on the screen (middle of ball)
  15.     private float mBallX = -100;
  16.     private float mBallY = -100;
  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.     // declaramos la raqueta que va a golpear la bola
  23.     private Bitmap mPaddle;
  24.  
  25.  
  26.  
  27.     //y esto declara que hara que aparezca abajo, y solo para moverse de izquierda a derecha.
  28.     private float mPaddleX = 0;
  29.  
  30.     //y con esto la velocidad con la que se movera
  31.     private float mPaddleSpeedX = 0;
  32.  
  33.     private float mMinDistanceBetweenBallAndPaddle;
  34.     private float mMinDistanceBetweenBallAndSmiley;
  35.  
  36.  
  37.     private Bitmap mSmiley;
  38.     private Bitmap mUnSmiley;
  39.     private float mSmileyX = 0;
  40.     private float mSmileyY = 0;
  41.  
  42.  
  43.  
  44.     //This is run before anything else, so we can prepare things here
  45.     public TheGame(GameView gameView) {
  46.         //House keeping
  47.         super(gameView);
  48.  
  49.         //Prepare the image so we can draw it on the screen (using a canvas)
  50.         mBall = BitmapFactory.decodeResource
  51.                 (gameView.getContext().getResources(),
  52.                         R.drawable.small_red_ball);
  53.         mPaddle = BitmapFactory.decodeResource
  54.                 (gameView.getContext().getResources(),
  55.                         R.drawable.yellow_ball);
  56.         mSmiley = BitmapFactory.decodeResource
  57.                 (gameView.getContext().getResources(),
  58.                         R.drawable.smiley_ball);
  59.         mUnSmiley = BitmapFactory.decodeResource
  60.                 (gameView.getContext().getResources(),
  61.                         R.drawable.sad_ball);
  62.  
  63.     }
  64.  
  65.     //This is run before a new game (also after an old game)
  66.     @Override
  67.     public void setupBeginning() {
  68.         //Initialise speeds
  69.         mBallSpeedX = mCanvasWidth / 3;
  70.         mBallSpeedY = mCanvasHeight / 3;
  71.  
  72.         //Place the ball in the middle of the screen.
  73.         //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
  74.         mBallX = mCanvasWidth / 2;
  75.         mBallY = mCanvasHeight / 2;
  76.  
  77.         mPaddleX = mCanvasWidth / 2;
  78.         mPaddleSpeedX = 0;
  79.  
  80.  
  81.         mSmileyX = mCanvasWidth / 2;
  82.         mSmileyY = 200;
  83.  
  84.         mMinDistanceBetweenBallAndPaddle = ((mPaddle.getWidth() / 2) + (mBall.getWidth() / 2)) * ((mPaddle.getWidth() / 2) + (mBall.getWidth() / 2));
  85.         mMinDistanceBetweenBallAndSmiley = ((mSmiley.getWidth() / 2) + (mBall.getWidth() / 2)) * ((mSmiley.getWidth() / 2) + (mBall.getWidth() / 2));
  86.  
  87.     }
  88.  
  89.     @Override
  90.     protected void doDraw(Canvas canvas) {
  91.         //If there isn't a canvas to draw on do nothing
  92.         //It is ok not understanding what is happening here
  93.         if(canvas == null) return;
  94.  
  95.         super.doDraw(canvas);
  96.  
  97.         //draw the image of the ball using the X and Y of the ball
  98.         //drawBitmap uses top left corner as reference, we use middle of picture
  99.         //null means that we will use the image without any extra features (called Paint)
  100.         canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null);
  101.  
  102.         canvas.drawBitmap(mPaddle, mPaddleX - mPaddle.getWidth() / 2, mCanvasHeight - mPaddle.getHeight() / 2, null);
  103.  
  104.         canvas.drawBitmap(mSmiley, mSmileyX - mSmiley.getWidth() / 2, mSmileyY - mSmiley.getHeight() / 2, null);
  105.  
  106.  
  107.     }
  108.  
  109.     //This is run whenever the phone is touched by the user
  110.     @Override
  111.     protected void actionOnTouch(float x, float y) {
  112.         mPaddleX = x;
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.     //This is run whenever the phone moves around its axises
  119.     @Override
  120.     protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
  121.  
  122.         mPaddleSpeedX = mPaddleSpeedX + 70f * xDirection;
  123.  
  124.         if (mPaddleX <= 0 && mPaddleSpeedX < 0)
  125.         { mPaddleSpeedX = 0;
  126.             mPaddleX = 0;}
  127.  
  128.  
  129.         if (mPaddleX >= mCanvasWidth && mPaddleSpeedX > 0) {
  130.  
  131.             mPaddleSpeedX = 0;
  132.  
  133.             mPaddleX = mCanvasWidth;
  134.         }
  135.  
  136.  
  137.     }
  138.  
  139.     //This is run just before the game "scenario" is printed on the screen
  140.     @Override
  141.  
  142.  
  143.     protected void updateGame(float secondsElapsed) {
  144.         float distanceBetweenBallAndPaddle;
  145.         float distanceBetweenBallAndSmiley;
  146.  
  147.         if (mBallSpeedY > 0) {
  148.             distanceBetweenBallAndPaddle = (mPaddleX - mBallX) * (mPaddleX - mBallX) + (mCanvasHeight - mBallY) * (mCanvasHeight - mBallY);
  149.  
  150.             if (mMinDistanceBetweenBallAndPaddle >= distanceBetweenBallAndPaddle ) {
  151.                 float speedOfBall = (float) Math.sqrt(mBallSpeedX * mBallSpeedX + mBallSpeedY * mBallSpeedY);
  152.  
  153.                 mBallSpeedY = mBallY - mCanvasHeight;
  154.                 mBallSpeedX = mBallX - mPaddleX;
  155.  
  156.                 float newSpeedOfBall = (float) Math.sqrt(mBallSpeedX * mBallSpeedX + mBallSpeedY * mBallSpeedY);
  157.  
  158.                 mBallSpeedX = mBallSpeedX * speedOfBall / newSpeedOfBall;
  159.                 mBallSpeedY = mBallSpeedY * speedOfBall / newSpeedOfBall;
  160.                 updateScore(1);
  161.  
  162.             }
  163.  
  164.         }
  165.  
  166.         distanceBetweenBallAndSmiley = (mSmileyX - mBallX) * (mSmileyX - mBallX) + (mSmileyY - mBallY) * (mSmileyY - mBallY);
  167.  
  168.         if (mMinDistanceBetweenBallAndSmiley >= distanceBetweenBallAndSmiley) {
  169.             //Get the present speed
  170.             float speedOfBall = (float) Math.sqrt(mBallSpeedX * mBallSpeedX + mBallSpeedY * mBallSpeedY);
  171.             //change the direction of the ball
  172.             mBallSpeedX = mBallX - mSmileyX;
  173.             mBallSpeedY = mBallY - mSmileyY;
  174.  
  175.             float newSpeedOfBall = (float) Math.sqrt(mBallSpeedX * mBallSpeedX + mBallSpeedY * mBallSpeedY);
  176.  
  177.             mBallSpeedX = mBallSpeedX * speedOfBall / newSpeedOfBall;
  178.             mBallSpeedY = mBallSpeedY * speedOfBall / newSpeedOfBall;
  179.             mSmiley = changeSmileyFace(mSmiley);
  180.             updateScore(5);
  181.         }
  182.  
  183.         //Move the ball's X and Y using the speed (pixel/sec)
  184.         mBallX = mBallX + secondsElapsed * mBallSpeedX;
  185.         mBallY = mBallY + secondsElapsed * mBallSpeedY;
  186.  
  187.         if ((mBallX <= mBall.getWidth()/2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2 && mBallSpeedX > 0)) {
  188.             mBallSpeedX = -mBallSpeedX;
  189.         }
  190.  
  191.         if ((mBallY <= mBall.getHeight()/2 && mBallSpeedY < 0) ) {
  192.             mBallSpeedY = -mBallSpeedY;
  193.         }
  194.  
  195.         mPaddleX = mPaddleX + secondsElapsed * mPaddleSpeedX;
  196.  
  197.         if (mBallY >= mCanvasHeight) {
  198.             setState(GameThread.STATE_LOSE);
  199.         }
  200.  
  201.     }
  202.  
  203.     public Bitmap changeSmileyFace(Bitmap mSmiley) {
  204.         mSmiley = mUnSmiley;
  205.         return mSmiley;
  206.     }
  207.  
  208.     public void setmSmiley(Bitmap mSmiley) {
  209.         this.mSmiley = mSmiley;
  210.     }
  211.  
  212.     public void setmSmileyX(float mSmileyX) {
  213.         this.mSmileyX = mSmileyX;
  214.     }
  215. }
  216.  
  217.  
  218. // This file is part of the course "Begin Programming: Build your first mobile game" from futurelearn.com
  219. // Copyright: University of Reading and Karsten Lundqvist
  220. // It is free software: you can redistribute it and/or modify
  221. // it under the terms of the GNU General Public License as published by
  222. // the Free Software Foundation, either version 3 of the License, or
  223. // (at your option) any later version.
  224. //
  225. // It is is distributed in the hope that it will be useful,
  226. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  227. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  228. // GNU General Public License for more details.
  229. //
  230. //
  231. // You should have received a copy of the GNU General Public License
  232. // along with it.  If not, see <http://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement