這篇文章主要介紹Android怎樣實(shí)現(xiàn)九宮格拼圖游戲,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
10年積累的成都網(wǎng)站建設(shè)、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有涇川免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。首先編寫拼圖界面布局:
接下來,我們編寫拼圖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;// 初始化時(shí)候空白區(qū)域的按鈕id private int time; private boolean timeswitch = true; // 聲明一個(gè)圖片數(shù)組的下標(biāo)數(shù)組,隨機(jī)排列這個(gè)數(shù)組 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() { // 為了更新時(shí)間用handler更新,其實(shí)就是textView.settext(時(shí)間) public void handleMessage(Message msg){ if (msg.what == 1) { textView.setText("時(shí)間:" + 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(); } // 監(jiān)聽方法 private void random() { timeswitch = true; for (int i = 0; i < imageIndex.length; i++) { // 利用循環(huán)講數(shù)組存入值為012345678 imageIndex[i] = i; } int rand1, rand2; for (int j = 0; j < 20; j++) { // math.random 0-1的隨機(jī)數(shù),乘以8就是0-8的隨機(jī)數(shù) rand1 = (int) (Math.random() * (length - 1)); do { rand2 = (int) (Math.random() * (length - 1)); if (rand1 != rand2) { break; } } while (true); swap(rand1, rand2); } // 隨機(jī)排列 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就是點(diǎn)擊按鈕的時(shí)候傳過來的button的id int id = view.getId(); // 通過id進(jìn)行條件語句的執(zhí)行 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; } } // 點(diǎn)擊的圖片與空白區(qū)域的交換的方法 public void move(int imagbtnId, int site) { int sitex = site / Imagex;// site 為第幾張圖片 int sitey = site % Imagey; // 初始化空白處的坐標(biāo) 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); // 定義一個(gè)新的圖片按鈕,然后findviewbyid空白控件的id ImageButton blankButton = (ImageButton) findViewById(blankImgid); // 然后將圖片按鈕重新設(shè)置圖片為我們傳過來的第二個(gè)參數(shù) blankButton.setImageDrawable(getResources().getDrawable(image[imageIndex[site]])); // 但是,這個(gè)控件還是不可見的,設(shè)置為可見 blankButton.setVisibility(View.VISIBLE); swap(site, blankSwap); // 將新的空白區(qū)域位置更新等于傳過來的點(diǎn)擊的按鈕的位置 blankSwap = site; // 將新的空白區(qū)域的id更新為傳過來的點(diǎn)擊的按鈕的id blankImgid = imagbtnId; } gameOver(); } // 如果重新開始,我們要還原被點(diǎn)擊的圖片按鈕變成初始化的模樣 public void restore() { handler.removeMessages(1); // 定義新的imagebutton 等于我們新的空白圖片按鈕id,并且設(shè)置可見, ImageButton clickButton = (ImageButton) findViewById(blankImgid); clickButton.setVisibility(View.VISIBLE); // 定義一個(gè)新的圖片按鈕,然后findviewbyid空白控件的id這個(gè)id就是我們初始化的時(shí)候設(shè)置隱藏的第九章圖片 ImageButton blankButton = (ImageButton) findViewById(R.id.btn_02x02); // 但是,這個(gè)控件還是不可見的,設(shè)置為不可見可見 blankButton.setVisibility(View.INVISIBLE); blankImgid = R.id.btn_02x02;// 初始化時(shí)候空白區(qū)域的按鈕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) { // 成功后,時(shí)間停止 timeswitch = false; // 玩家拼圖成功,禁止圖像按鈕移動(dòng) 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("恭喜,拼圖成功!您用的時(shí)間為" + time + "秒").setPositiveButton("確認(rèn)", null); AlertDialog dialog = builder.create(); dialog.show(); } } public void restart(View view) { time = 0; restore(); textView.setText("時(shí)間:" + 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怎樣實(shí)現(xiàn)九宮格拼圖游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!