Advertisement
Katja1973

Untitled

Nov 23rd, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 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 = 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.     //Will store the image of a paddle
  23.     private Bitmap mPaddle;
  24.    
  25.     //The x position of the paddle on the screen (middle of paddle)
  26.     private float mPaddleX = 0;
  27.     //Y-AKSELIA EI OLE MÄÄRITELTY!
  28.    
  29.     //Will store the image of the big ball
  30.     private Bitmap mBigBall;
  31.    
  32.     //The X and Y position of the bigball on the screen (middle of ball) before game starts
  33.     private float mBigBallX = 0;
  34.     private float mBigBallY = 0;
  35.    
  36.     //Will store minimum distance betw. the ball centers and paddle+ball center
  37.  
  38.     private float mMinDistanceBetweenRedBallAndPaddle = 0; //tämä itse lisätty...
  39.     private float mMinDistanceBetweenRedBallAndBigBall = 0; //tämä itse lisätty...
  40.  
  41.     //This is run before anything else, so we can prepare things here
  42.     public TheGame(GameView gameView) {
  43.         //House keeping
  44.         super(gameView);
  45.        
  46.         //Prepare the image of the ball so we can draw it on the screen (using a canvas)
  47.         mBall = BitmapFactory.decodeResource
  48.                 (gameView.getContext().getResources(),
  49.                 R.drawable.small_red_ball);
  50.         //Prepare the image of the paddle so we can draw it on the screen (using a canvas)
  51.         mPaddle = BitmapFactory.decodeResource
  52.                 (gameView.getContext().getResources(),
  53.                 R.drawable.yellow_ball);
  54.         //Prepare the image of the big ball so we can draw it on the screen (using a canvas)
  55.         mBigBall = BitmapFactory.decodeResource
  56.                 (gameView.getContext().getResources(),
  57.                 R.drawable.smiley_ball);
  58.    
  59.     }
  60.    
  61.     //This is run before a new game (also after an old game)
  62.     @Override
  63.     public void setupBeginning() {
  64.         //Initialise speeds
  65.         mBallSpeedX = -100;
  66.         mBallSpeedY = -100;
  67.        
  68.         //Place the ball in the middle of the screen.
  69.         //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
  70.         mBallX = mCanvasWidth / 2;
  71.         mBallY = mCanvasHeight / 2;
  72.        
  73.         //Place the paddle in the middle of below canvas edge
  74.         mPaddleX = mCanvasWidth / 2; //Y-aks määritykset ei vaikuta mihinkään suuntaan.
  75.        
  76.         //Place the big ball in the middle of upper edge.
  77.        
  78.         //mBall.Width() and mBall.getHeigh() gives us the height and width of the image of the ball
  79.         mBigBallX = mCanvasWidth / 2;
  80.         mBigBallY = mCanvasHeight = 1; //tarttetaanko tätä? tämä vei smileyn yläosaan. mutta paddle samoin siirtyi.
  81.        
  82.         //collision detection with red and big ball
  83.         mMinDistanceBetweenRedBallAndBigBall = (mBigBall.getWidth() / 2 + mBall.getWidth() / 2) * (mBigBall.getWidth() / 2 + mBall.getWidth() / 2);
  84.     //ITSE LISÄTTy - PUUTTUNEE koodia TÖRMÄYSLASKUISTA SMILEYn OSALTA
  85.         mMinDistanceBetweenRedBallAndPaddle = (mPaddle.getWidth() / 2 + mBall.getWidth() / 2) * (mPaddle.getWidth() / 2 + mBall.getWidth() / 2);
  86.    
  87.     }
  88.        
  89.  
  90.     @Override
  91.     protected void doDraw(Canvas canvas) {
  92.         //If there isn't a canvas to draw on do nothing
  93.         //It is ok not understanding what is happening here
  94.         if(canvas == null) return;
  95.        
  96.         super.doDraw(canvas);
  97.        
  98.         //draw the image of the ball using the X and Y of the ball
  99.         //drawBitmap uses top left corner as reference, we use middle of picture
  100.         //null means that we will use the image without any extra features (called Paint)
  101.         canvas.drawBitmap(mBall, mBallX - mBall.getWidth() / 2, mBallY - mBall.getHeight() / 2, null);
  102.         //draw the image of the paddle using the X of the paddle
  103.         canvas.drawBitmap(mPaddle, mPaddleX - mPaddle.getWidth() / 2, mCanvasHeight - mPaddle.getHeight() / 2, null);
  104.         //null means that we will use the image without any extra features (called Paint)
  105.         //VAIHDETTU YHDENMUKAISEKSI - JÄLKILAUSEESEEN EKAKSI MUUTTUJAKSI KORKEUS LEVEYDEN SIJAAN
  106.         canvas.drawBitmap(mBigBall, mBigBallX - mBigBall.getWidth() / 2,  mCanvasHeight - mBigBall.getHeight() / 2, null);
  107.  
  108.     }
  109.    
  110.     //This is run whenever the phone is touched by the user
  111.     @Override
  112.     protected void actionOnTouch(float x, float y) {
  113.         mPaddleX = x - mPaddle.getWidth() / 2;
  114.        
  115.         //Increase/decrease the speed of the ball making the ball move towards the touch
  116.         mBallSpeedX = x - mBallX;
  117.         mBallSpeedY = y - mBallY;
  118.     }
  119.    
  120.     //This is run whenever the phone moves around its axises
  121.     @Override
  122.     protected void actionWhenPhoneMoved(float xDirection, float yDirection, float zDirection) {
  123.        
  124.         if(mPaddleX >= 0 && mPaddleX <= mCanvasWidth){
  125.         mPaddleX = mPaddleX - xDirection;
  126.        
  127.         if(mPaddleX < 0) mPaddleX = 0;
  128.         if(mPaddleX > mCanvasWidth) mPaddleX = mCanvasWidth;
  129.        
  130.         } //TÄSTÄ POISTETTU PÄTKÄ NOPEUKSISTA
  131.     }
  132.    
  133.     //This is run just before the game "scenario" is printed on the screen
  134.     @Override
  135.     protected void updateGame(float secondsElapsed)
  136.     //make sure the ball does not go outside the canvas & including its size
  137.     {
  138.         if((mBallX <= mBall.getWidth()/2 && mBallSpeedX < 0) || (mBallX >= mCanvasWidth - mBall.getWidth()/2 && mBallSpeedX > 0)) {
  139.             mBallSpeedX = -mBallSpeedX;
  140.         }  
  141.        
  142.         {
  143.             if((mBallY <= mBall.getHeight()/2 && mBallSpeedY < 0) || (mBallY >= mCanvasHeight - mBall.getHeight()/2 && mBallSpeedY > 0)) {
  144.                 mBallSpeedY = -mBallSpeedY;
  145.             }
  146.                
  147.             float distanceBetweenBallAndPaddle = 0;
  148.             //TÄMÄ LISÄTTY kpl 3.6 - ei toimi vielä....
  149.             if(mBallSpeedY > 0) {  
  150.                         distanceBetweenBallAndPaddle =
  151.                                 (mPaddleX - mBallX) * (mPaddleX - mBallX) +
  152.                                 (mCanvasHeight - mBallY) * (mCanvasHeight - mBallY);
  153.             if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndPaddle) {
  154.                 float velocityOfBall = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  155.                 mBallSpeedX = mBallX - mPaddleX;
  156.                 mBallSpeedY = mBallY - mCanvasHeight;
  157.                
  158.                 float newVelocity = (float) Math.sqrt(mBallSpeedX*mBallSpeedX + mBallSpeedY*mBallSpeedY);
  159.                
  160.                
  161.                 mBallSpeedX = mBallSpeedX * velocityOfBall / newVelocity;
  162.                 mBallSpeedY = mBallSpeedY * velocityOfBall / newVelocity;
  163.             } //TÄMÄ LISÄTTY kpl 3.6 - ei toimi vielä....
  164.             }
  165.                        
  166.                         float distanceBetweenBallAndBall = 0;
  167.             if(mMinDistanceBetweenRedBallAndBigBall >= distanceBetweenBallAndBall) {
  168.                         //TODO change mBallSpeedX and mBallSpeedY
  169.             }  
  170.        
  171.         //Move the ball's X and Y using the speed (pixel/sec)
  172.         mBallX = mBallX + secondsElapsed * mBallSpeedX;
  173.         mBallY = mBallY + secondsElapsed * mBallSpeedY;
  174.     }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement