当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现简易版打地鼠

Android实现简易版打地鼠

2019年07月23日  | 移动技术网IT编程  | 我要评论

张保顺,手机充值卡进货渠道,杀人如麻1

本文实例为大家分享了android实现简易版打地鼠的具体代码,供大家参考,具体内容如下

目标效果:

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="分数"/>
 <button
 android:id="@+id/btstopwhackamole"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="match_parent"
 android:onclick="btnclick"
 android:text="结束"/>
 </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; //难度的时间
 private int time=time_s; //地鼠出来时间
 private int score=0; //成绩,打地鼠个数
 private int num=0; //地鼠出来个数
 private int temp_i=0,temp_j=0; //记录上一次出现的地鼠在数组view中的下标
 private int flag=1; //默认为停止状态,0开始,1结束,2运行
 @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);
 }
 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("分数: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) {
 //进入运行状态
 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){ //运行
 view[temp_i][temp_j].setbackgroundresource(r.drawable.emptyhole);
 view[temp_i][temp_j].setclickable(false); //上一次出现的设置为不能点击
 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,"游戏结束",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定义为一个二维数组,然后生成随机坐标进行改变图片。

源码:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网