Advertisement
xerpi

Ball class JAVA

Sep 13th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1.             public class Ball{
  2.                 public Random random = new Random();
  3.                 public double ang,x,y,incX,incY,vel;
  4.                 public Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
  5.                 public double w = img.getWidth();
  6.                 public double h = img.getHeight();
  7.                 public double inc = 0.01;
  8.                 public void setRandomAngle(){
  9.                     this.ang = this.random.nextDouble()*Math.PI*2;
  10.                 }
  11.                 public void calcIncVars(){
  12.                     this.incX = Math.cos(this.ang);
  13.                     this.incY = Math.sin(this.ang);
  14.                 }
  15.                 public void serve(){
  16.                     this.x = 240-this.img.getWidth();
  17.                     this.y = 854/2-this.img.getHeight();
  18.                     this.setRandomAngle();
  19.                     this.calcIncVars();
  20.                 }
  21.                 public void move(){
  22.                     this.vel = this.vel + this.inc;
  23.                     this.x = this.x +this.incX*this.vel;
  24.                     this.y = this.y +this.incY*this.vel;
  25.                     checkScreenCollisions();
  26.                 }
  27.                 public void checkScreenCollisions(){
  28.                     if ( this.x + this.w >= 480 || this.x <=0){
  29.                         this.incX = -this.incX;
  30.                     }
  31.                     if ( this.y + this.h >= 854 || this.y <=0){
  32.                         this.incY = -this.incY;
  33.                     }
  34.                 }
  35.                 public void blit(Canvas canvas,Paint mPaint){
  36.                     canvas.drawBitmap(this.img, (float) this.x,(float) this.y,mPaint);
  37.                 }
  38.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement