Android實(shí)現(xiàn)簡易版打地鼠
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡易版打地鼠的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:

1.activity_main.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.weixu.eclipsemole.MainActivity"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <ImageView android:id="@+id/ivZeroZero" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivZeroOne" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivZeroTwo" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivZeroThree" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <ImageView android:id="@+id/ivOneZero" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivOneOne" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivOneTwo" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivOneThree" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <ImageView android:id="@+id/ivTwoZero" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivTwoOne" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivTwoTwo" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivTwoThree" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <ImageView android:id="@+id/ivThreeZero" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivThreeOne" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivThreeTwo" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> <ImageView android:id="@+id/ivThreeThree" android:layout_width="0dp" android:layout_weight="1" android:onClick="whackAMole" android:clickable="false" android:background="@drawable/emptyhole" android:layout_height="match_parent" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp"> <Button android:id="@+id/btStartWhackAMole" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:onClick="btnClick" android:text="開始"/> <TextView android:id="@+id/tvWhackAMoleScore" android:gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="分?jǐn)?shù)"/> <Button android:id="@+id/btStopWhackAMole" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:onClick="btnClick" android:text="結(jié)束"/> </LinearLayout> </LinearLayout>
2.MainActivity.java頁面;
package com.example.weixu.eclipsemole;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView[][] view=new ImageView[4][4];
private Button btStartWhackAMole,btStopWhackAMole;
private TextView tvWhackAMoleScore;
private int time_s=1000; //難度的時(shí)間
private int time=time_s; //地鼠出來時(shí)間
private int score=0; //成績,打地鼠個(gè)數(shù)
private int num=0; //地鼠出來個(gè)數(shù)
private int temp_i=0,temp_j=0; //記錄上一次出現(xiàn)的地鼠在數(shù)組view中的下標(biāo)
private int flag=1; //默認(rèn)為停止?fàn)顟B(tài),0開始,1結(jié)束,2運(yùn)行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
view[0][0]= (ImageView) findViewById(R.id.ivZeroZero);
view[0][1]= (ImageView) findViewById(R.id.ivZeroOne);
view[0][2]= (ImageView) findViewById(R.id.ivZeroTwo);
view[0][3]= (ImageView) findViewById(R.id.ivZeroThree);
view[1][0]= (ImageView) findViewById(R.id.ivOneZero);
view[1][1]= (ImageView) findViewById(R.id.ivOneOne);
view[1][2]= (ImageView) findViewById(R.id.ivOneTwo);
view[1][3]= (ImageView) findViewById(R.id.ivOneThree);
view[2][0]= (ImageView) findViewById(R.id.ivTwoZero);
view[2][1]= (ImageView) findViewById(R.id.ivTwoOne);
view[2][2]= (ImageView) findViewById(R.id.ivTwoTwo);
view[2][3]= (ImageView) findViewById(R.id.ivTwoThree);
view[3][0]= (ImageView) findViewById(R.id.ivThreeZero);
view[3][1]= (ImageView) findViewById(R.id.ivThreeOne);
view[3][2]= (ImageView) findViewById(R.id.ivThreeTwo);
view[3][3]= (ImageView) findViewById(R.id.ivThreeThree);
btStartWhackAMole= (Button) findViewById(R.id.btStartWhackAMole);
btStopWhackAMole= (Button) findViewById(R.id.btStopWhackAMole);
tvWhackAMoleScore= (TextView) findViewById(R.id.tvWhackAMoleScore);
btStartWhackAMole.setClickable(true);
btStopWhackAMole.setClickable(false);
}
public void whackAMole(View view){
view.setBackgroundResource(R.drawable.hit);
view.setClickable(false);
score++;
time=time_s-score*10;
tvWhackAMoleScore.setText("分?jǐn)?shù):"+score);
}
public void btnClick(View view){
switch (view.getId()){
case R.id.btStartWhackAMole:
btStartWhackAMole.setClickable(false);
btStopWhackAMole.setClickable(true);
num=0;
score=0;
flag=0; //開始
tvWhackAMoleScore.setText("分?jǐn)?shù):0");
new MyAsyncTask().execute();
break;
case R.id.btStopWhackAMole:
btStartWhackAMole.setClickable(true);
btStopWhackAMole.setClickable(false);
score=0;
flag=1; //停止
new MyAsyncTask().execute();
break;
}
}
class MyAsyncTask extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... strings) {
//進(jìn)入運(yùn)行狀態(tài)
while(flag!=1){
flag=2;
double r=Math.random();
int i=((int)(r*10))%4;
r=Math.random();
int j=((int)(r*10))%4;
try {
Thread.sleep(time);
}catch (InterruptedException e){
e.printStackTrace();;
}
publishProgress(i,j);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) { //主線程
if(flag==0){ //開始
view[values[0]][values[1]].setBackgroundResource(R.drawable.emptyhole);
}else if(flag==2){ //運(yùn)行
view[temp_i][temp_j].setBackgroundResource(R.drawable.emptyhole);
view[temp_i][temp_j].setClickable(false); //上一次出現(xiàn)的設(shè)置為不能點(diǎn)擊
view[values[0]][values[1]].setBackgroundResource(R.drawable.show6);
view[values[0]][values[1]].setClickable(true);
num++;
if((num-score)==5){
flag=1;
btStartWhackAMole.setClickable(true);
Toast.makeText(MainActivity.this,"游戲結(jié)束",Toast.LENGTH_SHORT).show();
}
temp_i=values[0];
temp_j=values[1];
}else if(flag==1){
view[values[0]][values[1]].setBackgroundResource(R.drawable.emptyhole);
view[values[0]][values[1]].setClickable(false);
}
}
}
}
是將四行四列的ImageView定義為一個(gè)二維數(shù)組,然后生成隨機(jī)坐標(biāo)進(jìn)行改變圖片。
源碼:點(diǎn)擊打開鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程基礎(chǔ)之Menu功能菜單設(shè)計(jì)實(shí)例
這篇文章主要介紹了Android編程基礎(chǔ)之Menu功能菜單,結(jié)合實(shí)例形式分析了基本的Menu功能菜單原理、定義與響應(yīng)機(jī)制,需要的朋友可以參考下2016-10-10
Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成
這篇文章主要介紹了Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android中使用orc實(shí)現(xiàn)文字識別實(shí)例
這篇文章主要介紹了Android中使用orc實(shí)現(xiàn)文字識別實(shí)例,詳細(xì)的介紹了orc的簡介和用法,有興趣的可以了解一下2017-05-05
Android開發(fā)基礎(chǔ)簡化Toast調(diào)用方法詳解
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)簡化Toast調(diào)用方法的相關(guān)資料,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
flutter?Bloc?實(shí)現(xiàn)原理示例解析
這篇文章主要為大家介紹了flutter?Bloc實(shí)現(xiàn)原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android開發(fā)筆記之:在ImageView上繪制圓環(huán)的實(shí)現(xiàn)方法
本篇文章是對Android中在ImageView上繪制圓環(huán)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android中Viewpager禁止滑動(dòng)的實(shí)現(xiàn)
有時(shí)候在開發(fā)中會(huì)遇到一些特別的要求,如在ViewPager中嵌入ListView,或者再嵌入一個(gè)ViewPager,那么在滑動(dòng)的時(shí)候就會(huì)造成被嵌入的XXView不能滑動(dòng)了,那么就把最外層的ViewPager禁止滑動(dòng)吧,本文就介紹了Android中Viewpager禁止滑動(dòng)的實(shí)現(xiàn)方法,需要的朋友可以參考。2017-05-05
Android實(shí)現(xiàn)自動(dòng)匹配關(guān)鍵字并且標(biāo)紅功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)匹配關(guān)鍵字并且標(biāo)紅功能,單關(guān)鍵字和多關(guān)鍵字進(jìn)行匹配,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法
這篇文章主要介紹了Android使用廣播(BroadCast)實(shí)現(xiàn)強(qiáng)制下線的方法,實(shí)例分析了Android廣播BroadCast控制activity關(guān)閉的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01

