Android编写2048小游戏
先来说说2048游戏规则:
开始时棋盘内随机出现两个数字,出现的数字仅可能为2或4
玩家可以选择上下左右四个方向,若棋盘内的数字出现位移或合并,视为有效移动
玩家选择的方向上若有相同的数字则合并,每次有效移动可以同时合并,但不可以连续合并
合并所得的所有新生成数字想加即为该步的有效得分
玩家选择的方向行或列前方有空格则出现位移
每有效移动一步,棋盘的空位(无数字处)随机出现一个数字(依然可能为2或4)
棋盘被数字填满,无法进行有效移动,判负,游戏结束
棋盘上出现2048,判胜,游戏结束
下面代码
MainActivity类
publicclassMainActivityextendsAppCompatActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(newGameView(this)); } }
定义一个卡片类显示数据
/卡片类型 publicclassCardextendsFrameLayout{ //2 TextViewtv; privateintnumber=0; intwidth; publicCard(Contextcontext,intwidth){ super(context); this.width=width; init(); } privatevoidinit(){ tv=newTextView(getContext()); setPadding(5,5,5,5); FrameLayout.LayoutParamslp=newLayoutParams(width-10,width-10); tv.setLayoutParams(lp); tv.setGravity(Gravity.CENTER); tv.setTextColor(Color.WHITE); tv.setTextSize(48); this.addView(tv); setColor(); } publicvoidsetNumber(intnumber){ this.number=number; if(number==0) tv.setText(""); else tv.setText(number+""); setColor(); } @Override publicStringtoString(){ return"Card{"+ "tv="+tv+ ",number="+number+ ",width="+width+ '}'; } privatevoidsetColor(){ switch(number){ case0: tv.setBackgroundColor(getResources().getColor(R.color.c0)); break; case2: tv.setBackgroundColor(getResources().getColor(R.color.c2)); break; case4: tv.setBackgroundColor(getResources().getColor(R.color.c4)); break; case8: tv.setBackgroundColor(getResources().getColor(R.color.c8)); break; case16: tv.setBackgroundColor(getResources().getColor(R.color.c16)); break; case32: tv.setBackgroundColor(getResources().getColor(R.color.c32)); break; case64: tv.setBackgroundColor(getResources().getColor(R.color.c64)); break; case128: tv.setBackgroundColor(getResources().getColor(R.color.c128)); break; case256: tv.setBackgroundColor(getResources().getColor(R.color.c256)); break; case512: tv.setBackgroundColor(getResources().getColor(R.color.c512)); break; case1024: tv.setBackgroundColor(getResources().getColor(R.color.c1024)); break; case2048: tv.setBackgroundColor(getResources().getColor(R.color.c2048)); break; } } publicintgetNumber(){ returnnumber; } }
自己定义一下上面的几种颜色在values下的colors中
<?xmlversion="1.0"encoding="utf-8"?> <resources> <colorname="colorPrimary">#3F51B5</color> <colorname="colorPrimaryDark">#303F9F</color> <colorname="colorAccent">#FF4081</color> <colorname="c0">#43382a</color> <colorname="c2">#2a8618</color> <colorname="c4">#3d6319</color> <colorname="c8">#F2B179</color> <colorname="c16">#F59563</color> <colorname="c32">#F57C5F</color> <colorname="c64">#5b69b7</color> <colorname="c128">#b37d3e</color> <colorname="c256">#3a9096</color> <colorname="c512">#d7882c</color> <colorname="c1024">#5c1b78</color> <colorname="c2048">#640548</color> </resources>
布局类和里面的运算方法
publicclassGameViewextendsGridLayout{ int[][]values=newint[4][4]; Card[][]cards=newCard[4][4]; //Integer-128-127==大于这个数两个对象 publicGameView(Contextcontext){ super(context); init(); } publicGameView(Contextcontext,AttributeSetattrs){ super(context,attrs); init(); } privatevoidinit(){ setColumnCount(4); } @Override protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){ super.onSizeChanged(w,h,oldw,oldh); newGame(); } privatevoidnewGame(){ //ViewGroup.LayoutParamslp=newViewGroup.LayoutParams((int)(getResources().getDisplayMetrics().widthPixels/4),(int)(getResources().getDisplayMetrics().widthPixels/4)); GridLayout.LayoutParamsparams=newGridLayout.LayoutParams(); //params.width=getResources().getDisplayMetrics().widthPixels/4; //params.height=getResources().getDisplayMetrics().widthPixels/4; //Log.e("TAG",params.width+""+params.height); this.removeAllViews(); //GridLayout.LayoutParamslpa=newGridLayout.LayoutParams(lp); for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ values[i][j]=0; cards[i][j]=newCard(getContext(),getResources().getDisplayMetrics().widthPixels/4); //cards[i][j].setLayoutParams(params); //cards[i][j].upLv(); this.addView(cards[i][j]); } } //创建初始的两张卡 inti=(int)(Math.random()*16); intj=0; do{ j=(int)(Math.random()*16);//0-151533 }while(j==i); Log.e("TAG",i+""+j); values[i/4][i%4]=Math.random()*20<1?4:2; values[j/4][j%4]=Math.random()*20<1?4:2; setValues(); } floatoldx,oldy; intmove=-1; @Override publicbooleanonTouchEvent(MotionEventevent){ floatx=event.getX(); floaty=event.getY(); switch(event.getAction()){ caseMotionEvent.ACTION_DOWN: move=-1; oldx=x; oldy=y; break; caseMotionEvent.ACTION_MOVE: if(Math.abs(oldx-x)>Math.abs(oldy-y)){ if(oldx-x>15){//左 Log.e("TAG","---------->>>"); move=1; }elseif(oldx-x<-15){//右 Log.e("TAG","---------->>>"); move=2; } }else{ if(oldy-y>15){ move=3; }elseif(oldy-y<-15){ move=4; } } break; caseMotionEvent.ACTION_UP: //记录之前的数组 int[][]temp=newint[4][4]; for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ temp[i][j]=values[i][j]; } } switch(move){ case1: left(); break; case2: right(); break; case3: up(); break; case4: down(); break; } setValues(); if(move!=-1){ //比对当前的数组 for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ if(values[i][j]!=temp[i][j]){ addCard(); returntrue; } } } } //判断游戏胜利还是结束4 if(isWin()){ Toast.makeText(getContext(),"游戏胜利",Toast.LENGTH_SHORT).show(); } if(isOver()){ this.removeAllViews(); TextViewtv=newTextView(getContext()); tv.setText("游戏结束,点击从新开始"); this.addView(tv); tv.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ newGame(); } }); } break; } returntrue; } privatevoidaddCard(){ while(true){ intj=(int)(Math.random()*16); if(values[j/4][j%4]==0){ values[j/4][j%4]=2; cards[j/4][j%4].setNumber(2); return; } } } publicvoidleft(){ //取出4个集合 for(inti=0;i<4;i++){ List<Integer>list=newArrayList<>(); for(intj=0;j<4;j++){ intvalue=values[i][j]; if(value!=0) list.add(value); } //比较 Log.e("TAG",list.toString()); if(list.size()==0) continue; elseif(list.size()==1){ values[i][0]=list.get(0); for(intj=0;j<3;j++){ values[i][j+1]=0; } }elseif(list.size()==2){ if(list.get(0).equals(list.get(1))){ values[i][0]=list.get(0)*2; //三个值补0 for(intj=0;j<3;j++){ values[i][j+1]=0; } }else{ values[i][0]=list.get(0); values[i][1]=list.get(1); values[i][2]=0; values[i][3]=0; } }elseif(list.size()==3){ if(list.get(0).equals(list.get(1))){ values[i][0]=list.get(0)*2; values[i][1]=list.get(2); values[i][2]=0; values[i][3]=0; }elseif(list.get(1).equals(list.get(2))){ values[i][0]=list.get(0); values[i][1]=list.get(2)*2; values[i][2]=0; values[i][3]=0; }else{ values[i][0]=list.get(0); values[i][1]=list.get(1); values[i][2]=list.get(2); values[i][3]=0; } }else{ if(list.get(0).equals(list.get(1))){ if(list.get(3).equals(list.get(2))){ values[i][0]=list.get(0)*2; values[i][1]=list.get(2)*2; values[i][2]=0; values[i][3]=0; }else{ values[i][0]=list.get(0)*2; values[i][1]=list.get(2); values[i][2]=list.get(3); values[i][3]=0; } }else{ //1和2不相等 //先比对23相等,不等 if(list.get(1).equals(list.get(2))){ values[i][0]=list.get(0); values[i][1]=list.get(1)*2; values[i][2]=list.get(3); values[i][3]=0; }else{ if(list.get(2).equals(list.get(3))){ values[i][0]=list.get(0); values[i][1]=list.get(1); values[i][2]=list.get(2)*2; values[i][3]=0; } } } } } } privatevoiddelete(){ //Log.e("TAG","--------------------执行"); ////遍历 //for(inti=0;i<4;i++){ //for(intj=0;j<3;j++){ //Cardcard=cards[i][j]; //Log.e("TAG","i:"+i+"j:"+j+"num:"+card.getNumber()); //if(card.getNumber()==0){ //booleanisSub=false; //for(intk=j;k<3;k++){ //cards[i][k].setNumber(cards[i][k+1].getNumber()); //if(cards[i][k+1].getNumber()!=0){ //isSub=true; //} //} //if(isSub) //j--; //cards[i][3].setNumber(0); //}elseif(card.getNumber()==cards[i][j+1].getNumber()){ //card.upLv(); //cards[i][j+1].setNumber(0); ////后面的往前搬 //for(intk=j+1;k<3;k++){ //cards[i][k].setNumber(cards[i][k+1].getNumber()); //} //cards[i][3].setNumber(0); //j--; //} //} //} //for(intj=0;j<4;j++){//列 //for(inti=3;i>=1;i--){ //Cardcard=cards[j][i]; //if(card.getNumber()==0){ ////全行左移 ////要将 ////如果是最后一个,不需要理会 //continue; //}else{ ////判断左边一个 //if(cards[j][i-1].getNumber()==0){ ////从i--》i-1 //for(intk=i-1;k<3;k++){ //cards[j][k].setNumber(cards[j][k+1].getNumber()); //} //cards[j][3].setNumber(0); // //}elseif(cards[j][i-1].getNumber()==card.getNumber()){ //cards[j][i-1].upLv(); //card.setNumber(0); //for(intk=i;k<3;k++){ //cards[j][k].setNumber(cards[j][k+1].getNumber()); //} //cards[j][3].setNumber(0); //} //} //} //} } publicvoidright(){ mirrorH(); left(); mirrorH(); } privatevoidmirrorH(){ for(inti=0;i<4;i++){ inttemp=values[i][0]; values[i][0]=values[i][3]; values[i][3]=temp; temp=values[i][1]; values[i][1]=values[i][2]; values[i][2]=temp; } } publicvoiddown(){ //左旋 int[][]temp=newint[4][4]; for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ temp[i][j]=values[3-j][i]; } } values=temp; left(); temp=newint[4][4]; for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ temp[i][j]=values[j][3-i]; } } values=temp; } publicvoidup(){ mirrorV(); down(); mirrorV(); } privatevoidmirrorV(){ for(intj=0;j<4;j++){ inttemp=values[0][j]; values[0][j]=values[3][j]; values[3][j]=temp; temp=values[1][j]; values[1][j]=values[2][j]; values[2][j]=temp; } } publicvoidsetValues(){ for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ System.out.print(values[i][j]+""); cards[i][j].setNumber(values[i][j]); } System.out.println(); } } publicbooleanisWin(){ for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ if(values[i][j]==2048) returntrue; } } returnfalse; } publicbooleanisOver(){ for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ if(values[i][j]==0) returnfalse; } } //满了 for(inti=0;i<4;i++){ for(intj=0;j<4;j++){ intvalue=values[i][j]; if(i>1&&value==values[i-1][j]) returnfalse; elseif(i<3&&value==values[i+1][j]) returnfalse; elseif(j>1&&value==values[i][j-1]) returnfalse; elseif(j<3&&value==values[i][j+1]) returnfalse; } } returntrue; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。