課程
/移動開發
/Android
/快速實現不一樣的移動拼圖
感覺要改變很多代碼,弄了很久都不行
2016-10-29
源自:快速實現不一樣的移動拼圖
正在回答
關鍵代碼,在//加載關卡數據的下面追加
/*?======實現下一關效果=======?*/ //?初始化游戲小方塊 Bitmap?bigBm?=?((BitmapDrawable)?getResources().getDrawable(R.drawable.b)).getBitmap(); int?tuWandH?=?bigBm.getWidth()?/?3;//?每個小方塊的寬和高 //?重新設置對應的要展示的圖片 for?(int?i?=?0;?i?<?iv_game_arr.length;?i++)?{ for?(int?j?=?0;?j?<?iv_game_arr[0].length;?j++)?{ Bitmap?bm?=?Bitmap.createBitmap(bigBm,?j?*?tuWandH,?i?*?tuWandH,?tuWandH,?tuWandH);//?根據行列切成游戲圖片 iv_game_arr[i][j].setImageBitmap(bm);//?設置每一個方塊的圖標 iv_game_arr[i][j].setPadding(2,?2,?2,?2);//?設置方塊間距 iv_game_arr[i][j].setTag(new?GameData(i,?j,?bm));//?綁定自定義數據 } } setNullImageView(iv_game_arr[2][2]);//?設置最后一個方塊是空的 isGameStart?=?false;//?設置游戲未開始,方便下面打亂順序時不顯示動畫。 randomMove();//?初始化隨機打亂順序 isGameStart?=?true;//?開始游戲狀態,此時點擊方塊移動時會有動畫
第二關圖片如下
效果演示
慕勒0695240 提問者
阿旭_ 回復 慕勒0695240 提問者
完整代碼如下:
package?com.example.admin.pintu; import?android.app.Activity; import?android.graphics.Bitmap; import?android.graphics.drawable.BitmapDrawable; import?android.os.Bundle; import?android.os.SystemClock; import?android.view.GestureDetector; import?android.view.MotionEvent; import?android.view.View; import?android.view.animation.Animation; import?android.view.animation.TranslateAnimation; import?android.widget.Chronometer; import?android.widget.GridLayout; import?android.widget.ImageButton; import?android.widget.ImageView; import?android.widget.LinearLayout; import?android.widget.RelativeLayout; import?android.widget.TextView; public?class?MainActivity?extends?Activity?{ private?boolean?isGameStart?=?false;//?判斷游戲是否開始 private?boolean?isGameOver?=?false;//?判斷游戲是否結束 private?boolean?changeDataByImageView?=?false;//?判斷游戲是否交換數據 private?ImageView[][]?iv_game_arr?=?new?ImageView[3][3];//?利用二維數組創建方塊 private?GridLayout?gl_main_game;//?主頁面 private?ImageView?iv_null_ImageView;//?當前空方塊的實例的保存 private?GestureDetector?mDetector;//?當前手勢 private?boolean?isAnimRun?=?false;//?當前動畫是否在執行 private?Chronometer?timer; private?ImageButton?stop; private?View?mPassView; private?TextView?mCurrentTimeSpend; private?TextView?mCurrentStageView; private?int?mCurrentStageIndex?=?0; private?Bitmap?bigBm; //?private?ArrayList<BitmapDrawable>?mBigBm; //?設置手勢 @Override public?boolean?onTouchEvent(MotionEvent?event)?{ return?mDetector.onTouchEvent(event); } @Override public?boolean?dispatchTouchEvent(MotionEvent?ev)?{ mDetector.onTouchEvent(ev); return?super.dispatchTouchEvent(ev); } @Override protected?void?onCreate(Bundle?savedInstanceState)?{ super.onCreate(savedInstanceState); setContentView(R.layout.content_main); //?設置監聽器 mDetector?=?new?GestureDetector(this,?new?GestureDetector.OnGestureListener()?{ @Override public?boolean?onSingleTapUp(MotionEvent?arg0)?{ return?false; } @Override public?void?onShowPress(MotionEvent?arg0)?{ } @Override public?boolean?onScroll(MotionEvent?arg0,?MotionEvent?arg1,?float?arg2,?float?arg3)?{ return?false; } @Override public?void?onLongPress(MotionEvent?arg0)?{ } @Override public?boolean?onFling(MotionEvent?arg0,?MotionEvent?arg1,?float?arg2,?float?arg3)?{ int?type?=?getDirByGes(arg0.getX(),?arg0.getY(),?arg1.getX(),?arg1.getY()); changeByDir(type);//?傳遞方法 return?false; } @Override public?boolean?onDown(MotionEvent?arg0)?{ return?false; } }); //?獲取時間和停止按鈕資源 this.timer?=?(Chronometer)?findViewById(R.id.chronometer); this.stop?=?(ImageButton)?findViewById(R.id.imageButton); stop.setOnClickListener(isstop); //?顯示當前關的索引 mCurrentStageView?=?(TextView)?findViewById(R.id.textView); if?(mCurrentStageView?!=?null)?{ mCurrentStageView.setText("第"?+?(mCurrentStageIndex?+?1)?+?"關"); } //?初始化游戲小方塊 //?獲取一張大圖 Bitmap?bigBm?=?((BitmapDrawable)?getResources().getDrawable(R.drawable.a)).getBitmap(); int?tuWandH?=?bigBm.getWidth()?/?3;//?每個小方塊的寬和高 int?ivWandH?=?getWindowManager().getDefaultDisplay().getWidth()?/?3;//?小方塊寬應為整個屏幕/3 for?(int?i?=?0;?i?<?iv_game_arr.length;?i++)?{ for?(int?j?=?0;?j?<?iv_game_arr[0].length;?j++)?{ Bitmap?bm?=?Bitmap.createBitmap(bigBm,?j?*?tuWandH,?i?*?tuWandH,?tuWandH,?tuWandH);//?根據行列切成游戲圖片 iv_game_arr[i][j]?=?new?ImageView(this); iv_game_arr[i][j].setImageBitmap(bm);//?設置每一個方塊的圖標 iv_game_arr[i][j].setLayoutParams(new?RelativeLayout.LayoutParams(ivWandH,?ivWandH)); iv_game_arr[i][j].setPadding(2,?2,?2,?2);//?設置方塊間距 iv_game_arr[i][j].setTag(new?GameData(i,?j,?bm));//?綁定自定義數據 iv_game_arr[i][j].setOnClickListener(new?View.OnClickListener()?{ @Override public?void?onClick(View?v)?{ boolean?flag?=?isHasByNullImageView((ImageView)?v); if?(flag)?{ changeDataByImageView((ImageView)?v); } } }); } } //?初始化游戲界面,添加方塊 gl_main_game?=?(GridLayout)?findViewById(R.id.gl_main_game); for?(int?i?=?0;?i?<?iv_game_arr.length;?i++)?{ for?(int?j?=?0;?j?<?iv_game_arr[0].length;?j++)?{ gl_main_game.addView(iv_game_arr[i][j]); } } setNullImageView(iv_game_arr[2][2]);//?設置最后一個方塊是空的 randomMove();//?初始化隨機打亂順序 isGameStart?=?true;//?開始游戲狀態 timer.start(); } //?重載,由于開始100次隨即無動畫 public?void?changeByDir(int?type)?{ changeByDir(type,?true); } //?根據手勢方向獲取空方塊相鄰位置,存在方塊,進行數據交換,1up,2down,3left,4right public?void?changeByDir(int?type,?boolean?isAnim)?{ GameData?mNullGameData?=?(GameData)?iv_null_ImageView.getTag();//?獲取當前空方塊位置 int?new_x?=?mNullGameData.x;//?根據方向設置相應位置坐標 int?new_y?=?mNullGameData.y; if?(type?==?1)?{//?要移動的方塊在空的下面 new_x++; }?else?if?(type?==?2)?{ new_x--; }?else?if?(type?==?3)?{ new_y++; }?else?if?(type?==?4)?{ new_y--; } //?判斷新坐標是否存在 if?(new_x?>=?0?&&?new_x?<?iv_game_arr.length?&&?new_y?>=?0?&&?new_y?<?iv_game_arr[0].length)?{ if?(isAnim)?{ changeDataByImageView(iv_game_arr[new_x][new_y]);//?存在,移動 }?else?{//?無動畫 changeDataByImageView(iv_game_arr[new_x][new_y],?isAnim); } }?else?{ //?不用做什么 } } //?游戲暫停 private?View.OnClickListener?isstop?=?new?View.OnClickListener()?{ @Override public?void?onClick(View?v)?{ timer.stop(); } }; //?判斷游戲結束方法 public?void?isGameOver()?{ boolean?isGameOver?=?true; //?遍歷每個游戲方塊 for?(int?i?=?0;?i?<?iv_game_arr.length;?i++)?{ for?(int?j?=?0;?j?<?iv_game_arr[0].length;?j++)?{ //?為空的方塊數據跳過 if?(iv_game_arr[i][j]?==?iv_null_ImageView)?{ continue; } GameData?mGameData?=?(GameData)?iv_game_arr[i][j].getTag(); if?(!mGameData.isTrue())?{ isGameOver?=?false; break; } } } //?根據一個開關變量決定是否結束 if?(isGameOver)?{ timer.stop(); handlePassEvent(); } } //?處理過關界面 private?void?handlePassEvent()?{ //?顯示過關界面 mPassView?=?(LinearLayout)?findViewById(R.id.pass_view); mPassView.setVisibility(View.VISIBLE); //?當前關的索引 mCurrentTimeSpend?=?(TextView)?findViewById(R.id.time_spend); if?(mCurrentTimeSpend?!=?null)?{ mCurrentTimeSpend.setText("用時:"?+?timer.getText()); } //?下一關按鍵處理 ImageButton?btnPass?=?(ImageButton)?findViewById(R.id.btn_next); btnPass.setOnClickListener(new?View.OnClickListener()?{ @Override public?void?onClick(View?v)?{ /** ?*?if(judegAppPassed){?//進入到通關界面?}else?{ ?*/ //?開始新的一關 mPassView.setVisibility(View.GONE); //?時間清零 timer.setBase(SystemClock.elapsedRealtime()); timer.start(); //?加載關卡數據 /*?======實現下一關效果=======?*/ //?初始化游戲小方塊 Bitmap?bigBm?=?((BitmapDrawable)?getResources().getDrawable(R.drawable.b)).getBitmap(); int?tuWandH?=?bigBm.getWidth()?/?3;//?每個小方塊的寬和高 //?重新設置對應的要展示的圖片 for?(int?i?=?0;?i?<?iv_game_arr.length;?i++)?{ for?(int?j?=?0;?j?<?iv_game_arr[0].length;?j++)?{ Bitmap?bm?=?Bitmap.createBitmap(bigBm,?j?*?tuWandH,?i?*?tuWandH,?tuWandH,?tuWandH);//?根據行列切成游戲圖片 iv_game_arr[i][j].setImageBitmap(bm);//?設置每一個方塊的圖標 iv_game_arr[i][j].setPadding(2,?2,?2,?2);//?設置方塊間距 iv_game_arr[i][j].setTag(new?GameData(i,?j,?bm));//?綁定自定義數據 } } setNullImageView(iv_game_arr[2][2]);//?設置最后一個方塊是空的 isGameStart?=?false;//?設置游戲未開始,方便下面打亂順序時不顯示動畫。 randomMove();//?初始化隨機打亂順序 isGameStart?=?true;//?開始游戲狀態,此時點擊方塊移動時會有動畫 } }); } //?判斷是否通關 /** ?*?private?boolean?judegAppPassed(){?return?(mCurrentStageIndex== ?*?Const.SONG_INFO.length?-?1);?} ?*/ //?手勢判斷 public?int?getDirByGes(float?start_x,?float?start_y,?float?end_x,?float?end_y)?{ boolean?isLeftOrRight?=?(Math.abs(start_x?-?end_x)?>?Math.abs(start_y?-?end_y))???true?:?false;//?是否是左右 if?(isLeftOrRight)?{//?左右 boolean?isLeft?=?start_x?-?end_x?>?0???true?:?false; if?(isLeft)?{ return?3; }?else?{ return?4; } }?else?{//?上下 boolean?isUp?=?start_y?-?end_y?>?0???true?:?false; if?(isUp)?{ return?1; }?else?{ return?2; } } //?1up,2down,3left,4right } //?隨即打亂順序 public?void?randomMove()?{ //?打亂次數 for?(int?i?=?0;?i?<?10;?i++)?{ //?開始交換,無動畫 int?type?=?(int)?(Math.random()?*?4)?+?1; changeByDir(type,?false); } } public?void?changeDataByImageView(final?ImageView?miImageView)?{//?重載有動畫 changeDataByImageView(miImageView,?true); } //?利用動畫結束之后,交換兩個方塊數據 public?void?changeDataByImageView(final?ImageView?miImageView,?boolean?isAnim)?{ if?(isAnimRun)?{ return; } if?(!isAnim)?{ GameData?mGameData?=?(GameData)?miImageView.getTag(); iv_null_ImageView.setImageBitmap(mGameData.bm); GameData?mNullGameData?=?(GameData)?iv_null_ImageView.getTag(); mNullGameData.bm?=?mGameData.bm; mNullGameData.p_x?=?mGameData.p_x; mNullGameData.p_y?=?mGameData.p_y; setNullImageView(miImageView);//?設置當前點擊的為空方塊 if?(isGameStart)?{ isGameOver();//?成功時,彈出一個toast } return; } TranslateAnimation?translateAnimation?=?null;//?創建一個動畫,設置方向,移動距離 if?(miImageView.getX()?>?iv_null_ImageView.getX())?{//?當前點擊在空之上 translateAnimation?=?new?TranslateAnimation(0.1f,?-miImageView.getWidth(),?0.1f,?0.1f);//?向上移動 }?else?if?(miImageView.getX()?<?iv_null_ImageView.getX())?{ translateAnimation?=?new?TranslateAnimation(0.1f,?miImageView.getWidth(),?0.1f,?0.1f);//?向下移動 }?else?if?(miImageView.getY()?>?iv_null_ImageView.getY())?{ translateAnimation?=?new?TranslateAnimation(0.1f,?0.1f,?0.1f,?-miImageView.getHeight());//?向左移動 }?else?if?(miImageView.getY()?<?iv_null_ImageView.getY())?{ translateAnimation?=?new?TranslateAnimation(0.1f,?0.1f,?0.1f,?miImageView.getHeight());//?向右移動 } translateAnimation.setDuration(70);//?設置動畫時常,ms translateAnimation.setFillAfter(true);//?設置動畫結束之后是否停留 //?設置動畫結束之后真正交換數據 translateAnimation.setAnimationListener(new?Animation.AnimationListener()?{ @Override public?void?onAnimationStart(Animation?animation)?{ isAnimRun?=?true; } @Override public?void?onAnimationRepeat(Animation?animation)?{ } @Override public?void?onAnimationEnd(Animation?animation)?{ isAnimRun?=?false; miImageView.clearAnimation(); GameData?mGameData?=?(GameData)?miImageView.getTag(); iv_null_ImageView.setImageBitmap(mGameData.bm); GameData?mNullGameData?=?(GameData)?iv_null_ImageView.getTag(); mNullGameData.bm?=?mGameData.bm; mNullGameData.p_x?=?mGameData.p_x; mNullGameData.p_y?=?mGameData.p_y; setNullImageView(miImageView);//?設置當前點擊的為空方塊 if?(isGameStart)?{ isGameOver(); } } }); miImageView.startAnimation(translateAnimation);//?執行動畫 } //?設置空方塊,mImageView是當前設置為空方塊的實例 public?void?setNullImageView(ImageView?mImageView)?{ mImageView.setImageBitmap(null); iv_null_ImageView?=?mImageView; } //?判斷當前點擊的是否是空方塊位置為相鄰關系,mImageView所點擊的方塊 public?boolean?isHasByNullImageView(ImageView?mImageView)?{ //?分別獲取當前空方塊的位置與點擊方塊的位置 GameData?mNullGameData?=?(GameData)?iv_null_ImageView.getTag(); GameData?mGameData?=?(GameData)?mImageView.getTag(); if?(mNullGameData.y?==?mGameData.y?&&?mNullGameData.x?+?1?==?mGameData.x)?{//?當前點擊方塊在空方塊上面 return?true; }?else?if?(mNullGameData.y?==?mGameData.y?&&?mNullGameData.x?-?1?==?mGameData.x)?{//?當前點擊方塊在空方塊下面 return?true; }?else?if?(mNullGameData.y?==?mGameData.y?+?1?&&?mNullGameData.x?==?mGameData.x)?{//?當前點擊方塊在空方塊左面 return?true; }?else?if?(mNullGameData.y?==?mGameData.y?-?1?&&?mNullGameData.x?==?mGameData.x)?{//?當前點擊方塊在空方塊右面 return?true; } return?false; } //?小方塊的監測數據 class?GameData?{ public?int?x?=?0;//?方塊位置x public?int?y?=?0;//?方塊位置y public?Bitmap?bm;//?方塊圖片 public?int?p_x;//?圖片位置x public?int?p_y;//?圖片位置y public?GameData(int?x,?int?y,?Bitmap?bm)?{ super(); this.x?=?x; this.y?=?y; this.bm?=?bm; this.p_x?=?x; this.p_y?=?y; } //?方塊的位置是否正確 public?boolean?isTrue()?{ if?(x?==?p_x?&&?y?==?p_y)?{ return?true; } return?false; } } }
寫一個方法用來重置圖片即可。
步驟1:方法的參數是Bitmap的。
步驟2:重新為每個ImageView設置新的圖片,綁定新的GameData
步驟3:調用“設置最后一個方塊是空的”和“初始化隨機打亂順序方塊”方法。
如果還是實現不了,請把你的代碼貼上來,我幫你實現再發給你。
。。。
阿旭_
慕勒0695240 提問者 回復 阿旭_
點擊下一關后會出現這個
? //處理過關界面
? ? private void handlePassEvent(){
? ? ? ? //顯示過關界面
? ? ? ? mPassView = (LinearLayout)findViewById(R.id.pass_view);
? ? ? ? mPassView.setVisibility(View.VISIBLE);
? ? ? ? //當前關的索引
? ? ? ? mCurrentTimeSpend=(TextView)findViewById(R.id.time_spend);
? ? ? ? if(mCurrentTimeSpend!=null){
? ? ? ? ? ? mCurrentTimeSpend.setText("用時:"+timer.getText());
? ? ? ? }
? ? ? ? //下一關按鍵處理
? ? ? ? ImageButton btnPass = ( ImageButton)findViewById(R.id.btn_next);
? ? ? ? btnPass.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? /** ? if(judegAppPassed){
? ? ? ? ? ? ? ? ?//進入到通關界面
? ? ? ? ? ? ? ? ?}else {*/
? ? ? ? ? ? ? ? //開始新的一關
? ? ? ? ? ? ? ? mPassView.setVisibility(View.GONE);
? ? ? ? ? ? ? ? //時間清零
? ? ? ? ? ? ? ? timer.setBase(SystemClock.elapsedRealtime());
? ? ? ? ? ? ? ? timer.start();
? ? ? ? ? ? ? ? //加載關卡數據
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? ?//判斷是否通關
? /** ?private ?boolean judegAppPassed(){
? ? ? ? return (mCurrentStageIndex== Const.SONG_INFO.length - 1);
? ? }*/
? ? //手勢判斷
? ? public int getDirByGes(float start_x,float start_y,float end_x,float end_y){
? ? ? ? boolean isLeftOrRight=(Math.abs(start_x-end_x)>Math.abs(start_y-end_y))?true:false;//是否是左右
? ? ? ? if(isLeftOrRight){//左右
? ? ? ? ? ? boolean isLeft = start_x-end_x>0?true:false;
? ? ? ? ? ? if(isLeft){
? ? ? ? ? ? ? ? return 3;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return 4;
? ? ? ? }else{//上下
? ? ? ? ? ? boolean isUp = start_y-end_y>0?true:false;
? ? ? ? ? ? if(isUp){
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? return 2;
? ? ? ? //1up,2down,3left,4right
? ? //隨即打亂順序
? ? public void randomMove(){
? ? ? ? //打亂次數
? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? //開始交換,無動畫
? ? ? ? ? ? int type = (int) (Math.random()*4)+1;
? ? ? ? ? ? changeByDir(type,false);
? ? public void changeDataByImageView(final ImageView miImageView){//重載有動畫
? ? ? ? changeDataByImageView(miImageView,true);
? ? //利用動畫結束之后,交換兩個方塊數據
? ? public void changeDataByImageView(final ImageView miImageView,boolean isAnim){
? ? ? ? if (isAnimRun) {
? ? ? ? ? ? return;
? ? ? ? if(!isAnim){
? ? ? ? ? ? GameData mGameData = (GameData) miImageView.getTag();
? ? ? ? ? ? iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ? GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ? mNullGameData.bm = mGameData.bm;
? ? ? ? ? ? mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ? mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ? setNullImageView(miImageView);//設置當前點擊的為空方塊
? ? ? ? ? ? if(isGameStart){
? ? ? ? ? ? ? ? isGameOver();//成功時,彈出一個toast
? ? ? ? TranslateAnimation translateAnimation=null;//創建一個動畫,設置方向,移動距離
? ? ? ? if(miImageView.getX()>iv_null_ImageView.getX()){//當前點擊在空之上
? ? ? ? ? ? translateAnimation = new TranslateAnimation(0.1f,-miImageView.getWidth(), 0.1f,0.1f);//向上移動
? ? ? ? }else if(miImageView.getX()<iv_null_ImageView.getX()){
? ? ? ? ? ? translateAnimation = new TranslateAnimation( 0.1f,miImageView.getWidth(),0.1f,0.1f);//向下移動
? ? ? ? }else if(miImageView.getY()>iv_null_ImageView.getY()){
? ? ? ? ? ? translateAnimation = new TranslateAnimation(0.1f, 0.1f,0.1f,-miImageView.getHeight());//向左移動
? ? ? ? }else if(miImageView.getY()<iv_null_ImageView.getY()){
? ? ? ? ? ? translateAnimation = new TranslateAnimation(0.1f, 0.1f,0.1f,miImageView.getHeight());//向右移動
? ? ? ? translateAnimation.setDuration(70);//設置動畫時常,ms
? ? ? ? translateAnimation.setFillAfter(true);//設置動畫結束之后是否停留
? ? ? ? //設置動畫結束之后真正交換數據
? ? ? ? translateAnimation.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ? public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ? isAnimRun = true;
? ? ? ? ? ? public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ? public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ? isAnimRun = false;
? ? ? ? ? ? ? ? miImageView.clearAnimation();
? ? ? ? ? ? ? ? GameData mGameData = (GameData) miImageView.getTag();
? ? ? ? ? ? ? ? iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ? ? ? GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ? ? ? mNullGameData.bm = mGameData.bm;
? ? ? ? ? ? ? ? mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ? ? ? mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ? ? ? setNullImageView(miImageView);//設置當前點擊的為空方塊
? ? ? ? ? ? ? ? if(isGameStart){
? ? ? ? ? ? ? ? ? ? isGameOver();
? ? ? ? ? ? ? ? }
? ? ? ? miImageView.startAnimation(translateAnimation);//執行動畫
? ? //設置空方塊,mImageView是當前設置為空方塊的實例
? ? public void setNullImageView(ImageView mImageView){
? ? ? ? mImageView.setImageBitmap(null);
? ? ? ? iv_null_ImageView ?= mImageView;
? ? //判斷當前點擊的是否是空方塊位置為相鄰關系,mImageView所點擊的方塊
? ? public boolean isHasByNullImageView(ImageView mImageView){
? ? ? ? //分別獲取當前空方塊的位置與點擊方塊的位置
? ? ? ? GameData mNullGameData=(GameData) iv_null_ImageView.getTag();
? ? ? ? GameData mGameData=(GameData)mImageView.getTag();
? ? ? ? if(mNullGameData.y==mGameData.y&&mNullGameData.x+1==mGameData.x){//當前點擊方塊在空方塊上面
? ? ? ? ? ? return true;
? ? ? ? }else if(mNullGameData.y==mGameData.y&&mNullGameData.x-1==mGameData.x){//當前點擊方塊在空方塊下面
? ? ? ? }else if(mNullGameData.y==mGameData.y+1&&mNullGameData.x==mGameData.x){//當前點擊方塊在空方塊左面
? ? ? ? }else if(mNullGameData.y==mGameData.y-1&&mNullGameData.x==mGameData.x){//當前點擊方塊在空方塊右面
? ? ? ? return false;
? ? //小方塊的監測數據
? ? class GameData{
? ? ? ? public int x=0;//方塊位置x
? ? ? ? public int y=0;//方塊位置y
? ? ? ? public ?Bitmap bm;//方塊圖片
? ? ? ? public int p_x;//圖片位置x
? ? ? ? public int p_y;//圖片位置y
? ? ? ? public GameData(int x, int y, Bitmap bm) {
? ? ? ? ? ? super();
? ? ? ? ? ? this.x = x;
? ? ? ? ? ? this.y = y;
? ? ? ? ? ? this.bm = bm;
? ? ? ? ? ? this.p_x = x;
? ? ? ? ? ? this.p_y = y;
? ? ? ? //方塊的位置是否正確
? ? ? ? public boolean isTrue() {
? ? ? ? ? ? if (x==p_x&&y==p_y) {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? return false;
}
package com.example.admin.pintu;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Chronometer;
import android.widget.ExpandableListView;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.admin.R;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
? ? private boolean isGameStart = false;//判斷游戲是否開始
? ? private boolean isGameOver = false;//判斷游戲是否結束
? ? private boolean changeDataByImageView=false;//判斷游戲是否交換數據
? ? private ImageView[][] iv_game_arr = new ImageView[3][3];//利用二維數組創建方塊
? ? private GridLayout gl_main_game;//主頁面
? ? private ImageView iv_null_ImageView;//當前空方塊的實例的保存
? ? private GestureDetector mDetector;//當前手勢
? ? private boolean isAnimRun= false;//當前動畫是否在執行
? ? private Chronometer timer;
? ? private ImageButton stop;
? ? private View mPassView;
? ? private TextView mCurrentTimeSpend;
? ? private TextView mCurrentStageView;
? ? private int ?mCurrentStageIndex = 0;
? ? private ?Bitmap bigBm;
? // ?private ArrayList<BitmapDrawable> mBigBm;
? ? //設置手勢
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? return mDetector.onTouchEvent(event);
? ? public boolean dispatchTouchEvent(MotionEvent ev) {
? ? ? ? mDetector.onTouchEvent(ev);
? ? ? ? return super.dispatchTouchEvent(ev);
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.content_main);
? ? ? ? //設置監聽器
? ? ? ? mDetector = new GestureDetector(this,new GestureDetector.OnGestureListener() {
? ? ? ? ? ? public boolean onSingleTapUp(MotionEvent arg0) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? public void onShowPress(MotionEvent arg0) {
? ? ? ? ? ? public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? float arg3) {
? ? ? ? ? ? public void onLongPress(MotionEvent arg0) {
? ? ? ? ? ? public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,float arg3) {
? ? ? ? ? ? ? ? int type = getDirByGes(arg0.getX(), arg0.getY(), arg1.getX(), arg1.getY());
? ? ? ? ? ? ? ? changeByDir(type);//傳遞方法
? ? ? ? ? ? public boolean onDown(MotionEvent arg0) {
? ? ? ? //獲取時間和停止按鈕資源
? ? ? ? this.timer = (Chronometer)findViewById(R.id.chronometer);
? ? ? ? this.stop=(ImageButton)findViewById(R.id.imageButton);
? ? ? ? stop.setOnClickListener(isstop);
? ? ? ? //顯示當前關的索引
? ? ? ? mCurrentStageView = (TextView) findViewById(R.id.textView);
? ? ? ? if (mCurrentStageView != null) {
? ? ? ? ? ? mCurrentStageView.setText("第" + (mCurrentStageIndex + 1) + "關");
? ? ? ? //初始化游戲小方塊
? ? ? ? ? ? //獲取一張大圖
? ? ? ? ? ? Bitmap ?bigBm = ((BitmapDrawable) getResources().getDrawable(R.drawable.a)).getBitmap();
? ? ? ? ? ? int tuWandH = bigBm.getWidth() / 3;//每個小方塊的寬和高
? ? ? ? ? ? int ivWandH = getWindowManager().getDefaultDisplay().getWidth() / 3;//小方塊寬應為整個屏幕/3
? ? ? ? ? ? for (int i = 0; i < iv_game_arr.length; i++) {
? ? ? ? ? ? ? ? for (int j = 0; j < iv_game_arr[0].length; j++) {
? ? ? ? ? ? ? ? ? ? Bitmap bm = Bitmap.createBitmap(bigBm, j * tuWandH, i * tuWandH, tuWandH, tuWandH);//根據行列切成游戲圖片
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j] = new ImageView(this);
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setImageBitmap(bm);//設置每一個方塊的圖標
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setLayoutParams(new RelativeLayout.LayoutParams(ivWandH, ivWandH));
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setPadding(2, 2, 2, 2);//設置方塊間距
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setTag(new GameData(i, j, bm));//綁定自定義數據
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? boolean flag = isHasByNullImageView((ImageView) v);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? changeDataByImageView((ImageView) v);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? //初始化游戲界面,添加方塊
? ? ? ? ? ? gl_main_game = (GridLayout) findViewById(R.id.gl_main_game);
? ? ? ? ? ? ? ? ? ? gl_main_game.addView(iv_game_arr[i][j]);
? ? ? ? ? ? setNullImageView(iv_game_arr[2][2]);//設置最后一個方塊是空的
? ? ? ? ? ? randomMove();//初始化隨機打亂順序
? ? ? ? ? ? isGameStart = true;//開始游戲狀態
? ? ? ? ? ? timer.start();
? ? //重載,由于開始100次隨即無動畫
? ? public void changeByDir(int type){
? ? ? ? changeByDir(type,true);
? ? //根據手勢方向獲取空方塊相鄰位置,存在方塊,進行數據交換,1up,2down,3left,4right
? ? public void changeByDir(int type,boolean isAnim){
? ? ? ? GameData mNullGameData=(GameData) iv_null_ImageView.getTag();//獲取當前空方塊位置
? ? ? ? int new_x=mNullGameData.x;//根據方向設置相應位置坐標
? ? ? ? int new_y=mNullGameData.y;
? ? ? ? if (type==1) {//要移動的方塊在空的下面
? ? ? ? ? ? new_x++;
? ? ? ? }else if (type==2) {
? ? ? ? ? ? new_x--;
? ? ? ? }else if (type==3) {
? ? ? ? ? ? new_y++;
? ? ? ? }else if (type==4) {
? ? ? ? ? ? new_y--;
? ? ? ? //判斷新坐標是否存在
? ? ? ? if(new_x>=0&&new_x<iv_game_arr.length&&new_y>=0&&new_y<iv_game_arr[0].length){
? ? ? ? ? ? if (isAnim) {
? ? ? ? ? ? ? ? changeDataByImageView(iv_game_arr[new_x][new_y]);//存在,移動
? ? ? ? ? ? }else {//無動畫
? ? ? ? ? ? ? ? changeDataByImageView(iv_game_arr[new_x][new_y],isAnim);
? ? ? ? }else {
? ? ? ? ? ? //不用做什么
? ? //游戲暫停
? ? private View.OnClickListener isstop=new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? timer.stop();
? ? };
? ? //判斷游戲結束方法
? ? public void isGameOver(){
? ? ? ? boolean isGameOver = true;
? ? ? ? //遍歷每個游戲方塊
? ? ? ? for (int i = 0; i < iv_game_arr.length; i++) {
? ? ? ? ? ? for (int j = 0; j < iv_game_arr[0].length; j++) {
? ? ? ? ? ? ? ? //為空的方塊數據跳過
? ? ? ? ? ? ? ? if (iv_game_arr[i][j]==iv_null_ImageView) {
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? GameData mGameData=(GameData) iv_game_arr[i][j].getTag();
? ? ? ? ? ? ? ? if(!mGameData.isTrue()){
? ? ? ? ? ? ? ? ? ? isGameOver = false;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? //根據一個開關變量決定是否結束
? ? ? ? if (isGameOver) {
? ? ? ? ? ? handlePassEvent();
??
舉報
實現一個支持手勢的移動拼圖小游戲,手把手帶你開發小游戲
1 回答手勢設置的那一張為什么只能在空白處滑動才可以?
1 回答圖片縮放的方法應該加入
1 回答圖片的問題
2 回答圖片超出屏幕
2 回答圖片不能換行
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-10-29
關鍵代碼,在//加載關卡數據的下面追加
第二關圖片如下
效果演示
2016-10-29
完整代碼如下:
2016-10-29
寫一個方法用來重置圖片即可。
步驟1:方法的參數是Bitmap的。
步驟2:重新為每個ImageView設置新的圖片,綁定新的GameData
步驟3:調用“設置最后一個方塊是空的”和“初始化隨機打亂順序方塊”方法。
如果還是實現不了,請把你的代碼貼上來,我幫你實現再發給你。
2016-10-29
2016-10-29
2016-10-29
? //處理過關界面
? ? private void handlePassEvent(){
? ? ? ? //顯示過關界面
? ? ? ? mPassView = (LinearLayout)findViewById(R.id.pass_view);
? ? ? ? mPassView.setVisibility(View.VISIBLE);
? ? ? ? //當前關的索引
? ? ? ? mCurrentTimeSpend=(TextView)findViewById(R.id.time_spend);
? ? ? ? if(mCurrentTimeSpend!=null){
? ? ? ? ? ? mCurrentTimeSpend.setText("用時:"+timer.getText());
? ? ? ? }
? ? ? ? //下一關按鍵處理
? ? ? ? ImageButton btnPass = ( ImageButton)findViewById(R.id.btn_next);
? ? ? ? btnPass.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? /** ? if(judegAppPassed){
? ? ? ? ? ? ? ? ?//進入到通關界面
? ? ? ? ? ? ? ? ?}else {*/
? ? ? ? ? ? ? ? //開始新的一關
? ? ? ? ? ? ? ? mPassView.setVisibility(View.GONE);
? ? ? ? ? ? ? ? //時間清零
? ? ? ? ? ? ? ? timer.setBase(SystemClock.elapsedRealtime());
? ? ? ? ? ? ? ? timer.start();
? ? ? ? ? ? ? ? //加載關卡數據
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? ?//判斷是否通關
? /** ?private ?boolean judegAppPassed(){
? ? ? ? return (mCurrentStageIndex== Const.SONG_INFO.length - 1);
? ? }*/
? ? //手勢判斷
? ? public int getDirByGes(float start_x,float start_y,float end_x,float end_y){
? ? ? ? boolean isLeftOrRight=(Math.abs(start_x-end_x)>Math.abs(start_y-end_y))?true:false;//是否是左右
? ? ? ? if(isLeftOrRight){//左右
? ? ? ? ? ? boolean isLeft = start_x-end_x>0?true:false;
? ? ? ? ? ? if(isLeft){
? ? ? ? ? ? ? ? return 3;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return 4;
? ? ? ? ? ? }
? ? ? ? }else{//上下
? ? ? ? ? ? boolean isUp = start_y-end_y>0?true:false;
? ? ? ? ? ? if(isUp){
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return 2;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //1up,2down,3left,4right
? ? }
? ? //隨即打亂順序
? ? public void randomMove(){
? ? ? ? //打亂次數
? ? ? ? for (int i = 0; i < 10; i++) {
? ? ? ? ? ? //開始交換,無動畫
? ? ? ? ? ? int type = (int) (Math.random()*4)+1;
? ? ? ? ? ? changeByDir(type,false);
? ? ? ? }
? ? }
? ? public void changeDataByImageView(final ImageView miImageView){//重載有動畫
? ? ? ? changeDataByImageView(miImageView,true);
? ? }
? ? //利用動畫結束之后,交換兩個方塊數據
? ? public void changeDataByImageView(final ImageView miImageView,boolean isAnim){
? ? ? ? if (isAnimRun) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if(!isAnim){
? ? ? ? ? ? GameData mGameData = (GameData) miImageView.getTag();
? ? ? ? ? ? iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ? GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ? mNullGameData.bm = mGameData.bm;
? ? ? ? ? ? mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ? mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ? setNullImageView(miImageView);//設置當前點擊的為空方塊
? ? ? ? ? ? if(isGameStart){
? ? ? ? ? ? ? ? isGameOver();//成功時,彈出一個toast
? ? ? ? ? ? }
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? TranslateAnimation translateAnimation=null;//創建一個動畫,設置方向,移動距離
? ? ? ? if(miImageView.getX()>iv_null_ImageView.getX()){//當前點擊在空之上
? ? ? ? ? ? translateAnimation = new TranslateAnimation(0.1f,-miImageView.getWidth(), 0.1f,0.1f);//向上移動
? ? ? ? }else if(miImageView.getX()<iv_null_ImageView.getX()){
? ? ? ? ? ? translateAnimation = new TranslateAnimation( 0.1f,miImageView.getWidth(),0.1f,0.1f);//向下移動
? ? ? ? }else if(miImageView.getY()>iv_null_ImageView.getY()){
? ? ? ? ? ? translateAnimation = new TranslateAnimation(0.1f, 0.1f,0.1f,-miImageView.getHeight());//向左移動
? ? ? ? }else if(miImageView.getY()<iv_null_ImageView.getY()){
? ? ? ? ? ? translateAnimation = new TranslateAnimation(0.1f, 0.1f,0.1f,miImageView.getHeight());//向右移動
? ? ? ? }
? ? ? ? translateAnimation.setDuration(70);//設置動畫時常,ms
? ? ? ? translateAnimation.setFillAfter(true);//設置動畫結束之后是否停留
? ? ? ? //設置動畫結束之后真正交換數據
? ? ? ? translateAnimation.setAnimationListener(new Animation.AnimationListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationStart(Animation animation) {
? ? ? ? ? ? ? ? isAnimRun = true;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationRepeat(Animation animation) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onAnimationEnd(Animation animation) {
? ? ? ? ? ? ? ? isAnimRun = false;
? ? ? ? ? ? ? ? miImageView.clearAnimation();
? ? ? ? ? ? ? ? GameData mGameData = (GameData) miImageView.getTag();
? ? ? ? ? ? ? ? iv_null_ImageView.setImageBitmap(mGameData.bm);
? ? ? ? ? ? ? ? GameData mNullGameData = (GameData) iv_null_ImageView.getTag();
? ? ? ? ? ? ? ? mNullGameData.bm = mGameData.bm;
? ? ? ? ? ? ? ? mNullGameData.p_x = mGameData.p_x;
? ? ? ? ? ? ? ? mNullGameData.p_y = mGameData.p_y;
? ? ? ? ? ? ? ? setNullImageView(miImageView);//設置當前點擊的為空方塊
? ? ? ? ? ? ? ? if(isGameStart){
? ? ? ? ? ? ? ? ? ? isGameOver();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? miImageView.startAnimation(translateAnimation);//執行動畫
? ? }
? ? //設置空方塊,mImageView是當前設置為空方塊的實例
? ? public void setNullImageView(ImageView mImageView){
? ? ? ? mImageView.setImageBitmap(null);
? ? ? ? iv_null_ImageView ?= mImageView;
? ? }
? ? //判斷當前點擊的是否是空方塊位置為相鄰關系,mImageView所點擊的方塊
? ? public boolean isHasByNullImageView(ImageView mImageView){
? ? ? ? //分別獲取當前空方塊的位置與點擊方塊的位置
? ? ? ? GameData mNullGameData=(GameData) iv_null_ImageView.getTag();
? ? ? ? GameData mGameData=(GameData)mImageView.getTag();
? ? ? ? if(mNullGameData.y==mGameData.y&&mNullGameData.x+1==mGameData.x){//當前點擊方塊在空方塊上面
? ? ? ? ? ? return true;
? ? ? ? }else if(mNullGameData.y==mGameData.y&&mNullGameData.x-1==mGameData.x){//當前點擊方塊在空方塊下面
? ? ? ? ? ? return true;
? ? ? ? }else if(mNullGameData.y==mGameData.y+1&&mNullGameData.x==mGameData.x){//當前點擊方塊在空方塊左面
? ? ? ? ? ? return true;
? ? ? ? }else if(mNullGameData.y==mGameData.y-1&&mNullGameData.x==mGameData.x){//當前點擊方塊在空方塊右面
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return false;
? ? }
? ? //小方塊的監測數據
? ? class GameData{
? ? ? ? public int x=0;//方塊位置x
? ? ? ? public int y=0;//方塊位置y
? ? ? ? public ?Bitmap bm;//方塊圖片
? ? ? ? public int p_x;//圖片位置x
? ? ? ? public int p_y;//圖片位置y
? ? ? ? public GameData(int x, int y, Bitmap bm) {
? ? ? ? ? ? super();
? ? ? ? ? ? this.x = x;
? ? ? ? ? ? this.y = y;
? ? ? ? ? ? this.bm = bm;
? ? ? ? ? ? this.p_x = x;
? ? ? ? ? ? this.p_y = y;
? ? ? ? }
? ? ? ? //方塊的位置是否正確
? ? ? ? public boolean isTrue() {
? ? ? ? ? ? if (x==p_x&&y==p_y) {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
}
2016-10-29
package com.example.admin.pintu;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Chronometer;
import android.widget.ExpandableListView;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.admin.R;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
? ? private boolean isGameStart = false;//判斷游戲是否開始
? ? private boolean isGameOver = false;//判斷游戲是否結束
? ? private boolean changeDataByImageView=false;//判斷游戲是否交換數據
? ? private ImageView[][] iv_game_arr = new ImageView[3][3];//利用二維數組創建方塊
? ? private GridLayout gl_main_game;//主頁面
? ? private ImageView iv_null_ImageView;//當前空方塊的實例的保存
? ? private GestureDetector mDetector;//當前手勢
? ? private boolean isAnimRun= false;//當前動畫是否在執行
? ? private Chronometer timer;
? ? private ImageButton stop;
? ? private View mPassView;
? ? private TextView mCurrentTimeSpend;
? ? private TextView mCurrentStageView;
? ? private int ?mCurrentStageIndex = 0;
? ? private ?Bitmap bigBm;
? // ?private ArrayList<BitmapDrawable> mBigBm;
? ? //設置手勢
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? return mDetector.onTouchEvent(event);
? ? }
? ? @Override
? ? public boolean dispatchTouchEvent(MotionEvent ev) {
? ? ? ? mDetector.onTouchEvent(ev);
? ? ? ? return super.dispatchTouchEvent(ev);
? ? }
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.content_main);
? ? ? ? //設置監聽器
? ? ? ? mDetector = new GestureDetector(this,new GestureDetector.OnGestureListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onSingleTapUp(MotionEvent arg0) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onShowPress(MotionEvent arg0) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? float arg3) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onLongPress(MotionEvent arg0) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,float arg3) {
? ? ? ? ? ? ? ? int type = getDirByGes(arg0.getX(), arg0.getY(), arg1.getX(), arg1.getY());
? ? ? ? ? ? ? ? changeByDir(type);//傳遞方法
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onDown(MotionEvent arg0) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //獲取時間和停止按鈕資源
? ? ? ? this.timer = (Chronometer)findViewById(R.id.chronometer);
? ? ? ? this.stop=(ImageButton)findViewById(R.id.imageButton);
? ? ? ? stop.setOnClickListener(isstop);
? ? ? ? //顯示當前關的索引
? ? ? ? mCurrentStageView = (TextView) findViewById(R.id.textView);
? ? ? ? if (mCurrentStageView != null) {
? ? ? ? ? ? mCurrentStageView.setText("第" + (mCurrentStageIndex + 1) + "關");
? ? ? ? }
? ? ? ? //初始化游戲小方塊
? ? ? ? ? ? //獲取一張大圖
? ? ? ? ? ? Bitmap ?bigBm = ((BitmapDrawable) getResources().getDrawable(R.drawable.a)).getBitmap();
? ? ? ? ? ? int tuWandH = bigBm.getWidth() / 3;//每個小方塊的寬和高
? ? ? ? ? ? int ivWandH = getWindowManager().getDefaultDisplay().getWidth() / 3;//小方塊寬應為整個屏幕/3
? ? ? ? ? ? for (int i = 0; i < iv_game_arr.length; i++) {
? ? ? ? ? ? ? ? for (int j = 0; j < iv_game_arr[0].length; j++) {
? ? ? ? ? ? ? ? ? ? Bitmap bm = Bitmap.createBitmap(bigBm, j * tuWandH, i * tuWandH, tuWandH, tuWandH);//根據行列切成游戲圖片
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j] = new ImageView(this);
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setImageBitmap(bm);//設置每一個方塊的圖標
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setLayoutParams(new RelativeLayout.LayoutParams(ivWandH, ivWandH));
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setPadding(2, 2, 2, 2);//設置方塊間距
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setTag(new GameData(i, j, bm));//綁定自定義數據
? ? ? ? ? ? ? ? ? ? iv_game_arr[i][j].setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? boolean flag = isHasByNullImageView((ImageView) v);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? changeDataByImageView((ImageView) v);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? //初始化游戲界面,添加方塊
? ? ? ? ? ? gl_main_game = (GridLayout) findViewById(R.id.gl_main_game);
? ? ? ? ? ? for (int i = 0; i < iv_game_arr.length; i++) {
? ? ? ? ? ? ? ? for (int j = 0; j < iv_game_arr[0].length; j++) {
? ? ? ? ? ? ? ? ? ? gl_main_game.addView(iv_game_arr[i][j]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? setNullImageView(iv_game_arr[2][2]);//設置最后一個方塊是空的
? ? ? ? ? ? randomMove();//初始化隨機打亂順序
? ? ? ? ? ? isGameStart = true;//開始游戲狀態
? ? ? ? ? ? timer.start();
? ? ? ? }
? ? //重載,由于開始100次隨即無動畫
? ? public void changeByDir(int type){
? ? ? ? changeByDir(type,true);
? ? }
? ? //根據手勢方向獲取空方塊相鄰位置,存在方塊,進行數據交換,1up,2down,3left,4right
? ? public void changeByDir(int type,boolean isAnim){
? ? ? ? GameData mNullGameData=(GameData) iv_null_ImageView.getTag();//獲取當前空方塊位置
? ? ? ? int new_x=mNullGameData.x;//根據方向設置相應位置坐標
? ? ? ? int new_y=mNullGameData.y;
? ? ? ? if (type==1) {//要移動的方塊在空的下面
? ? ? ? ? ? new_x++;
? ? ? ? }else if (type==2) {
? ? ? ? ? ? new_x--;
? ? ? ? }else if (type==3) {
? ? ? ? ? ? new_y++;
? ? ? ? }else if (type==4) {
? ? ? ? ? ? new_y--;
? ? ? ? }
? ? ? ? //判斷新坐標是否存在
? ? ? ? if(new_x>=0&&new_x<iv_game_arr.length&&new_y>=0&&new_y<iv_game_arr[0].length){
? ? ? ? ? ? if (isAnim) {
? ? ? ? ? ? ? ? changeDataByImageView(iv_game_arr[new_x][new_y]);//存在,移動
? ? ? ? ? ? }else {//無動畫
? ? ? ? ? ? ? ? changeDataByImageView(iv_game_arr[new_x][new_y],isAnim);
? ? ? ? ? ? }
? ? ? ? }else {
? ? ? ? ? ? //不用做什么
? ? ? ? }
? ? }
? ? //游戲暫停
? ? private View.OnClickListener isstop=new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? timer.stop();
? ? ? ? }
? ? };
? ? //判斷游戲結束方法
? ? public void isGameOver(){
? ? ? ? boolean isGameOver = true;
? ? ? ? //遍歷每個游戲方塊
? ? ? ? for (int i = 0; i < iv_game_arr.length; i++) {
? ? ? ? ? ? for (int j = 0; j < iv_game_arr[0].length; j++) {
? ? ? ? ? ? ? ? //為空的方塊數據跳過
? ? ? ? ? ? ? ? if (iv_game_arr[i][j]==iv_null_ImageView) {
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? GameData mGameData=(GameData) iv_game_arr[i][j].getTag();
? ? ? ? ? ? ? ? if(!mGameData.isTrue()){
? ? ? ? ? ? ? ? ? ? isGameOver = false;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //根據一個開關變量決定是否結束
? ? ? ? if (isGameOver) {
? ? ? ? ? ? timer.stop();
? ? ? ? ? ? handlePassEvent();
? ? ? ? }
? ? }
??