Created
August 16, 2014 13:56
-
-
Save mrp1q7z/d42c75408e7a62683428 to your computer and use it in GitHub Desktop.
Android WhacMole App. @see: http://4jiokisoft.blogspot.jp/2014/08/899.html
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="0" | |
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" | |
android:layout_weight="1"> | |
<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" | |
android:layout_weight="1"> | |
<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" | |
android:layout_weight="1"> | |
<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" | |
android:layout_weight="1"> | |
<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" | |
android:layout_weight="1"> | |
<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:id="@+id/replay_text" | |
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
/* | |
* Copyright (C) 2014 4jiokiSoft | |
* | |
* This program is free software; you can redistribute it and/or modify it under the | |
* terms of the GNU General Public License as published by the Free Software | |
* Foundation; either version 3 of the License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License along with this | |
* program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
package com.yojiokisoft.whacmole.app; | |
import android.media.AudioManager; | |
import android.media.SoundPool; | |
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; | |
private SoundPool mSound; | |
private int[] mSoundId; | |
private Timer mMoleTimer = null; | |
private TextView mReplayText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mSoundId = new int[2]; | |
mSound = new SoundPool(mSoundId.length, AudioManager.STREAM_MUSIC, 0); | |
mSoundId[0] = mSound.load(getApplicationContext(), R.raw.middle_punch1, 0); | |
mSoundId[1] = mSound.load(getApplicationContext(), R.raw.swing1, 0); | |
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); | |
mReplayText = (TextView) findViewById(R.id.replay_text); | |
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. | |
return true; | |
} | |
private void gameStart() { | |
mReplayText.setVisibility(View.INVISIBLE); | |
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; | |
mMoleTimer.cancel(); | |
mMoleTimer = null; | |
mGameOver.setVisibility(View.VISIBLE); | |
Timer timer = new Timer(true); | |
timer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mReplayText.setVisibility(View.VISIBLE); | |
} | |
}); | |
} | |
}, 2000); | |
} | |
@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(); | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
mSound.release(); | |
} | |
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]; | |
if (mMoleTimer != null) { | |
mMoleTimer.cancel(); | |
} | |
long delay = (long) (Math.random() * (1000 - 100) + 100); | |
mMoleTimer = new Timer(true); | |
mMoleTimer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
printMole(); | |
} | |
}); | |
} | |
}, delay); | |
} | |
private View.OnClickListener mButtonClicked = new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (view != mMoleButton) { | |
mSound.play(mSoundId[1], 1.0F, 1.0F, 0, 0, 1.0F); | |
return; | |
} | |
mSound.play(mSoundId[0], 1.0F, 1.0F, 0, 0, 1.0F); | |
mScore++; | |
String strScore = String.valueOf(mScore); | |
mScoreText.setText(strScore); | |
printMole(); | |
} | |
}; | |
private View.OnClickListener mGameOverClicked = new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (mReplayText.getVisibility() != View.VISIBLE) { | |
return; | |
} | |
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