Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.media.Image;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.os.Handler;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- public class MainActivity extends AppCompatActivity {
- final Handler handler = new Handler();
- int randomNumber;
- int score = 0;
- int lives = 10;
- private Runnable runnableCode = new Runnable() {
- @Override
- public void run() {
- final ImageView moles[] = new ImageView[3];
- //add moles to the array
- moles[0] = (ImageView) findViewById(R.id.mole1);
- //add more moles
- moles[1] = (ImageView) findViewById(R.id.mole2);
- moles[2] = (ImageView) findViewById(R.id.mole3);
- moles[3] = (ImageView) findViewById(R.id.mole4);
- //get a random number
- randomNumber = new Random().nextInt(4);
- moles[randomNumber].animate().translationY(-150).setDuration(300);
- Log.i("MainActivity", "mole info:" + moles[randomNumber].getTranslationY());
- //checks the mole array to see which one is "up" and moves it back down
- //this should be added to a Timer().schedule new (TimerTask() {} wrap
- new Timer().schedule(new TimerTask() {
- @Override
- public void run () {
- for (int i = 0; i < 4; i++) {
- if (moles[i].getTranslationY() == -150) {
- // moles[i].setClickable(false);
- moles[i].animate().translationY(0).setDuration(300);
- lives = lives -1;
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- TextView textLives = (TextView) findViewById(R.id.textLives);
- textLives.setText("" + lives);
- }
- });
- if(lives== 0)
- {
- System.exit(0);
- }
- }
- }
- }
- }, 1000);
- handler.postDelayed(runnableCode, 2000);
- }
- };
- public void tapMole(View view){
- Log.d("MainActivity", "score is:" + score);
- ImageView mole = (ImageView) view;
- TextView textScore = (TextView) findViewById(R.id.textScore);
- mole.setClickable(false);
- mole.animate().translationY(0).setDuration(0);
- // Add 100 to score
- score = score +100;
- // Display score
- textScore.setText("" + score);
- // Set Clickable as true
- mole.setClickable(true);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d("MainActivity", "oncreate" + score);
- //get the handler to run the mole movement code
- handler.post(runnableCode);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.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();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement