尊龙凯时网址

android如何实现打地鼠游戏 -尊龙凯时网址

2023-06-20

这篇文章主要为大家展示了android如何实现打地鼠游戏,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

首先是打地鼠游戏的玩法

1.每隔1秒或者0.5秒地鼠会出现在九宫格中的任一位置
2.点击界面,如果地鼠出现的位置与点击位置相同,则认为打中地鼠。否则游戏继续。
3.打中地鼠后,游戏立即停止。

先上两张效果图

布局实现


 
 

逻辑代码

需要实现每隔1秒绘制一次界面,且得确定更新地鼠的位置(这里我用的handler来发送循环消息实现的)

arraylisttexts;//用来存放九个textview
handler handler = new handler(new callback() {
 @override
 public boolean handlemessage(message msg) {
  number = (int) (math.random() * 8);
  system.out.println(number);
  for (int i = 0; i < arrays.length; i  ) {
  if (i==number) {
   texts.get(i).settext("地鼠");
  }else {
   texts.get(i).settext("");
  }
  }
  handler.sendemptymessagedelayed(0, 500);
  return false;
 }
 });

这里单独写一个textview的点击事件(一共九个),这里需要注意的是,我给每个textview都赋给了一个值a,从0-8,当点击的textview值 a=(地鼠出现的位置)时,我们就可以认为打中了地鼠。

public void onclick(view v) {
 // todo auto-generated method stub
 switch (v.getid()) {
 case r.id.text11:
  if (number==0) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 }
 }

最后贴上全部代码。

package com.example.dadishu;
import java.io.ioexception;
import java.util.arraylist;
import java.util.timer;
import java.util.timertask;
import android.app.activity;
import android.media.mediaplayer;
import android.os.bundle;
import android.os.handler;
import android.os.handler.callback;
import android.os.message;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.window;
import android.widget.textview;
import android.widget.toast;
public class mainactivity extends activity implements onclicklistener {
 private int number;
 private textview text11;
 private textview text12;
 private textview text13;
 private textview text21;
 private textview text22;
 private textview text23;
 private textview text31;
 private textview text32;
 private textview text33;
 private int[] arrays = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 arraylist texts;
 handler handler = new handler(new callback() {
 @override
 public boolean handlemessage(message msg) {
  number = (int) (math.random() * 8);
  system.out.println(number);
  for (int i = 0; i < arrays.length; i  ) {
  if (i == number) {
   texts.get(i).settext("地鼠");
  } else {
   texts.get(i).settext("");
  }
  }
  handler.sendemptymessagedelayed(0, 500);
  return false;
 }
 });
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 requestwindowfeature(window.feature_no_title);
 setcontentview(r.layout.activity_main);
 text11 = (textview) findviewbyid(r.id.text11);
 text12 = (textview) findviewbyid(r.id.text12);
 text13 = (textview) findviewbyid(r.id.text13);
 text21 = (textview) findviewbyid(r.id.text21);
 text22 = (textview) findviewbyid(r.id.text22);
 text23 = (textview) findviewbyid(r.id.text23);
 text31 = (textview) findviewbyid(r.id.text31);
 text32 = (textview) findviewbyid(r.id.text32);
 text33 = (textview) findviewbyid(r.id.text33);
 texts = new arraylist();
 for (int i = 0; i < arrays.length; i  ) {
  texts.add(text11);
  texts.add(text12);
  texts.add(text13);
  texts.add(text21);
  texts.add(text22);
  texts.add(text23);
  texts.add(text31);
  texts.add(text32);
  texts.add(text33);
 }
 for (textview text : texts) {
  text.setonclicklistener(this);
 }
 }
 public void start(view v) {
 timertask task = new timertask() {
  @override
  public void run() {
  handler.sendemptymessage(0);
  }
 };
 timer timer = new timer();
 timer.schedule(task, 500);
 }
 public void end(view v) {
 handler.removemessages(0);
 }
 @override
 public void onclick(view v) {
 // todo auto-generated method stub
 switch (v.getid()) {
 case r.id.text11:
  if (number == 0) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text12:
  if (number == 1) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text13:
  if (number == 2) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text21:
  if (number == 3) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text22:
  if (number == 4) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text23:
  if (number == 5) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text31:
  if (number == 6) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text32:
  if (number == 7) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 case r.id.text33:
  if (number == 8) {
  toast.maketext(this, "打中地鼠了!。。。", 1).show();
  handler.removemessages(0);
  }
  break;
 default:
  break;
 }
 }
 @override
 protected void ondestroy() {
 // todo auto-generated method stub
 super.ondestroy();
 player.release();
 }
}

以上就是关于android如何实现打地鼠游戏的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。

网站地图