Created
August 14, 2014 12:04
-
-
Save mrp1q7z/f1ef3c7940cb58366de2 to your computer and use it in GitHub Desktop.
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@drawable/bk_grass" | |
tools:context="com.yojiokisoft.whacmole.app.MainActivity"> | |
<LinearLayout | |
android:id="@+id/relativeLayout" | |
android:layout_width="fill_parent" | |
android:orientation="horizontal" | |
android:paddingTop="20dp" | |
android:paddingLeft="10dp" | |
android:layout_height="60dp"> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="Score:" | |
android:textSize="25sp" /> | |
<TextView | |
android:id="@+id/scoreText" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="100" | |
android:textSize="30sp" /> | |
<TextView | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="Time:" | |
android:textSize="25sp" /> | |
<TextView | |
android:id="@+id/timeText" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="60" | |
android:textSize="30sp" /> | |
</LinearLayout> | |
<FrameLayout | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:layout_alignParentEnd="true" | |
android:layout_alignParentRight="false" | |
android:layout_below="@+id/relativeLayout"> | |
<TableLayout | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentTop="true"> | |
<TableRow | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<Button | |
android:id="@+id/Button1A" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button1B" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button1C" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button1D" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
</TableRow> | |
<TableRow | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<Button | |
android:id="@+id/Button2A" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button2B" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button2C" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button2D" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
</TableRow> | |
<TableRow | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<Button | |
android:id="@+id/Button3A" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button3B" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button3C" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button3D" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
</TableRow> | |
<TableRow | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<Button | |
android:id="@+id/Button4A" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button4B" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button4C" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button4D" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
</TableRow> | |
<TableRow | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<Button | |
android:id="@+id/Button5A" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button5B" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button5C" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
<Button | |
android:id="@+id/Button5D" | |
android:layout_width="0dp" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:background="@drawable/bg_button" /> | |
</TableRow> | |
</TableLayout> | |
<RelativeLayout | |
android:id="@+id/game_over_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:background="#8000" | |
android:clickable="true" | |
android:visibility="invisible"> | |
<TextView | |
android:id="@+id/game_over_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Game Over" | |
android:textSize="50sp" | |
android:textColor="#ffffff"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/game_over_text" | |
android:layout_centerHorizontal="true" | |
android:text="画面タップでリプレイ" | |
android:textSize="20sp" | |
android:textColor="#888888"/> | |
</RelativeLayout> | |
</FrameLayout> | |
</RelativeLayout> |
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.view.View; | |
import android.widget.Button; | |
import android.widget.RelativeLayout; | |
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 Button mMoleButton; | |
private TextView mScoreText; | |
private int mScore; | |
private TextView mTimeText; | |
private int mGameTime = 60; | |
private Timer mTimer = null; | |
private Handler mHandler = new Handler(); | |
private RelativeLayout mGameOver; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mMoleButton = null; | |
mScore = 0; | |
mScoreText = (TextView) findViewById(R.id.scoreText); | |
mTimeText = (TextView) findViewById(R.id.timeText); | |
mGameOver = (RelativeLayout) findViewById(R.id.game_over_container); | |
mGameOver.setOnClickListener(mGameOverClicked); | |
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); | |
for (int row = 0; row < mButton.length; row++) { | |
for (int col = 0; col < mButton[1].length; col++) { | |
mButton[row][col].setOnClickListener(mButtonClicked); | |
} | |
} | |
} | |
@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; | |
mGameOver.setVisibility(View.VISIBLE); | |
} | |
@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() { | |
if (mMoleButton != null) { | |
mMoleButton.setBackgroundResource(R.drawable.bg_button); | |
} | |
int row = (int) (Math.random() * 5); | |
int col = (int) (Math.random() * 4); | |
mButton[row][col].setBackgroundResource(R.drawable.mole); | |
mMoleButton = mButton[row][col]; | |
} | |
private View.OnClickListener mButtonClicked = new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (view != mMoleButton) { | |
return; | |
} | |
mScore++; | |
String strScore = String.valueOf(mScore); | |
mScoreText.setText(strScore); | |
printMole(); | |
} | |
}; | |
private View.OnClickListener mGameOverClicked = new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mGameOver.setVisibility(View.INVISIBLE); | |
mScore = 0; | |
String strScore = String.valueOf(mScore); | |
mScoreText.setText(strScore); | |
mGameTime = 60; | |
gameStart(); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment