Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ball{
- public Random random = new Random();
- public double ang,x,y,incX,incY,vel;
- public Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
- public double w = img.getWidth();
- public double h = img.getHeight();
- public double inc = 0.01;
- public void setRandomAngle(){
- this.ang = this.random.nextDouble()*Math.PI*2;
- }
- public void calcIncVars(){
- this.incX = Math.cos(this.ang);
- this.incY = Math.sin(this.ang);
- }
- public void serve(){
- this.x = 240-this.img.getWidth();
- this.y = 854/2-this.img.getHeight();
- this.setRandomAngle();
- this.calcIncVars();
- }
- public void move(){
- this.vel = this.vel + this.inc;
- this.x = this.x +this.incX*this.vel;
- this.y = this.y +this.incY*this.vel;
- checkScreenCollisions();
- }
- public void checkScreenCollisions(){
- if ( this.x + this.w >= 480 || this.x <=0){
- this.incX = -this.incX;
- }
- if ( this.y + this.h >= 854 || this.y <=0){
- this.incY = -this.incY;
- }
- }
- public void blit(Canvas canvas,Paint mPaint){
- canvas.drawBitmap(this.img, (float) this.x,(float) this.y,mPaint);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement