Android怎样实现九宫格拼图游戏
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍Android怎样实现九宫格拼图游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先编写拼图界面布局: 接
千家信息网最后更新 2026年11月02日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安全错误
数据库的锁怎样保障安全
科技赋能下的互联网金融
互联网科技巨头监管
美国互联网保险保险科技
互联网科技公司的优势
互联网的科技创新
世界互联网领先科技成果
凡科互联网科技
上海万想互联网科技
天扬科技互联网广告
互联网巨头投资的硬核科技
恒生科技互联网指数分析
华服互联网科技官网
互联网大会黑科技排名
苏州五指互联网络科技有限
半导体行业属于互联网高科技类吗
互联网高科技行业有哪些企业
艺电能源互联网科技有限公司招聘
数字互联网科技有限公司
黑龙江互联网科技公司
中国主要互联网科技公司
高科技互联网
互联网 的科技创新
成都互联网科技公司
互联网前沿科技
多迪互联网科技有限公司
新华互联网科技2019
黑科技闪耀互联网之光
江苏苏星互联网科技有限公司
云鲸互联网络科技有限公司
广东猫王互联网科技有限公司电话