Advertisement
Guest User

Untitled

a guest
May 18th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package gal.doron.ballinthehole;
  2.  
  3. import android.content.Context;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Handler;
  7. import android.os.Looper;
  8. public class BallMover extends Thread
  9. {
  10.     Handler handler = new Handler(Looper.getMainLooper());
  11.     private Ball[] balls;
  12.     private Paddle paddle;
  13.     private GameLevels levels;
  14.     private GameView gameview;
  15.     private Context context;
  16.     private enum Directions {Top, Right, Bottom, Left, None};
  17.     public BallMover(Ball[] b, Paddle p, GameLevels levels, GameView view,Context context)
  18.     {
  19.         this.balls = b;
  20.         this.paddle = p;
  21.         this.levels = levels;
  22.         this.gameview = view;
  23.         this.context= context;
  24.     }
  25.     @Override
  26.     public void run()
  27.     {
  28.         while(true)
  29.         {
  30.             if(levels.isFinishLevel())
  31.             {
  32.                 levels.setCurrentLevel(levels.getCurrentLevel()+1);
  33.                 for(int i=1;i<balls.length;i++)
  34.                 {
  35.                     balls[i].setVisible(false);
  36.                     balls[i].restart();
  37.                 }
  38.                 balls[0].restart();
  39.                 try
  40.                 {
  41.                     sleep(1000);
  42.                 }
  43.                 catch (InterruptedException e)
  44.                 {
  45.                     e.printStackTrace();
  46.                 }
  47.             }
  48.             for(int i=0;i<balls.length;i++)
  49.             {
  50.                 if(this.balls[i].isVisible())
  51.                 {
  52.                     this.balls[i].moveBall();
  53.                     checkHitPaddle(i);
  54.                     checkHitBrick(i);
  55.                 }
  56.             }
  57.             this.gameview.postInvalidate();
  58.             try
  59.             {
  60.                 sleep(1);
  61.             }
  62.             catch (InterruptedException e)
  63.             {
  64.                 e.printStackTrace();
  65.             }
  66.         }
  67.     }
  68.     public void checkHitPaddle(int ballI)
  69.     {
  70.         Ball ball = balls[ballI];
  71.         if(ball.Left()<=paddle.Right() && ball.Right()>=paddle.Left())
  72.         {
  73.             if(ball.Bottom()>=paddle.Top())
  74.             {
  75.                 ball.bounceUp();
  76.             }
  77.         }
  78.         else if(ball.Bottom()>=paddle.Bottom()+10){
  79.            
  80.             *************************************************
  81.             //ball.restart();
  82.         }
  83.            
  84.     }
  85.    
  86.    
  87.    
  88.    
  89.    
  90.    
  91.    
  92.     public void checkHitBrick(int ballI)
  93.     {
  94.         Ball ball = balls[ballI];
  95.         String direction = getHitBricksDirection(ballI).name();
  96.         if(direction.compareTo("Top")==0)
  97.             ball.bounceUp();
  98.         if(direction.compareTo("Bottom")==0)
  99.             ball.bounceDown();
  100.         if(direction.compareTo("Left")==0)
  101.             ball.bounceLeft();
  102.         if(direction.compareTo("Right")==0)
  103.             ball.bounceRight();
  104.     }
  105.     public Directions getHitBricksDirection(int ballI)
  106.     {
  107.         Ball ball = balls[ballI];
  108.         Bricks bricks = levels.getLevelBricks();
  109.         Brick brick;
  110.         for(int i=0;i<bricks.rows();i++)
  111.         {
  112.             for(int j=0;j<bricks.cols();j++)
  113.             {
  114.                 brick = bricks.getBrick(i, j);
  115.                 if(brick.getType()!=0)
  116.                 {
  117.                     if(ball.Left()<=brick.Right() && ball.Right()>=brick.Left())
  118.                     {
  119.                         if(ball.Bottom()-brick.Top()>=0 && ball.Bottom()-brick.Top()<=1)
  120.                         {
  121.                             brick.ballHitBrick();
  122.                             return Directions.Top;
  123.                         }
  124.                         if(ball.Top()-brick.Bottom()<=0 && ball.Top()-brick.Bottom()>=-1)
  125.                         {
  126.                             brick.ballHitBrick();
  127.                             return Directions.Bottom;
  128.                         }
  129.                     }
  130.                     if(ball.Top()<=brick.Bottom() && ball.Bottom()>=brick.Top())
  131.                     {
  132.                         if(ball.Right()-brick.Left()>=0 && ball.Right()-brick.Left()<=1)
  133.                         {
  134.                             brick.ballHitBrick();
  135.                             return Directions.Left;
  136.                         }
  137.                         if(ball.Left()-brick.Right()<=0 && ball.Left()-brick.Right()>=-1)
  138.                         {
  139.                             brick.ballHitBrick();
  140.                             return Directions.Right;
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.         return Directions.None;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement