Advertisement
Guest User

Untitled

a guest
May 26th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. import android.media.Image;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.os.Handler;
  8. import android.view.View;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11.  
  12. import java.util.Random;
  13. import java.util.Timer;
  14. import java.util.TimerTask;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. final Handler handler = new Handler();
  19. int randomNumber;
  20. int score = 0;
  21. int lives = 10;
  22.  
  23. private Runnable runnableCode = new Runnable() {
  24. @Override
  25. public void run() {
  26.  
  27. final ImageView moles[] = new ImageView[3];
  28.  
  29. //add moles to the array
  30. moles[0] = (ImageView) findViewById(R.id.mole1);
  31. //add more moles
  32. moles[1] = (ImageView) findViewById(R.id.mole2);
  33. moles[2] = (ImageView) findViewById(R.id.mole3);
  34. moles[3] = (ImageView) findViewById(R.id.mole4);
  35.  
  36. //get a random number
  37. randomNumber = new Random().nextInt(4);
  38. moles[randomNumber].animate().translationY(-150).setDuration(300);
  39.  
  40. Log.i("MainActivity", "mole info:" + moles[randomNumber].getTranslationY());
  41.  
  42. //checks the mole array to see which one is "up" and moves it back down
  43. //this should be added to a Timer().schedule new (TimerTask() {} wrap
  44. new Timer().schedule(new TimerTask() {
  45. @Override
  46. public void run () {
  47. for (int i = 0; i < 4; i++) {
  48. if (moles[i].getTranslationY() == -150) {
  49. // moles[i].setClickable(false);
  50. moles[i].animate().translationY(0).setDuration(300);
  51. lives = lives -1;
  52. runOnUiThread(new Runnable() {
  53. @Override
  54. public void run() {
  55.  
  56. TextView textLives = (TextView) findViewById(R.id.textLives);
  57. textLives.setText("" + lives);
  58. }
  59. });
  60.  
  61. if(lives== 0)
  62. {
  63. System.exit(0);
  64. }
  65.  
  66. }
  67. }
  68. }
  69. }, 1000);
  70.  
  71. handler.postDelayed(runnableCode, 2000);
  72. }
  73. };
  74.  
  75. public void tapMole(View view){
  76.  
  77. Log.d("MainActivity", "score is:" + score);
  78.  
  79. ImageView mole = (ImageView) view;
  80.  
  81. TextView textScore = (TextView) findViewById(R.id.textScore);
  82. mole.setClickable(false);
  83. mole.animate().translationY(0).setDuration(0);
  84.  
  85. // Add 100 to score
  86. score = score +100;
  87. // Display score
  88. textScore.setText("" + score);
  89. // Set Clickable as true
  90. mole.setClickable(true);
  91.  
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. @Override
  102. protected void onCreate(Bundle savedInstanceState) {
  103. super.onCreate(savedInstanceState);
  104. setContentView(R.layout.activity_main);
  105. Log.d("MainActivity", "oncreate" + score);
  106. //get the handler to run the mole movement code
  107. handler.post(runnableCode);
  108.  
  109. }
  110.  
  111. @Override
  112. public boolean onCreateOptionsMenu(Menu menu) {
  113. // Inflate the menu; this adds items to the action bar if it is present.
  114. getMenuInflater().inflate(R.menu.menu_main, menu);
  115. return true;
  116. }
  117.  
  118. @Override
  119. public boolean onOptionsItemSelected(MenuItem item) {
  120. // Handle action bar item clicks here. The action bar will
  121. // automatically handle clicks on the Home/Up button, so long
  122. // as you specify a parent activity in AndroidManifest.xml.
  123. int id = item.getItemId();
  124.  
  125. //noinspection SimplifiableIfStatement
  126. if (id == R.id.action_settings) {
  127. return true;
  128. }
  129.  
  130. return super.onOptionsItemSelected(item);
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement