Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mpadhq.tonysmith.whackamonkey;
- import android.app.*;
- import android.os.*;
- import android.view.*;
- import android.view.View.*;
- import android.widget.*;
- import android.content.*;
- import android.content.ClipboardManager;
- import android.graphics.*;
- import android.media.*;
- import android.net.*;
- import android.text.*;
- import android.util.*;
- import android.webkit.*;
- import android.animation.*;
- import android.view.animation.*;
- import com.google.android.gms.ads.AdRequest;
- import com.google.android.gms.ads.AdView;
- import com.google.android.gms.ads.InterstitialAd;
- import com.google.android.gms.ads.MobileAds;
- import com.google.android.gms.ads.reward.RewardItem;
- import com.google.android.gms.ads.reward.RewardedVideoAd;
- import com.google.android.gms.ads.reward.RewardedVideoAdListener;
- import java.util.*;
- import java.text.*;
- public class GameActivity extends Activity implements RewardedVideoAdListener {
- private LinearLayout linear1;
- private LinearLayout linear4;
- private LinearLayout linear5;
- private LinearLayout linear6;
- private LinearLayout linear2;
- private LinearLayout linear3;
- private LinearLayout linear7;
- private LinearLayout linear8;
- private TextView textview1;
- private TextView monkey;
- private TextView score;
- private TextView lives;
- private TextView deco1;
- private Button mole1;
- private Button mole2;
- private Button mole3;
- private Button mole4;
- private Button mole5;
- private Button mole6;
- private TextView deco2;
- private TextView notify;
- private Button video_life;
- private double speed = 0;
- private double random = 0;
- private String mole = "";
- private double scoreValue = 0;
- private double life = 0;
- private String lifeString = "";
- private double hideSpeed = 0;
- private double level = 0;
- private double threshold = 0;
- private double blinkSpeed = 0;
- private double defaultSpeed = 0;
- private double defaultHideSpeed = 0;
- private double hideThreshold = 0;
- private ArrayList<String> moleList = new ArrayList<String>();
- private Timer _timer = new Timer();
- private TimerTask timer1;
- private TimerTask temp;
- private MediaPlayer music;
- private AlertDialog.Builder dialog;
- private Intent i = new Intent();
- //Ad variables
- private AdView mAdView;
- private RewardedVideoAd mRewardedVideoAd;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.game);
- //Start ad process
- MobileAds.initialize(this,
- "ca-app-pub-3940256099942544~3347511713");//TODO this
- //Insert ad in game banner
- mAdView = (AdView) findViewById(R.id.adView);
- AdRequest adRequest = new AdRequest.Builder().build();
- mAdView.loadAd(adRequest);
- //Get reward video ad
- mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
- mRewardedVideoAd.setRewardedVideoAdListener(this);
- loadRewardedVideoAd();
- initialize();
- initializeLogic();
- }
- private void initialize() {
- linear1 = (LinearLayout) findViewById(R.id.linear1);
- linear4 = (LinearLayout) findViewById(R.id.linear4);
- linear5 = (LinearLayout) findViewById(R.id.linear5);
- linear6 = (LinearLayout) findViewById(R.id.linear6);
- linear2 = (LinearLayout) findViewById(R.id.linear2);
- linear3 = (LinearLayout) findViewById(R.id.linear3);
- linear7 = (LinearLayout) findViewById(R.id.linear7);
- linear8 = (LinearLayout) findViewById(R.id.linear8);
- textview1 = (TextView) findViewById(R.id.textview1);
- monkey = (TextView) findViewById(R.id.monkey);
- score = (TextView) findViewById(R.id.score);
- lives = (TextView) findViewById(R.id.lives);
- deco1 = (TextView) findViewById(R.id.deco1);
- mole1 = (Button) findViewById(R.id.mole1);
- mole2 = (Button) findViewById(R.id.mole2);
- mole3 = (Button) findViewById(R.id.mole3);
- mole4 = (Button) findViewById(R.id.mole4);
- mole5 = (Button) findViewById(R.id.mole5);
- mole6 = (Button) findViewById(R.id.mole6);
- deco2 = (TextView) findViewById(R.id.deco2);
- notify = (TextView) findViewById(R.id.notify);
- video_life = (Button) findViewById(R.id.video_life);
- dialog = new AlertDialog.Builder(this);
- video_life.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mRewardedVideoAd.isLoaded()) {
- mRewardedVideoAd.show();
- }
- }
- });
- mole1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mole1.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole1.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole1.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole1.getText().toString().equals("š£")) {
- life--;
- showMessage("Owie!\n⤠- 1");
- }
- if (mole1.getText().toString().equals("ā¤")) {
- life++;
- showMessage("⤠+ 1");
- }
- if (mole1.getText().toString().equals("š")) {
- showMessage("Speed Reset š");
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- }
- mole1.setEnabled(false);
- _updateScore();
- }
- });
- mole2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mole2.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole2.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole2.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole2.getText().toString().equals("š£")) {
- life--;
- showMessage("Owie!\n⤠- 1");
- }
- if (mole2.getText().toString().equals("ā¤")) {
- life++;
- showMessage("⤠+ 1");
- }
- if (mole2.getText().toString().equals("š")) {
- showMessage("Speed Reset š");
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- }
- mole2.setEnabled(false);
- _updateScore();
- }
- });
- mole3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mole3.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole3.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole3.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole3.getText().toString().equals("š£")) {
- life--;
- showMessage("Owie!\n⤠- 1");
- }
- if (mole3.getText().toString().equals("ā¤")) {
- life++;
- showMessage("⤠+ 1");
- }
- if (mole3.getText().toString().equals("š")) {
- showMessage("Speed Reset š");
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- }
- mole3.setEnabled(false);
- _updateScore();
- }
- });
- mole4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mole4.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole4.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole4.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole4.getText().toString().equals("š£")) {
- life--;
- showMessage("Owie!\n⤠- 1");
- }
- if (mole4.getText().toString().equals("ā¤")) {
- life++;
- showMessage("⤠+ 1");
- }
- if (mole4.getText().toString().equals("š")) {
- showMessage("Speed Reset š");
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- }
- mole4.setEnabled(false);
- _updateScore();
- }
- });
- mole5.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mole5.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole5.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole5.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole5.getText().toString().equals("š£")) {
- life--;
- showMessage("Owie!\n⤠- 1");
- }
- if (mole5.getText().toString().equals("ā¤")) {
- life++;
- showMessage("⤠+ 1");
- }
- if (mole5.getText().toString().equals("š")) {
- showMessage("Speed Reset š");
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- }
- mole5.setEnabled(false);
- _updateScore();
- }
- });
- mole6.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View _v) {
- if (mole6.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole6.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole6.getText().toString().equals("š")) {
- scoreValue++;
- }
- if (mole6.getText().toString().equals("š£")) {
- life--;
- showMessage("Owie!\n⤠- 1");
- }
- if (mole6.getText().toString().equals("ā¤")) {
- life++;
- showMessage("⤠+ 1");
- }
- if (mole6.getText().toString().equals("š")) {
- showMessage("Speed Reset š");
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- }
- mole6.setEnabled(false);
- _updateScore();
- }
- });
- }
- private void initializeLogic() {
- music = MediaPlayer.create(getApplicationContext(), R.raw.christophertinbabayetu);
- music.start();
- music.setLooping(true);
- showMessage("Music credits: Christopher Tin - Baba Yetu");
- defaultSpeed = 800;
- defaultHideSpeed = 700;
- speed = defaultSpeed;
- hideSpeed = defaultHideSpeed;
- life = 3;
- threshold = 5;
- blinkSpeed = 500;
- hideThreshold = 1;
- moleList.add("š");
- moleList.add("š");
- moleList.add("š");
- monkey.setText("š x ");
- deco1.setText("šššššššššššššššššššššššš");
- deco2.setText("šššššššššššššššššššššššš");
- _showLife();
- _generateMoles();
- dialog.setTitle("Leaving already?");
- dialog.setMessage("Please stay");
- dialog.setPositiveButton("Leave", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface _dialog, int _which) {
- i.setClass(getApplicationContext(), MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(i);
- }
- });
- dialog.setNegativeButton("Stay", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface _dialog, int _which) {
- }
- });
- // ActionBar actionBar = getActionBar(); actionBar.setBackground///Drawable(new android.graphics.drawable.ColorDrawable(Color.parseColor("#FF66BB6A")));
- //getActionBar().setTitle(Html.fromHtml("<font color='#FFFFFFFF'>'Score' + scoreValue </jenniferlynne>"));
- }
- private void loadRewardedVideoAd() {
- mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
- new AdRequest.Builder().build());
- }
- @Override
- public void onRewarded(RewardItem reward) {
- scoreValue++;
- }
- @Override
- public void onRewardedVideoAdLeftApplication() {
- }
- @Override
- public void onRewardedVideoAdClosed() {
- // Load the next rewarded video ad.
- loadRewardedVideoAd();
- }
- @Override
- public void onRewardedVideoAdFailedToLoad(int errorCode) {
- }
- @Override
- public void onRewardedVideoAdLoaded() {
- }
- @Override
- public void onRewardedVideoAdOpened() {
- }
- @Override
- public void onRewardedVideoStarted() {
- }
- @Override
- public void onRewardedVideoCompleted() {
- life++;
- showMessage("⤠+ 1");
- }
- @Override
- public void onPause() {
- super.onPause();
- music.release();
- }
- @Override
- public void onStop() {
- super.onStop();
- music.release();
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- music.release();
- }
- @Override
- public void onBackPressed() {
- music.release();
- dialog.create().show();
- }
- private void _generateMoles () {
- timer1 = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- random = getRandom((int)(1), (int)(6));
- _viewMole(random);
- temp = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- _hideMole(random);
- }
- });
- }
- };
- _timer.schedule(temp, (int)(hideSpeed));
- }
- });
- }
- };
- _timer.scheduleAtFixedRate(timer1, (int)(1000), (int)(speed));
- }
- private void _viewMole (final double _index) {
- mole = moleList.get((int)(getRandom((int)(0), (int)(moleList.size() - 1))));
- if (getRandom((int)(0), (int)(10)) < 3) {
- mole = "š£";
- }
- if (getRandom((int)(0), (int)(10)) < 1) {
- mole = "ā¤";
- }
- if (getRandom((int)(0), (int)(100)) < 5) {
- mole = "š";
- }
- if (_index == 1) {
- mole1.setText(mole);
- mole1.setEnabled(true);
- }
- if (_index == 2) {
- mole2.setText(mole);
- mole2.setEnabled(true);
- }
- if (_index == 3) {
- mole3.setText(mole);
- mole3.setEnabled(true);
- }
- if (_index == 4) {
- mole4.setText(mole);
- mole4.setEnabled(true);
- }
- if (_index == 5) {
- mole5.setText(mole);
- mole5.setEnabled(true);
- }
- if (_index == 6) {
- mole6.setText(mole);
- mole6.setEnabled(true);
- }
- }
- private void _hideMole (final double _index) {
- if (_index == 1) {
- mole1.setText("ā");
- mole1.setEnabled(false);
- }
- if (_index == 2) {
- mole2.setText("ā");
- mole2.setEnabled(false);
- }
- if (_index == 3) {
- mole3.setText("ā");
- mole3.setEnabled(false);
- }
- if (_index == 4) {
- mole4.setText("ā");
- mole4.setEnabled(false);
- }
- if (_index == 5) {
- mole5.setText("ā");
- mole5.setEnabled(false);
- }
- if (_index == 6) {
- mole6.setText("ā");
- mole6.setEnabled(false);
- }
- }
- private void _showLife () {
- lifeString = "";
- if (life > 5) {
- lifeString = "⤠x ".concat(String.valueOf((long)(life)));
- }
- else {
- for(int _repeat10 = 0; _repeat10 < (int)(life); _repeat10++) {
- lifeString = lifeString.concat("⤠");
- }
- }
- lives.setText(lifeString);
- }
- private void _updateScore () {
- _showLife();
- score.setText(String.valueOf((long)(scoreValue)));
- if (level < (scoreValue / 5)) {
- _notifyUser("Speed Up!!");
- }
- level = scoreValue / 5;
- speed = speed - (level * threshold);
- hideSpeed = hideSpeed - (level * hideThreshold);
- _checkGameOver();
- }
- private void _checkGameOver () {
- if (life == 0) {
- showMessage("Game Over\nPress back to try again!");
- timer1.cancel();
- }
- }
- private void _notifyUser (final String _message) {
- notify.setText(_message);
- temp = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- notify.setVisibility(View.INVISIBLE);
- }
- });
- }
- };
- _timer.schedule(temp, (int)(blinkSpeed * 1));
- temp = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- notify.setVisibility(View.VISIBLE);
- }
- });
- }
- };
- _timer.schedule(temp, (int)(blinkSpeed * 2));
- temp = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- notify.setVisibility(View.INVISIBLE);
- }
- });
- }
- };
- _timer.schedule(temp, (int)(blinkSpeed * 3));
- temp = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- notify.setVisibility(View.VISIBLE);
- }
- });
- }
- };
- _timer.schedule(temp, (int)(blinkSpeed * 4));
- temp = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- notify.setVisibility(View.INVISIBLE);
- }
- });
- }
- };
- _timer.schedule(temp, (int)(blinkSpeed * 5));
- }
- // created automatically
- private void showMessage(String _s) {
- Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
- }
- private int getLocationX(View _v) {
- int _location[] = new int[2];
- _v.getLocationInWindow(_location);
- return _location[0];
- }
- private int getLocationY(View _v) {
- int _location[] = new int[2];
- _v.getLocationInWindow(_location);
- return _location[1];
- }
- private int getRandom(int _minValue ,int _maxValue){
- Random random = new Random();
- return random.nextInt(_maxValue - _minValue + 1) + _minValue;
- }
- public ArrayList<Double> getCheckedItemPositionsToArray(ListView _list) {
- ArrayList<Double> _result = new ArrayList<Double>();
- SparseBooleanArray _arr = _list.getCheckedItemPositions();
- for (int _iIdx = 0; _iIdx < _arr.size(); _iIdx++) {
- if (_arr.valueAt(_iIdx))
- _result.add((double)_arr.keyAt(_iIdx));
- }
- return _result;
- }
- private float getDip(int _input){
- return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, _input, getResources().getDisplayMetrics());
- }
- private int getDisplayWidthPixels(){
- return getResources().getDisplayMetrics().widthPixels;
- }
- private int getDisplayHeightPixels(){
- return getResources().getDisplayMetrics().heightPixels;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement