您好,登錄后才能下訂單哦!
小編給大家分享一下怎么用Android實現拼圖游戲,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內容如下
源碼
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); // 主布局沒設置啥東西 setContentView(R.layout.activity_main); // 用于設置生成 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(); } }); } // 添加圖片到布局中并設置點擊事件 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(); // 設置圖片的 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() { // 根據 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; } } // 安排圖片數組 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; } }
看完了這篇文章,相信你對“怎么用Android實現拼圖游戲”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。