亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

如果要設計成關卡,進入下一關,只改變圖片,要怎么設計

感覺要改變很多代碼,弄了很久都不行

正在回答

7 回答

關鍵代碼,在//加載關卡數據的下面追加

				/*?======實現下一關效果=======?*/
				//?初始化游戲小方塊
				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;//?開始游戲狀態,此時點擊方塊移動時會有動畫

第二關圖片如下

http://img1.sycdn.imooc.com//58143b150001fce503330333.jpg

效果演示

58143b450001426305000889.jpg

58143b4900016dcb05000889.jpg

58143b4e0001bf1005000889.jpg

58143b530001299705000889.jpg


0 回復 有任何疑惑可以回復我~
#1

慕勒0695240 提問者

如果是這種方法,要導入很多圖片的話,就會重復很多代碼,有沒有一種不用寫很多代碼的。我想把要用的圖片放到一個數組里,通過關卡索引來改變圖片
2016-10-29 回復 有任何疑惑可以回復我~
#2

阿旭_ 回復 慕勒0695240 提問者

可以把這段代碼封裝起來,方便的參數是int id類型,然后全局做一個變量控制關卡的變量int t (做為數組的下標,0表示第一關,1表示第二關以此類推),再做一個數組保存所有的圖片id 。int[] arr_img_id = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};于是調用方法法的時候傳的id就變成了arr_img_id[t];即,第N關的的圖片就是arr_img_id[t]
2016-10-29 回復 有任何疑惑可以回復我~

完整代碼如下:

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;
		}
	}
}


0 回復 有任何疑惑可以回復我~

寫一個方法用來重置圖片即可。

步驟1:方法的參數是Bitmap的。

步驟2:重新為每個ImageView設置新的圖片,綁定新的GameData

步驟3:調用“設置最后一個方塊是空的”和“初始化隨機打亂順序方塊”方法。

如果還是實現不了,請把你的代碼貼上來,我幫你實現再發給你。

0 回復 有任何疑惑可以回復我~

http://img1.sycdn.imooc.com//5814572f0001bead12030569.jpg。。。

0 回復 有任何疑惑可以回復我~
#1

慕勒0695240 提問者

這是什么問題啊
2016-10-29 回復 有任何疑惑可以回復我~
#2

阿旭_

內存不夠了,可能的原因有兩個,一個是圖片太大。另一種可能是代碼可能有地方寫成了死循環。 建議把圖片改成333*333像素的試一下。
2016-10-29 回復 有任何疑惑可以回復我~
#3

慕勒0695240 提問者 回復 阿旭_

對!是圖片太大了
2016-10-29 回復 有任何疑惑可以回復我~

http://img1.sycdn.imooc.com//58144eef0001531c03870715.jpg點擊下一關后會出現這個

0 回復 有任何疑惑可以回復我~
#1

阿旭_

發一下控制臺的錯誤日志,看能不能定位崩潰的代碼是哪一行。
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;

? ? ? ? }

? ? }

}



0 回復 有任何疑惑可以回復我~

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();

? ? ? ? }

? ? }

??

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

如果要設計成關卡,進入下一關,只改變圖片,要怎么設計

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號