Android怎样实现九宫格拼图游戏
发表于:2025-11-17 作者:千家信息网编辑
千家信息网最后更新 2025年11月17日,这篇文章主要介绍Android怎样实现九宫格拼图游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先编写拼图界面布局: 接
千家信息网最后更新 2025年11月17日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安全错误
数据库的锁怎样保障安全
php数据库插入不成功
江苏爱上门网络技术有限公司
pg4数据库
洛阳联联网络技术
无法连接更新服务器
上海福缔网络技术
常州软件开发技术项目实训中心
联盟服务器选择页面乱码
软件开发 税目
网络安全前沿国际会议
软件开发是一线还是二三线
青少年受骗的网络安全视频
sql数据库安全性实验报告
我的世界失落世界服务器80级
精准普法进行网络安全法讲座
软件开发人员英语翻译
数据库like后需要加空格吗
网络安全专题自查工作总结
移动平台软件开发课程
网络安全法三下乡
银川网络安全学院网络安全培训
凤凰县网络安全宣传
常州软件开发技术项目实训中心
教育科技互联网经济
网络安全公益广告词
网络安全事件动态报道
蓝山县委网络安全委员会
籽岷双人服务器小游戏
应用最广泛的数据库
网络安全广告怎么去除