Tuesday, 26 May 2015

Ninjasmack source code

This is the source code for the game "Ninjasmack" that I made.

Check out the app here : https://play.google.com/store/apps/details?id=com.psiuol21.smashstack&hl=en

(This is only the java files)

First Acitivity.java

package com.example.smashstack;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.psiuol21.smashstack.R;

public class FirstAct extends ActionBarActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
   getActionBar().hide();
setContentView(R.layout.activity_first);
tv=(TextView)findViewById(R.id.textView1);
   Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/FONTX.TTF");
   tv.setTypeface(mFont);
   tv.setTextColor(Color.parseColor("#FFF000"));
   tv.setText("NINJASMACK");


}

public void enter(View v)
{
Intent intent=new Intent(FirstAct.this,MainActivity.class);
startActivity(intent);
finish();


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}



}






MainActivity.java


package com.example.smashstack;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.psiuol21.smashstack.R;

public class MainActivity extends ActionBarActivity {

private float x1,x2,y1,y2;
RelativeLayout root;
int count=0;
int cuts[]=new int[] {0,0,0,0};
ImageView down,up,left,right;
MediaPlayer mediaPlayer;
final Handler handler= new Handler();
long starttime;
long remainingTime=12000;
Runnable runnable;
static final int MIN_DISTANCE = 150;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
   getActionBar().hide();
setContentView(R.layout.org);
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.start();
down=(ImageView)findViewById(R.id.down);
up=(ImageView)findViewById(R.id.up);
left=(ImageView)findViewById(R.id.left);
right=(ImageView)findViewById(R.id.right);
Thread thread = new Thread() {
   @Override
   public void run() {
       try {
       
           while(true) {
            Random rand = new Random();
            int randomNum = rand.nextInt((4-0));    
            while(cuts[0]==1 || cuts[1]==1 || cuts[2]==1 || cuts[3]==1);
          if(cuts[0]==0 && cuts[1]==0 && cuts[2]==0 && cuts[3]==0)
          {
          cuts[randomNum]=1;
         
          }
          runOnUiThread(new Runnable() {
    @Override
    public void run() {
     if(cuts[0]==1)
    right.setVisibility(View.VISIBLE);
     else if(cuts[1]==1)
     left.setVisibility(View.VISIBLE);
     else if(cuts[2]==1)
     down.setVisibility(View.VISIBLE);
     else if(cuts[3]==1)
     up.setVisibility(View.VISIBLE);

//stuff that updates ui

   }
});
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
     
   }
};
thread.start();
 

runnable=new Runnable() {
    @Override
    public void run() {
   
   
     
        // Do something after 5s = 5000ms
    // mediaPlayer.release();
    // mediaPlayer=null;
    Intent intent=new Intent(MainActivity.this,LastActivity.class);
   
    intent.putExtra("score", count);
startActivity(intent);
finish();
   // Toast.makeText(getApplicationContext(), count+"", Toast.LENGTH_LONG).show();
    }
};
// handler.postDelayed(runnable, remainingTime);
 

}


 

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
*/
public void onPause()
{
 super.onPause();
 
mediaPlayer.pause();
 
 handler.removeCallbacks(runnable);
 long elapsedTime = System.nanoTime();
//  elapsedTime=TimeUnit.SECONDS.convert(elapsedTime, TimeUnit.MILLISECONDS);
elapsedTime=(elapsedTime-starttime)/1000000;
   System.out.println(elapsedTime+"");
 remainingTime =  remainingTime- (elapsedTime);


}
@Override
public void onResume(){
   super.onResume();
   // put your code here...
   mediaPlayer.start();
   starttime = System.nanoTime();

   handler.postDelayed(runnable, remainingTime);
 

}

@Override
public boolean onTouchEvent(MotionEvent event)
{  
 
    switch(event.getAction())
    {
      case MotionEvent.ACTION_DOWN:
          x1 = event.getX();  
          y1 = event.getY();
      break;         
      case MotionEvent.ACTION_UP:
          x2 = event.getX();
          y2= event.getY();
          if(Math.abs(x2-x1)>Math.abs(y2-y1))
          {
          if(x2>x1)
          {
          float deltaX = x2 - x1;
          if(cuts[0]==1)
          {
         
          if (Math.abs(deltaX) > MIN_DISTANCE)
          {
        //   Toast.makeText(this, "left2right swipe", Toast.LENGTH_SHORT).show ();
          root=(RelativeLayout)findViewById(R.id.root);
        root.setBackgroundResource(R.drawable.hor);
        count++;
         
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                root.setBackgroundResource(R.drawable.org);
            }
        }, 200);
       
        right.setVisibility(View.INVISIBLE);
        cuts[0]=0;
          }
          else
          {
              // consider as something else - a screen tap for example
          }
          }
          }
          else
          {
          float deltaX = x2 - x1;
          if(cuts[1]==1)
          {
         
          if (Math.abs(deltaX) > MIN_DISTANCE)
          {
     //     Toast.makeText(this, "right2left swipe", Toast.LENGTH_SHORT).show ();
          root=(RelativeLayout)findViewById(R.id.root);
        root.setBackgroundResource(R.drawable.hor);
        count++;
        final Handler handler = new Handler();
         
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                // Do something after 5s = 5000ms
                root.setBackgroundResource(R.drawable.org);
            }
        }, 200);
       
       left.setVisibility(View.INVISIBLE);
       cuts[1]=0;
          }
          else
          {
              // consider as something else - a screen tap for example
          }
          } 
          }
          }
          else
          {
             if(y2>y1)
            {
             if(cuts[2]==1)
            {
            float deltaX = y2 - y1;
            if (Math.abs(deltaX) > MIN_DISTANCE)
            {
          //   Toast.makeText(this, "up2down swipe", Toast.LENGTH_SHORT).show ();
            root=(RelativeLayout)findViewById(R.id.root);
          root.setBackgroundResource(R.drawable.ver);
          count++;
          final Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  // Do something after 5s = 5000ms
                  root.setBackgroundResource(R.drawable.org);
              }
          }, 200);
          down.setVisibility(View.INVISIBLE);
          cuts[2]=0;
            }
            else
            {
                // consider as something else - a screen tap for example
            }
            }
            }
            else
            {
            float deltaX = y2 - y1;
           if(cuts[3]==1)
            {
            if (Math.abs(deltaX) > MIN_DISTANCE)
            {
         //   Toast.makeText(this, "down2up swipe", Toast.LENGTH_SHORT).show ();
            root=(RelativeLayout)findViewById(R.id.root);
          root.setBackgroundResource(R.drawable.ver);
          count++;
          final Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  // Do something after 5s = 5000ms
                  root.setBackgroundResource(R.drawable.org);
              }
          }, 200);
          up.setVisibility(View.INVISIBLE);
          cuts[3]=0;
         
            }
            else
            {
                // consider as something else - a screen tap for example
            }
            }
            
            }
          
          }
      break;   
    }           
    return super.onTouchEvent(event);       
}

}





LastActivity.java


package com.example.smashstack;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.psiuol21.smashstack.R;

public class LastActivity extends ActionBarActivity {
TextView tv,tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
   getActionBar().hide();
setContentView(R.layout.activity_last);
   int score = getIntent().getIntExtra("score",0);
  // Toast.makeText(getApplicationContext(), score, Toast.LENGTH_SHORT).show();
   tv=(TextView)findViewById(R.id.textView1);
   tv2=(TextView)findViewById(R.id.textView2);
   Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/FONTX.TTF");
   tv.setTypeface(mFont);
   tv.setTextColor(Color.parseColor("#FFF000"));
   tv.setText(score+"");
   tv2.setTypeface(mFont);
   tv2.setTextColor(Color.parseColor("#FFF000"));
   if(score<5)
   tv2.setText("WHITE BELT");
   else if(score<10)
   tv2.setText("YELLOW BELT");
   else if(score<15)
   tv2.setText("GREEN BELT");
   else if(score<20)
   tv2.setText("BLUE BELT");
   else if(score<25)
   tv2.setText("RED BELT");
   else if(score<30)
   tv2.setText("BLACK BELT");
   
}
private boolean MyStartActivity(Intent aIntent) {
   try
   {
       startActivity(aIntent);
       return true;
   }
   catch (ActivityNotFoundException e)
   {
       return false;
   }
}

public void rateus(View v)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
    //Try Google play
    intent.setData(Uri.parse("market://details?id=com.psiuol21.smashstack"));
    if (!MyStartActivity(intent)) {
        //Market (Google play) app seems not installed, let's try to open a webbrowser
        intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.psiuol21.smashstack"));
        if (!MyStartActivity(intent)) {
            //Well if this also fails, we have run out of options, inform the user.
            Toast.makeText(this, "Could not open Android market, please install the market app.", Toast.LENGTH_SHORT).show();
        }
    }}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.last, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

/**
* A placeholder fragment containing a simple view.
* @return 
*/
public void goback(View v)
{
Intent intent=new Intent(LastActivity.this,MainActivity.class);
startActivity(intent);
finish();
}

}