Android實(shí)現(xiàn)九宮格拼圖游戲
經(jīng)常有同學(xué)問到,使用Android能不能開發(fā)游戲呢?能開發(fā)那些游戲呢?由于操作系統(tǒng)和開發(fā)語言局限,一般開發(fā)安卓手機(jī)游戲,我們很少使用其自帶語言開發(fā)。而是使用指定編譯器和語言完成,能夠使界面更流暢,用戶體驗(yàn)感更好。但是對于一些常見小游戲,使用JAVA語言開發(fā)運(yùn)行,還是不在話下的,那在本篇博客中,我將給大家簡單介紹一下,九宮格拼圖游戲的開發(fā)過程,基本邏輯和思路我將在代碼的注釋中體現(xiàn)。
九宮格拼圖游戲,相信大家小時候都玩過。大概邏輯是,將1張圖采用3*3的方式,分成9部分,將第3行3列的小圖取出,打亂剩余的8個部分的位置,然后開始游戲,將打亂的8個位置的圖片通過左右挪動的方式復(fù)位,成功后,將第9張圖歸位,即游戲結(jié)束。
編程時同樣采取了這個邏輯,將切割后的小圖片存放入容器中,然后隨機(jī)拜訪,給每一張小圖設(shè)置點(diǎn)擊事件,點(diǎn)擊后可根據(jù)所缺空隙進(jìn)行挪動,直到全部正確歸位為止,我引入了計時功能,可以記錄完成游戲時間。
那么,接下來我們進(jìn)入正題,開始編寫代碼:
首先編寫拼圖界面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="28sp" android:textStyle="bold" android:textColor="#C00" android:text="耗時:0秒" /> <LinearLayout android:id="@+id/liner_first" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_gravity="center"> <ImageButton android:id="@+id/btn_00x00" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_00x00" android:padding="0dp" /> <ImageButton android:id="@+id/btn_00x01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_00x01" android:padding="0dp" /> <ImageButton android:id="@+id/btn_00x02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_00x02" android:padding="0dp" /> </LinearLayout> <LinearLayout android:id="@+id/liner_second" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_gravity="center"> <ImageButton android:id="@+id/btn_01x00" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_01x00" android:padding="0dp" /> <ImageButton android:id="@+id/btn_01x01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_01x01" android:padding="0dp" /> <ImageButton android:id="@+id/btn_01x02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_01x02" android:padding="0dp" /> </LinearLayout> <LinearLayout android:id="@+id/liner_third" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_gravity="center"> <ImageButton android:id="@+id/btn_02x00" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_02x00" android:padding="0dp" /> <ImageButton android:id="@+id/btn_02x01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_02x01" android:padding="0dp" /> <ImageButton android:id="@+id/btn_02x02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="onClick" android:src="@mipmap/img_xiaoxiong_02x02" android:padding="0dp" android:visibility="invisible" /> </LinearLayout> <Button android:id="@+id/btn_restart" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onClick="restart" android:layout_gravity="center" android:text="重新開始" /> <ImageView android:id="@+id/iv_yuantu" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/yangtu" /> </LinearLayout>
效果圖如下:

接下來,我們編寫拼圖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;// 初始化時候空白區(qū)域的按鈕id
private int time;
private boolean timeswitch = true;
// 聲明一個圖片數(shù)組的下標(biāo)數(shù)組,隨機(jī)排列這個數(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() {
// 為了更新時間用handler更新,其實(shí)就是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();
}
// 監(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)擊按鈕的時候傳過來的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);
// 定義一個新的圖片按鈕,然后findviewbyid空白控件的id
ImageButton blankButton = (ImageButton) findViewById(blankImgid);
// 然后將圖片按鈕重新設(shè)置圖片為我們傳過來的第二個參數(shù)
blankButton.setImageDrawable(getResources().getDrawable(image[imageIndex[site]]));
// 但是,這個控件還是不可見的,設(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);
// 定義一個新的圖片按鈕,然后findviewbyid空白控件的id這個id就是我們初始化的時候設(shè)置隱藏的第九章圖片
ImageButton blankButton = (ImageButton) findViewById(R.id.btn_02x02);
// 但是,這個控件還是不可見的,設(shè)置為不可見可見
blankButton.setVisibility(View.INVISIBLE);
blankImgid = R.id.btn_02x02;// 初始化時候空白區(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) {
// 成功后,時間停止
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("確認(rèn)", 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();
}
}最后運(yùn)行項(xiàng)目,就能夠進(jìn)行拼圖游戲了!效果圖如下:

好了,這就是拼圖游戲了,在我的項(xiàng)目中,我將神仙姐姐的圖片也進(jìn)行了切隔操作,大家可以試試使用神仙姐姐圖片進(jìn)行編程,感謝您的閱讀!
點(diǎn)擊下載相關(guān)項(xiàng)目代碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)拼圖小游戲
- 基于Android平臺實(shí)現(xiàn)拼圖小游戲
- Android實(shí)現(xiàn)美女拼圖游戲詳解
- Android自定義View實(shí)現(xiàn)拼圖小游戲
- Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
- Android拼圖游戲 玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化
- Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
- Android 簡單的實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
- Android Studio做超好玩的拼圖游戲 附送詳細(xì)注釋源碼
- Android實(shí)現(xiàn)九格智能拼圖算法
相關(guān)文章
Android實(shí)現(xiàn)兩個數(shù)相加功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)兩個數(shù)相加功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
Android自定義水波紋動畫Layout實(shí)例代碼
這篇文章主要介紹了Android自定義水波紋動畫Layout的實(shí)例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11
Android ListView下拉刷新上拉自動加載更多DEMO示例
這篇文章主要介紹了Android ListView下拉刷新上拉自動加載更多DEMO示例的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
Android性能優(yōu)化之利用Rxlifecycle解決RxJava內(nèi)存泄漏詳解
RxJava作為一種響應(yīng)式編程框架,是目前編程界網(wǎng)紅,可謂是家喻戶曉,其簡潔的編碼風(fēng)格、易用易讀的鏈?zhǔn)椒椒ㄕ{(diào)用、強(qiáng)大的異步支持等使得RxJava被廣泛使用。2017-01-01
OpenGL ES正交投影實(shí)現(xiàn)方法(三)
這篇文章主要為大家詳細(xì)介紹了OpenGL ES正交投影的實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android MarkTipsView文字標(biāo)識控件使用方法
這篇文章主要為大家詳細(xì)介紹了Android MarkTipsView文字標(biāo)識控件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-04-04
Android開發(fā)之菜單(menu)用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)之菜單(menu)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android菜單的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-03-03

