網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)拼圖游戲的具體代碼,供大家參考,具體內(nèi)容如下
本人是用 android studio 完成的
源碼
package packageName; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import MyImg; public class MainActivity extends AppCompatActivity { ? ? // 顯示圖片的寬度 ? ? public static final int W = 250; ? ? // 左上邊距 ? ? public static final int MARGIN = 200; ? ? // 空圖片的索引 ? ? public static final int NULLINDEX = 0; ? ? private MyImg[] imgs = new MyImg[9]; ? ? // 存儲圖片位置的地圖 ? ? private int[] map = new int[9]; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? // 主布局沒設(shè)置啥東西 ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? // 用于設(shè)置生成 view 對象的寬高 ? ? ? ? ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ? ? ? ? initImg(params); ? ? ? ? randomMap(); ? ? ? ? addImg(params); ? ? ? ? // 開始新游戲 ? ? ? ? Button newBtn = new Button(this); ? ? ? ? newBtn.setText("新游戲"); ? ? ? ? newBtn.setTextSize(16); ? ? ? ? newBtn.setX(40); ? ? ? ? // 添加控件要用的 ? ? ? ? ViewGroup.LayoutParams p1= new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ? ? ? ? addContentView(newBtn, p1); ? ? ? ? newBtn.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? newGame(); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ?? ?// 添加圖片到布局中并設(shè)置點擊事件 ? ? private void addImg(ViewGroup.LayoutParams params) { ? ? ? ? for (int i = 0; i < 3; i++) { ? ? ? ? ? ? for (int j = 0; j < 3; j++) { ? ? ? ? ? ? ? ? int index = i * 3 + j; ? ? ? ? ? ? ? ? // 計算x, y坐標 ? ? ? ? ? ? ? ? int x = j * W + MARGIN; ? ? ? ? ? ? ? ? int y = i * W + MARGIN; ? ? ? ? ? ? ? ? ImageView imgView = imgs[map[index]].getImg(); ? ? ? ? ? ? ? ? imgView.setX(x); ? ? ? ? ? ? ? ? imgView.setY(y); ? ? ? ? ? ? ? ? addContentView(imgView, params); ? ? ? ? ? ? ? ? imgView.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? ? ? ? ? ImageView tempImg = imgs[NULLINDEX].getImg(); ? ? ? ? ? ? ? ? ? ? ? ? int x = (int) v.getX(); ? ? ? ? ? ? ? ? ? ? ? ? int y = (int) v.getY(); ? ? ? ? ? ? ? ? ? ? ? ? // goal image ? ? ? ? ? ? ? ? ? ? ? ? int x1 = (int) tempImg.getX(); ? ? ? ? ? ? ? ? ? ? ? ? int y1 = (int) tempImg.getY(); ? ? ? ? ? ? ? ? ? ? ? ? // move top ? ? ? ? ? ? ? ? ? ? ? ? if (y - y1 == W && x == x1) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? top((ImageView) v); ? ? ? ? ? ? ? ? ? ? ? ? } else if (y - y1 == -W && x == x1) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? down((ImageView) v); ? ? ? ? ? ? ? ? ? ? ? ? } else if (x - x1 == W && y == y1) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? left((ImageView) v); ? ? ? ? ? ? ? ? ? ? ? ? } else if (x - x1 == -W && y == y1) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? right((ImageView) v); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if (isWin()) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "You Win!", Toast.LENGTH_SHORT).show(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? private void newGame() { ? ? ? ? randomMap(); ? ? ? ? // 設(shè)置圖片的 x, y坐標 ? ? ? ? for (int i = 0; i < 3; i++) { ? ? ? ? ? ? for (int j = 0; j < 3; j++) { ? ? ? ? ? ? ? ? int index = i * 3 + j; ? ? ? ? ? ? ? ? int x = j * W + MARGIN; ? ? ? ? ? ? ? ? int y = i * W + MARGIN; ? ? ? ? ? ? ? ? ImageView imgView = imgs[map[index]].getImg(); ? ? ? ? ? ? ? ? imgView.setX(x); ? ? ? ? ? ? ? ? imgView.setY(y); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? ? private void left(ImageView img) { ? ? ? ? img.setX(img.getX() - W); ? ? ? ? imgs[NULLINDEX].getImg().setX(img.getX() + W); ? ? } ? ? private void right(ImageView img) { ? ? ? ? img.setX(img.getX() + W); ? ? ? ? imgs[NULLINDEX].getImg().setX(img.getX() - W); ? ? } ? ? private void top(ImageView img) { ? ? ? ? img.setY(img.getY() - W); ? ? ? ? imgs[NULLINDEX].getImg().setY(img.getY() + W); ? ? } ? ? private void down(ImageView img) { ? ? ? ? img.setY(img.getY() + W); ? ? ? ? imgs[NULLINDEX].getImg().setY(img.getY() - W); ? ? } ? ? private boolean isWin() { ? ? ? ? // 根據(jù) x, y的坐標算出圖片的位置,假如一一對應的話,那么久是贏了 ? ? ? ? for (int i = 0; i < 9; i++) { ? ? ? ? ? ? ImageView img = imgs[i].getImg(); ? ? ? ? ? ? int x = (int) img.getX(); ? ? ? ? ? ? int y = (int) img.getY(); ? ? ? ? ? ? int index = (y - MARGIN) / W * 3 + (x - MARGIN) / W; ? ? ? ? ? ? // 有一個沒對上,就是沒贏 ? ? ? ? ? ? if (index != imgs[i].getType()) { ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return true; ? ? } ? ? private void randomMap() { ? ? ? ? // 打亂地圖的位置 ? ? ? ? int a, b; ? ? ? ? for (int i = 0; i < 50; i++) { ? ? ? ? ? ? a = (int) (Math.random() * 9); ? ? ? ? ? ? b = (int) (Math.random() * 9); ? ? ? ? ? ? int t = map[a]; ? ? ? ? ? ? map[a] = map[b]; ? ? ? ? ? ? map[b] = t; ? ? ? ? } ? ? } ? ? // 安排圖片數(shù)組 ? ? private void initImg(ViewGroup.LayoutParams params) { ? ? ? ? int[] imgId = {R.drawable.img10, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, ? ? ? ? ? ? ? ? R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9}; ? ? ? ? for (int i = 0; i < 3; i++) { ? ? ? ? ? ? for (int j = 0; j < 3; j++) { ? ? ? ? ? ? ? ? int index = i * 3 + j; ? ? ? ? ? ? ? ? imgs[index] = new MyImg(index); ? ? ? ? ? ? ? ? MyImg img = imgs[index]; ? ? ? ? ? ? ? ? ImageView image = new ImageView(this); ? ? ? ? ? ? ? ? params.width = W; ? ? ? ? ? ? ? ? params.height = W; ? ? ? ? ? ? ? ? image.setLayoutParams(params); ? ? ? ? ? ? ? ? image.setImageResource(imgId[index]); ? ? ? ? ? ? ? ? img.setImg(image); ? ? ? ? ? ? ? ? // 讓地圖初始化 ? ? ? ? ? ? ? ? map[index] = index; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
MyImg類
package packageName; import android.widget.ImageView; public class MyImg { ?? ?// 用于存儲圖片位置的索引 ? ? private int type; ? ? private ImageView img; ? ? public MyImg(int type) { ? ? ? ? this.type = type; ? ? } ? ? public void setImg(ImageView img) { ? ? ? ? this.img = img; ? ? } ? ? public ImageView getImg() { ? ? ? ? return img; ? ? } ?? ?// 獲取圖片索引 ? ? public int getType() { ? ? ? ? return type; ? ? } }
原文鏈接:https://blog.csdn.net/weixin_44116706/article/details/98784787
相關(guān)推薦
- 2022-04-22 element在使用el-row與el-col排序混亂問題
- 2022-09-15 Android?Jetpack庫剖析之ViewModel組件篇_Android
- 2022-05-04 詳解Python函數(shù)式編程之裝飾器_python
- 2022-01-31 torch.save實現(xiàn)對網(wǎng)絡(luò)結(jié)構(gòu)和模型參數(shù)的保存 & pytorch模型文件.pt .pt
- 2022-12-23 Android同步異步任務(wù)與多線程及Handler消息處理機制基礎(chǔ)詳細講解_Android
- 2023-07-27 el-select下拉框處理分頁數(shù)據(jù),觸底加載更多
- 2022-01-18 VSCode git拉取代碼,提示:在簽出前,請清理存儲庫工作樹。
- 2022-09-24 python?繪制3D圖案例分享_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支