Mr. Rogers' IB Computer Science - Google Android SmartPhone Programming Project
Help boost K-12 science, math, and computer education. Donate your obsolete Android phone to us (see below)! We promise to put it to good use in a classroom.

Mr Rogers Android Project Menu




   3D Compass

   3D Accelerometer

   Friction Tester


   Spherical Game



Greenville, SC



Spherical: A New Game for Android Devices


This application is a game that utilizes the android touch screen and gyroscope. The object of the game is to collect as many golden pieces as possible. Each piece is 10 points. When a piece is collected, another one appears in another random part of the screen. Collecting the pieces must be done by touching the piece with either of the two white spheres. One of the spheres is controlled by tilting the screen where you want the sphere to roll. The other moves towards wherever you touch the screen. However, there will be a constantly bouncing grey sphere. If the grey sphere hits either one of the two white spheres, the game is over. The game starts when the screen is touched.


This is a screenshot of the game. The yellow circle is the golden piece. The farthest right of the 3 larger spheres is the grey sphere. The other two large spheres are user-controlled. Part of the challenge is remembering which sphere is controlled by touch and which by tilt.


The Hardwares Class creates the program and the pause menu for selecting difficulty. The Panel class creates the GUI, the sphere objects, and controls the operations of the game. The Circle class provides a template for the objects of the game.

Hardwares Class





import android.content.DialogInterface;

import android.os.Bundle;

import android.view.Menu;

/**Starts the program and creates a difficulty menu


* @Akshay Chandrasekhar    

* 10/23/10


public class Hardwares extends Activity {


        public Panel p;


   public void onCreate(Bundle savedInstanceState) {


           p = new Panel(this);






















Creates and starts the program by instantiating the Panel class

   public boolean onPrepareOptionsMenu(Menu m) {


           p.adb2 = new AlertDialog.Builder(this);

           p.adb2.setMessage("What's your game?");

               p.adb2.setNegativeButton("Insane", new DialogInterface.OnClickListener() {                            

        public void onClick(DialogInterface dialog, int which) {

                   p.difficulty = "Hard";




             p.adb2.setPositiveButton("Easy", new

DialogInterface.OnClickListener() {

             public void onClick(DialogInterface dialog, int id) {

                     p.difficulty = "Easy";




             p.adb2.setNeutralButton("Medium", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {

                   p.difficulty = "Medium";




             AlertDialog adg = p.adb2.create();



           return true;



Creates a pause menu which lets the user choose the difficulty of the game.


The OnPrepareOptionsMenu(Menu m) method is called when the menu button on the phone is pressed. The OnCreate(Bundle savedInstanceState) is automatically called when the program starts.


Panel Class*

(*for reference, the sphere moved by the screen’s tilt is the first sphere, the sphere moving towards a point touched on the screen is the second sphere, and the grey sphere is the bouncing sphere)





import android.content.Context;

import android.content.DialogInterface;






import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.CountDownTimer;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

/**Draws, moves, and controls all parts of GUI


* @Akshay Chandrasekhar

* 10/23/10



public class Panel extends View implements OnTouchListener,SensorEventListener{

   private Panel yes;

   private CountDownTimer count;

   private int z = 0, score = 0;

   public boolean brighten = false, over = false, start = false, pause=false;

   Paint p = new Paint();

   public Circle [] c = new Circle[4];    

   public float [] f = new float[4];

   public int x2 = 0, y2 = 0;

   public float dx, dy;

   PointF co;

   AlertDialog.Builder adb, adb2;

   public Activity act;

   public String e = "Easy",m="Medium", h="Hard",            difficulty="Medium";



   public Panel(Context context) {


           act = (Activity)context;





           count = new CountDownTimer(20, 1) {

                   public void onTick(long f) {}

                   public void onFinish() {







           SensorManager m = (SensorManager) this.getContext().getSystemService(Context.SENSOR_SERVICE);

           Sensor s = m.getDefaultSensor(Sensor.TYPE_ORIENTATION);

           m.registerListener(this, s, SensorManager.SENSOR_DELAY_FASTEST);

           c[0] = new Circle(20,50,250);

           c[1] = new Circle(20,100,100);

           c[2] = new Circle(20,0,0);

           c[3] = new Circle(10,0,0);

           for(int j=0; j<f.length; j++)

                   f[j] = 0;





























Creates all fields
























Creates timer that repaints the program








initializes some fields

   public boolean onTouch(View v, MotionEvent m) {

           if(m.getAction() == MotionEvent.ACTION_DOWN) {

                   co = new PointF(m.getX(), m.getY());





           if(m.getAction() == MotionEvent.ACTION_UP)


           if(m.getAction() == MotionEvent.ACTION_MOVE) {



           return true;    



Gets point touched and sets it as destination for the second ballsphere


The screen starts brightening



Starts darkening



Game will start

public void onDraw(Canvas canvas) {


           if (over)


           else {







           x2= getWidth();

           y2 = getHeight();    

           int k = Color.rgb(z, z,z);


           int f = Color.rgb(255, 255, 255);


           if(start) {



           canvas.drawCircle(c[0].x, c[0].y, 20,p);




           canvas.drawCircle(c[3].x,c[3].y,c[3].r, p);

           f=Color.rgb(192, 192, 192);


           canvas.drawCircle(c[2].x,c[2].y, 20, p);

           if(start) {














Checks if game is over



sets the speed of the bouncing sphere according to difficulty





Draws background







Moves first sphere


Draws objects on screen








If coordinate of point on screen touched is within range of sphere, it moves the second sphere




If the game isn’t paused, the bouncing sphere keeps moving, the brightness is changing, and checks are performed to see if the game is over or a piece is collected

public void restart() {



public void moove() {

           if(c[0].x>=20 && (c[0].x+20)<=x2)







           if(c[0].y>=20 && (c[0].y+20)<=y2)







public void restart2() {





           c[2].x=(float) (Math.random()*x2);

           c[2].y = (float)(Math.random()*y2);

           c[3].x= (float)(Math.random()*(getWidth()-c[3].r));








           if(co!=null) {






public void bright() {

           if(brighten && !(z==255))






Restarts timer



Moves the first sphere according to tilt














Resets all values when the game restarts




















Brightens the screen slowly to white if the screen is being touched. Fades to black when the screen is not touched.




public void moveTo() {





public void check() {

           PointF coor = new PointF(c[2].x+20, c[2].y+20);

           if(Math.sqrt(Math.pow(((c[1].x+20)-coor.x), 2) + Math.pow(((c[1].y+20)-coor.y),2))<37)


           if(Math.sqrt(Math.pow(((c[0].x+20)-coor.x), 2) + Math.pow(((c[0].y+20)-coor.y),2))<37)




   public void check2() {

           PointF coor2 = new PointF(c[3].x+10, c[3].y+10);

           if(Math.sqrt(Math.pow(((c[1].x+20)-coor2.x), 2) + Math.pow(((c[1].y+20)-coor2.y),2))<37) {





           if(Math.sqrt(Math.pow(((c[0].x+20)-coor2.x), 2) + Math.pow(((c[0].y+20)-coor2.y),2))<37) {






   public void onAccuracyChanged(Sensor sensor, int accuracy) {



   public void onSensorChanged(SensorEvent event) {                     f[0]=(float)(Math.sin(Math.toRadians(event.values[2]))*-9.8*1.5);            f[1]=(float)(Math.sin(Math.toRadians(event.values[1]))*-9.8*1.5);



   public void onSizeChanged(int oldwidth, int oldheight, int newwidth, int newheight) {

           c[2].x= (float)(Math.random()*(getWidth()-c[2].r));


           c[3].x= (float)(Math.random()*(getWidth()-c[3].r));




public void over() {

           adb = new AlertDialog.Builder(act);

           adb.setMessage(difficulty + " Score: " + score);

               adb.setPositiveButton("Restart", new DialogInterface.OnClickListener() {                            

           public void onClick(DialogInterface dialog, int which) { act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);






             adb.setNegativeButton("Quit", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int id) {




             AlertDialog adg = adb.create();

             adg.setTitle("GAME OVER");





Moves the second sphere incrementally towards a point set when the screen is touched.


Checks if either white sphere is hit by the bouncing sphere.









Checks if either white sphere collects a golden piece.


















Gets the values of the tilt of the screen






Initializes the positions of the bouncing sphere and golden piece






When the game is over, this creates a dialog which lets the user restart or quit the game.

The countdown timer constantly updates the GUI by callling the onDraw(Canvas canvas) method repeatedly which in turn calls methods like check(), check2(), and bright(). When the screen is touched, the onTouch(View v, MotionEvent m)  method is called. Similarly, when the tilt of the screen is changed in any of the 3 axes, the onSensorChanged(SensorEvent event) method is called. Finally the onSizeChanged(int oldwidth, int oldheight, int newwidth, int newheight) method is called to initialize two of the circles because in this method, getWidth() and getHeight() are first defined.


Circle Class


/**Creates a circle object and a method to have the circle

* rebound

* @Akshay Chandrasekhar

* 10/23/10


public class Circle {


   public float x;

   public float y;

   public int changex;

   public int changey;

   public int r;

   public boolean hitx, hity;

   public float amount;


   public Circle(int radius, float a, float b) {














Initializes fields for circles








Creates a Circle object with a radius, an x, and a y position.

public void rebound(int w, int h) {



















Moves the bouncing sphere which continues on its path until it hits a boundary of the phone. It then simply rebounds and continues.


All 4 objects are Circle objects. Only the grey sphere utilizes the rebound method.



Warning: include(footerMrRstudentsAndroid.htm): failed to open stream: No such file or directory in /home/intuitor/ Phone Site/AkshayAndroid1.php on line 685

Warning: include(): Failed opening 'footerMrRstudentsAndroid.htm' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/intuitor/ Phone Site/AkshayAndroid1.php on line 685