Android Open Source - BounceMe Ball






From Project

Back to project page BounceMe.

License

The source code is released under:

MIT License

If you think the Android project BounceMe listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.bounceme;
//w w w .  j  a v  a 2s  .  c  o  m
import android.graphics.Canvas;
import android.graphics.Paint;

public class Ball {
  private int radius = 20;
  private final double gravity = .1;
  private int startX;
  private int startY;
  private double yacc = gravity;
  private double xacc = 0;
  private double xspeed = 0;
  private double yspeed = 0;
  private int xpos;
  private int ypos;
  private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  private Paint mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
  private int canvasWidth;
  private int canvasHeight;
  
  
  public Ball(int x , int y){
    xpos = startX = x;
    ypos = startY = y;
    mPaint.setColor(0xFF66BD4A);
    mPaint2.setColor(0xFFF2642F);
  }
  
  public void draw(Canvas can){
    can.drawCircle(xpos, ypos, radius + 5, mPaint2);
    can.drawCircle(xpos, ypos, radius, mPaint);
    xacc = 0;
    yacc = gravity;
  }
  
  public boolean update(int width, int height){
    //update the speeed based on the velocity
    xspeed += xacc;
    yspeed += yacc;
    
    //update the position based on the velocity
    xpos += xspeed;
    ypos += yspeed;
    
    
    
    
    canvasWidth = width;
    canvasHeight = height;
    
    if (xpos > canvasWidth || ypos > canvasHeight){
      return false;
    }
    
    //setting terminal velocity in the x and y planes
    if (xspeed > 10){ 
      xspeed = 10;
    }else if(xspeed < -10){
      xspeed = -10;
    }
    
    if (yspeed > 10){ 
      yspeed = 10;
    }else if(yspeed < -10){
      yspeed = -10;
    }
    
    
    return true;
  }
  
  //how to ball moves when it is touched
  public void onTouch(int x){
    if(x == -1){
      xacc = -.1;
    }else if(x == -2){
      xacc = -.2;
    }else if(x == 1){
      xacc = .1;
    }else if(x == 2){
      xacc = .2;
    }else{
      xacc = 0;
    }
  }
  
  public void restart(){
    xpos = startX;
    ypos = startY;
    yspeed = 0;
    xspeed = 0;
    yacc = 0;
    xacc = 0;
  }
  
  public void setAcc(double xAdd, double yAdd){
    xacc += xAdd;
    yacc += yAdd;
  }
  
  public int getX(){
    return xpos;
  }
  
  public int getY(){
    return ypos;
  }
  
  public int getRadius(){
    return radius;
  }
}




Java Source Code List

com.cse3345.f13.martin.Ball.java
com.cse3345.f13.martin.CreditActivity.java
com.cse3345.f13.martin.Goal.java
com.cse3345.f13.martin.LevelGen.java
com.cse3345.f13.martin.LevelPicker.java
com.cse3345.f13.martin.Level.java
com.cse3345.f13.martin.MenuActivity.java
com.cse3345.f13.martin.PlaySurfaceView.java
com.cse3345.f13.martin.SetActivity.java
com.cse3345.f13.martin.Sling.java
com.cse3345.f13.martin.TutActivity.java
com.cse3345.f13.martin.Wall.java
com.cse3345.f13.martin.WinActivity.java
com.example.bounceme.Ball.java
com.example.bounceme.CreditActivity.java
com.example.bounceme.Goal.java
com.example.bounceme.GridAdapter.java
com.example.bounceme.Level.java
com.example.bounceme.MenuActivity.java
com.example.bounceme.PlaySurfaceView.java
com.example.bounceme.SetActivity.java
com.example.bounceme.Sling.java
com.example.bounceme.WinActivity.java
com.example.bounceme.levelGen.java
com.example.bounceme.levelPicker.java