千家信息网

Android怎样实现九宫格拼图游戏

发表于:2024-10-12 作者:千家信息网编辑
千家信息网最后更新 2024年10月12日,这篇文章主要介绍Android怎样实现九宫格拼图游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先编写拼图界面布局: 接
千家信息网最后更新 2024年10月12日Android怎样实现九宫格拼图游戏

这篇文章主要介绍Android怎样实现九宫格拼图游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

首先编写拼图界面布局:

                          

接下来,我们编写拼图activity的逻辑代码:

import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.ImageButton;import android.widget.TextView;public class MainActivity extends Activity{ private ImageButton button00,button01,button02,button10,button11,button12,button20,button21,button22; private Button buttonrestart; private TextView textView; private int Imagex = 3; private int Imagey = 3; private int imgCount = Imagex * Imagey; private int length = imgCount; private int blankSwap = length - 1; private int blankImgid = R.id.btn_02x02;// 初始化时候空白区域的按钮id private int time; private boolean timeswitch = true; // 声明一个图片数组的下标数组,随机排列这个数组 private int[] imageIndex = new int[length]; private int[] image = { R.mipmap.img_xiaoxiong_00x00, R.mipmap.img_xiaoxiong_00x01, R.mipmap.img_xiaoxiong_00x02, R.mipmap.img_xiaoxiong_01x00,   R.mipmap.img_xiaoxiong_01x01, R.mipmap.img_xiaoxiong_01x02, R.mipmap.img_xiaoxiong_02x00, R.mipmap.img_xiaoxiong_02x01,   R.mipmap.img_xiaoxiong_02x02, }; Handler handler = new Handler() {  // 为了更新时间用handler更新,其实就是textView.settext(时间)  public void handleMessage(Message msg){   if (msg.what == 1) {    textView.setText("时间:" + time);    if (timeswitch){     time++;     handler.sendEmptyMessageDelayed(1,1000);    }   }  }; }; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  // 初始化这些控件  button00 = (ImageButton) findViewById(R.id.btn_00x00);  button01 = (ImageButton) findViewById(R.id.btn_00x01);  button02 = (ImageButton) findViewById(R.id.btn_00x02);  button10 = (ImageButton) findViewById(R.id.btn_01x00);  button11 = (ImageButton) findViewById(R.id.btn_01x01);  button12 = (ImageButton) findViewById(R.id.btn_01x02);  button20 = (ImageButton) findViewById(R.id.btn_02x00);  button21 = (ImageButton) findViewById(R.id.btn_02x01);  button22 = (ImageButton) findViewById(R.id.btn_02x02);  textView = (TextView) findViewById(R.id.text_time);  buttonrestart = (Button) findViewById(R.id.btn_restart);  handler.sendEmptyMessageDelayed(1,1000);  random(); } // 监听方法 private void random() {  timeswitch = true;  for (int i = 0; i < imageIndex.length; i++) {   // 利用循环讲数组存入值为012345678   imageIndex[i] = i;  }  int rand1, rand2;  for (int j = 0; j < 20; j++) {   // math.random 0-1的随机数,乘以8就是0-8的随机数   rand1 = (int) (Math.random() * (length - 1));   do {    rand2 = (int) (Math.random() * (length - 1));    if (rand1 != rand2) {     break;    }   } while (true);   swap(rand1, rand2);  }  // 随机排列  button00.setImageDrawable(getResources().getDrawable(image[imageIndex[0]]));  button01.setImageDrawable(getResources().getDrawable(image[imageIndex[1]]));  button02.setImageDrawable(getResources().getDrawable(image[imageIndex[2]]));  button10.setImageDrawable(getResources().getDrawable(image[imageIndex[3]]));  button11.setImageDrawable(getResources().getDrawable(image[imageIndex[4]]));  button12.setImageDrawable(getResources().getDrawable(image[imageIndex[5]]));  button20.setImageDrawable(getResources().getDrawable(image[imageIndex[6]]));  button21.setImageDrawable(getResources().getDrawable(image[imageIndex[7]]));  button22.setImageDrawable(getResources().getDrawable(image[imageIndex[8]])); } public void swap(int rand1, int rand2){  int temp = imageIndex[rand1];  imageIndex[rand1] = imageIndex[rand2];  imageIndex[rand2] = temp; } public void onClick(View view) {  // id就是点击按钮的时候传过来的button的id  int id = view.getId();  // 通过id进行条件语句的执行  switch (id) {   case R.id.btn_00x00:    move(R.id.btn_00x00, 0);    break;   case R.id.btn_00x01:    move(R.id.btn_00x01, 1);    break;   case R.id.btn_00x02:    move(R.id.btn_00x02, 2);    break;   case R.id.btn_01x00:    move(R.id.btn_01x00, 3);    break;   case R.id.btn_01x01:    move(R.id.btn_01x01, 4);    break;   case R.id.btn_01x02:    move(R.id.btn_01x02, 5);    break;   case R.id.btn_02x00:    move(R.id.btn_02x00, 6);    break;   case R.id.btn_02x01:    move(R.id.btn_02x01, 7);    break;   case R.id.btn_02x02:    move(R.id.btn_02x02, 8);    break;  } } // 点击的图片与空白区域的交换的方法 public void move(int imagbtnId, int site) {  int sitex = site / Imagex;// site 为第几张图片  int sitey = site % Imagey;  // 初始化空白处的坐标  int blankx = blankSwap / Imagex;  int blanky = blankSwap % Imagey;  // 取绝对值  int x = Math.abs(sitex - blankx);  int y = Math.abs(sitey - blanky);  // 两种情况要不是在同一行的不同列,要不就是在同一列的不同行  if ( (x == 0 && y == 1) || (x == 1 && y == 0)) {   // 定义新的imagebutton 等于我们传过来的图片buttonid   ImageButton clickButton = (ImageButton) findViewById(imagbtnId);   clickButton.setVisibility(View.INVISIBLE);   // 定义一个新的图片按钮,然后findviewbyid空白控件的id   ImageButton blankButton = (ImageButton) findViewById(blankImgid);   // 然后将图片按钮重新设置图片为我们传过来的第二个参数   blankButton.setImageDrawable(getResources().getDrawable(image[imageIndex[site]]));   // 但是,这个控件还是不可见的,设置为可见   blankButton.setVisibility(View.VISIBLE);   swap(site, blankSwap);   // 将新的空白区域位置更新等于传过来的点击的按钮的位置   blankSwap = site;   // 将新的空白区域的id更新为传过来的点击的按钮的id   blankImgid = imagbtnId;  }  gameOver(); } // 如果重新开始,我们要还原被点击的图片按钮变成初始化的模样 public void restore() {  handler.removeMessages(1);  // 定义新的imagebutton 等于我们新的空白图片按钮id,并且设置可见,  ImageButton clickButton = (ImageButton) findViewById(blankImgid);  clickButton.setVisibility(View.VISIBLE);  // 定义一个新的图片按钮,然后findviewbyid空白控件的id这个id就是我们初始化的时候设置隐藏的第九章图片  ImageButton blankButton = (ImageButton) findViewById(R.id.btn_02x02);  // 但是,这个控件还是不可见的,设置为不可见可见  blankButton.setVisibility(View.INVISIBLE);  blankImgid = R.id.btn_02x02;// 初始化时候空白区域的按钮id  blankSwap = length - 1; } // 判断拼图是否成功 public void gameOver() {  boolean loop = true;  for (int i = 0; i < imageIndex.length; i++) {   if (imageIndex[i] != i) {    loop = false;   }  }  if (loop) {   // 成功后,时间停止   timeswitch = false;   // 玩家拼图成功,禁止图像按钮移动   button00.setClickable(false);   button01.setClickable(false);   button02.setClickable(false);   button10.setClickable(false);   button11.setClickable(false);   button12.setClickable(false);   button20.setClickable(false);   button21.setClickable(false);   button22.setClickable(false);   button22.setImageDrawable(getResources().getDrawable(image[8]));   button22.setVisibility(View.VISIBLE);   handler.removeMessages(1);   AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);   builder.setMessage("恭喜,拼图成功!您用的时间为" + time + "秒").setPositiveButton("确认", null);   AlertDialog dialog = builder.create();   dialog.show();  } } public void restart(View view) {  time = 0;  restore();  textView.setText("时间:" + time);  timeswitch = true;  handler.sendEmptyMessageDelayed(1,1000);  button00.setClickable(true);  button01.setClickable(true);  button02.setClickable(true);  button10.setClickable(true);  button11.setClickable(true);  button12.setClickable(true);  button20.setClickable(true);  button21.setClickable(true);  button22.setClickable(true);  random(); }}

以上是"Android怎样实现九宫格拼图游戏"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

图片 按钮 空白 时间 区域 就是 控件 成功 数组 时候 更新 九宫 位置 内容 方法 篇文章 要不 还是 随机数 不同 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 计算机网络安全评价标准 epa毒理数据库 vba如何导出数据库 百度软通网络安全答案 网络安全手抄小报文字图片 万年县信远软件开发服务中心 智能科技与互联网 临沂德州软件开发公司 山西浪潮服务器维修哪家便宜 自主可控的内生网络安全 云服务器有哪些 价格怎么样的 手机系统更新提示无法连接服务器 开了流量还显示无法连接服务器 赤峰市网络安全知识答题 花溪软件开发app 疫情下的科技与互联网 全日制软件开发大专 网络安全等级保护标准2.0 戴尔服务器报修 财务网络安全制度 学网络安全与执法出来工资待遇 最常用的网络安全模式是指 平安寿险软件开发招聘 数据库如何去掉字段里的数字 互联网科技结合发展 湖南网络技术学院怎么样 数据库技术第二版概要设计总结 舟山电脑软件开发需要学什么 宏基的服务器指的是什么 网络安全咨询厂家
0