Android Implements a Simple Edition of Gopher Hitting

Time:2019-10-3

This article gives you an example to share the specific code for Android to implement a simple version of hamster, for your reference, the details are as follows.

Target effect:

1. Activity_main.xml page:

<?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= "start"/>
 <TextView
 android:id="@+id/tvWhackAMoleScore"
 android:gravity="center"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="match_parent"
 Android: text= "score"/>
 <Button
 android:id="@+id/btStopWhackAMole"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="match_parent"
 android:onClick="btnClick"
 Android: text = "end"/>
 </LinearLayout>
 
</LinearLayout>

2. MainActivity. Java page;

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; // Difficult time
 Private int time = time_s; // Hamster exit time
 Private int score = 0; // score, number of hamsters
 Private int num = 0; // Number of hamsters coming out
 Private int temp_i = 0, temp_j = 0; // Record the subscript of the last hamster in the array view
 Private int flag = 1; // defaults to stop state, 0 starts, 1 ends, 2 runs
 @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 ("score:"+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; // Start
 TvWhackAMoleScore. setText ("Score: 0");
 new MyAsyncTask().execute();
 break;
 case R.id.btStopWhackAMole:
 btStartWhackAMole.setClickable(true);
 btStopWhackAMole.setClickable(false);
 score=0;
 Flag = 1; // Stop
 new MyAsyncTask().execute();
 break;
 }
 }
 class MyAsyncTask extends AsyncTask<String,Integer,String>{
 
 @Override
 protected String doInBackground(String... strings) {
 // Enter operational status
 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 on ProgressUpdate (Integer... values) {// main thread
 If (flag==0) {// start
 view[values[0]][values[1]].setBackgroundResource(R.drawable.emptyhole);
 } else if (flag==2) {// run
 view[temp_i][temp_j].setBackgroundResource(R.drawable.emptyhole);
 View [temp_i] [temp_j]. setClickable (false); // The last time it appeared was set to not click
 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, "Game over", 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 is defined as a two-dimensional array of four rows and four columns, and then random coordinates are generated to change the picture.

Source: Click to open the link

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.