-
-
Save mrp1q7z/cdae635e02c16c1cb9e9 to your computer and use it in GitHub Desktop.
Whac Mole 2014.04.12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.yojiokisoft.whacmole.app; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.v7.app.ActionBarActivity; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class MainActivity extends ActionBarActivity { | |
private Button[][] mButton = new Button[5][4]; // [行][列] | |
private TextView mTimeText; | |
private int mGameTime = 60; | |
private Timer mTimer = null; | |
private Handler mHandler = new Handler(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mTimeText = (TextView) findViewById(R.id.timeText); | |
mButton[0][0] = (Button) findViewById(R.id.Button1A); | |
mButton[0][1] = (Button) findViewById(R.id.Button1B); | |
mButton[0][2] = (Button) findViewById(R.id.Button1C); | |
mButton[0][3] = (Button) findViewById(R.id.Button1D); | |
mButton[1][0] = (Button) findViewById(R.id.Button2A); | |
mButton[1][1] = (Button) findViewById(R.id.Button2B); | |
mButton[1][2] = (Button) findViewById(R.id.Button2C); | |
mButton[1][3] = (Button) findViewById(R.id.Button2D); | |
mButton[2][0] = (Button) findViewById(R.id.Button3A); | |
mButton[2][1] = (Button) findViewById(R.id.Button3B); | |
mButton[2][2] = (Button) findViewById(R.id.Button3C); | |
mButton[2][3] = (Button) findViewById(R.id.Button3D); | |
mButton[3][0] = (Button) findViewById(R.id.Button4A); | |
mButton[3][1] = (Button) findViewById(R.id.Button4B); | |
mButton[3][2] = (Button) findViewById(R.id.Button4C); | |
mButton[3][3] = (Button) findViewById(R.id.Button4D); | |
mButton[4][0] = (Button) findViewById(R.id.Button5A); | |
mButton[4][1] = (Button) findViewById(R.id.Button5B); | |
mButton[4][2] = (Button) findViewById(R.id.Button5C); | |
mButton[4][3] = (Button) findViewById(R.id.Button5D); | |
} | |
@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; | |
} | |
private void gameStart() { | |
printMole(); | |
if (mTimer != null) { | |
return; | |
} | |
mTimer = new Timer(true); | |
mTimer.scheduleAtFixedRate(new TimerTask() { | |
@Override | |
public void run() { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mGameTime--; | |
if (mGameTime <= 0) { | |
gameOver(); | |
} | |
mTimeText.setText(Integer.toString(mGameTime)); | |
} | |
}); | |
} | |
}, 1000, 1000); | |
} | |
private void gameOver() { | |
mTimer.cancel(); | |
mTimer = null; | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
gameStart(); | |
} | |
@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); | |
} | |
private void printMole() { | |
int row = (int) (Math.random() * 5); | |
int col = (int) (Math.random() * 4); | |
mButton[row][col].setBackgroundResource(R.drawable.mole); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment