android自定义view实现推箱子小游戏

android自定义view实现推箱子小游戏,第1张

概述本文实例为大家分享了android推箱子游戏的具体实现代码,供大家参考,具体内容如下

本文实例为大家分享了androID推箱子游戏的具体实现代码,供大家参考,具体内容如下

自定义view:

package com.jisai.materialdesigndemo.tuixiangzhi;  import androID.content.Context; import androID.graphics.Bitmap; import androID.graphics.BitmapFactory; import androID.graphics.Canvas; import androID.graphics.color; import androID.graphics.Paint; import androID.graphics.Rect; import androID.util.AttributeSet; import androID.util.Log; import androID.Widget.ImageVIEw;  import com.jisai.materialdesigndemo.R;  import java.util.ArrayList;  public class VIEw_tuixiangzhi extends ImageVIEw {   ArrayList<int[][]> arrayList = new ArrayList<>();   int[][][] ccc =new int[300][15][15];   int kk=0;   Canvas canvas;   private int mWIDth;   private int mHeight;   Boolean flag=false;   Bitmap bitmap0= BitmapFactory.decodeResource(getResources(),R.mipmap.qiang);   Bitmap bitmap1= BitmapFactory.decodeResource(getResources(),R.mipmap.kong);   Bitmap bitmap2= BitmapFactory.decodeResource(getResources(),R.mipmap.ren_1);   Bitmap bitmap3= BitmapFactory.decodeResource(getResources(),R.mipmap.xiang_1);   Bitmap bitmap4= BitmapFactory.decodeResource(getResources(),R.mipmap.xiang_2);   Bitmap bitmap5= BitmapFactory.decodeResource(getResources(),R.mipmap.hua);   Bitmap bitmap6= BitmapFactory.decodeResource(getResources(),R.mipmap.renandhua);   int [][]aaa= {//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花       {0,0},{0,5,1,3,2,0}   };    public interface Passlitener   {     voID pass();   }   private Passlitener mPasslitener;   public voID setPasslitener(Passlitener mPasslitener)   {     this.mPasslitener = mPasslitener;   }    public VIEw_tuixiangzhi(Context context) {     super(context);     init("MyImageVIEw(Context context)");   }    public VIEw_tuixiangzhi(Context context,AttributeSet attrs) {     super(context,attrs);     init("MyImageVIEw(Context context,AttributeSet attrs)");   }    public VIEw_tuixiangzhi(Context context,AttributeSet attrs,int defStyle) {     super(context,attrs,defStyle);     init("MyImageVIEw(Context context,int defStyle)");    }    private voID init(String structname) {   }    @OverrIDe   protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {     int specsize = MeasureSpec.getSize(wIDthMeasureSpec);      mWIDth = specsize;     specsize = MeasureSpec.getSize(heightmeasureSpec);      mHeight = specsize;      Log.e("宽高:","宽:"+mWIDth+"高:"+mHeight);      setMeasuredDimension(mWIDth,mHeight);    }    @OverrIDe   protected voID onDraw(Canvas canvas) {     Log.e("onDraw:","onDraw");     this.canvas=canvas;     super.onDraw(canvas);     Paint p=new Paint();     p.setcolor(color.RED);     //canvas.drawRect(20,20,100,p);     //canvas.drawcolor(color.RED);     for(int i=0;i<15;i++){       for(int k=0;k<15;k++){         int x=aaa[i][k];          Rect rect = new Rect((mWIDth/15)*k,(mWIDth/15)*i,(mWIDth/15)*(k+1),(mWIDth/15)*(i+1));         switch (x){           case 0:             canvas.drawBitmap(bitmap0,null,rect,p);             break;           case 1:             canvas.drawBitmap(bitmap1,p);             break;           case 2:             canvas.drawBitmap(bitmap2,p);             break;           case 3:             canvas.drawBitmap(bitmap3,p);             break;           case 4:             canvas.drawBitmap(bitmap4,p);             break;           case 5:             canvas.drawBitmap(bitmap5,p);             break;           case 6:             canvas.drawBitmap(bitmap6,p);             break;          }       }     }      }    public voID setaaa(int [][]xxx){     for(int i=0;i<15;i++){       for(int k=0;k<15;k++) {         aaa[i][k]=xxx[i][k];       }     }     kk=0;     arrayList.clear();     postInvalIDate();   }   public voID moveleft(){     addBack();     flag=false;     for(int i=0;i<15;i++){       if (flag){         break;       }       for(int k=0;k<15;k++) {         if (flag){           break;         }         int x = aaa[i][k];         if(x==2|x==6){           Log.e("x",""+x+"i:"+i+"k:"+k);            switch ( aaa[i][k-1]){             case 0://左边是墙,不能移动               break;             case 1://左边是空,可以移动               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i][k-1]=2;                break;             case 3://左边是空箱子               if(aaa[i][k-2]==5){//箱子左边是花                 aaa[i][k-2]=4;                 aaa[i][k-1]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }                }else if(aaa[i][k-2]==1){//箱子左边是空                 aaa[i][k-2]=3;                 aaa[i][k-1]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }                }else{}//是其他则不能移动                break;             case 4://左边是满箱子               if(aaa[i][k-2]==5){//箱子左边是花                 aaa[i][k-2]=4;                 aaa[i][k-1]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }                }else if(aaa[i][k-2]==1){//箱子左边是空                 aaa[i][k-2]=3;                 aaa[i][k-1]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }                }else{}//是其他则不能移动               break;             case 5://左边是花               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i][k-1]=6;                break;           }           flag=true;         }       }     }       postInvalIDate();     ifPass();   }   public voID moveRight(){     addBack();     flag=false;     for(int i=0;i<15;i++){       if (flag){         break;       }       for(int k=0;k<15;k++) {         int x = aaa[i][k];         if (flag){           break;         }         if(x==2|x==6){           Log.e("x",""+x+"i:"+i+"k:"+k);           switch ( aaa[i][k+1]){             case 0://右边是墙,不能移动               break;             case 1://右边是空,可以移动               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i][k+1]=2;               break;             case 3://右边是空箱子               if(aaa[i][k+2]==5){//箱子右边是花                 aaa[i][k+2]=4;                 aaa[i][k+1]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else if(aaa[i][k+2]==1){//箱子右边是空                 aaa[i][k+2]=3;                 aaa[i][k+1]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else{}//是其他则不能移动               break;             case 4://右边是满箱子               if(aaa[i][k+2]==5){//箱子右边是花                 aaa[i][k+2]=4;                 aaa[i][k+1]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else if(aaa[i][k+2]==1){//箱子右边是空                 aaa[i][k+2]=3;                 aaa[i][k+1]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else{}//是其他则不能移动               break;             case 5://右边是花               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i][k+1]=6;               break;           }           flag=true;         }       }     }      postInvalIDate();     ifPass();   }   public voID moveUp(){     addBack();     flag=false;     for(int i=0;i<15;i++){       if (flag){         break;       }       for(int k=0;k<15;k++) {         int x = aaa[i][k];         if (flag){           break;         }         if(x==2|x==6){           Log.e("x",""+x+"i:"+i+"k:"+k);           switch ( aaa[i-1][k]){             case 0://上边是墙,不能移动               break;             case 1://上边是空,可以移动               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i-1][k]=2;               break;             case 3://上边是空箱子               if(aaa[i-2][k]==5){//箱子上边是花                 aaa[i-2][k]=4;                 aaa[i-1][k]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else if(aaa[i-2][k]==1){//箱子上边是空                 aaa[i-2][k]=3;                 aaa[i-1][k]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else{}//是其他则不能移动               break;             case 4://上边是满箱子               if(aaa[i-2][k]==5){//箱子上边是花                 aaa[i-2][k]=4;                 aaa[i-1][k]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else if(aaa[i-2][k]==1){//箱子上边是空                 aaa[i-2][k]=3;                 aaa[i-1][k]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else{}//是其他则不能移动               break;             case 5://上边是花               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i-1][k]=6;               break;           }           flag=true;         }       }     }      postInvalIDate();     ifPass();   }   public voID moveDown(){     addBack();     flag=false;     for(int i=0;i<15;i++){       if (flag){         break;       }       for(int k=0;k<15;k++) {         int x = aaa[i][k];         if (flag){           break;         }         if(x==2|x==6){           Log.e("x",""+x+"i:"+i+"k:"+k);           switch ( aaa[i+1][k]){             case 0://下边是墙,不能移动               break;             case 1://下边是空,可以移动               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i+1][k]=2;               break;             case 3://下边是空箱子               if(aaa[i+2][k]==5){//箱子下边是花                 aaa[i+2][k]=4;                 aaa[i+1][k]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else if(aaa[i+2][k]==1){//箱子下边是空                 aaa[i+2][k]=3;                 aaa[i+1][k]=2;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else{}//是其他则不能移动               break;             case 4://下边是满箱子               if(aaa[i+2][k]==5){//箱子下边是花                 aaa[i+2][k]=4;                 aaa[i+1][k]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else if(aaa[i+2][k]==1){//箱子下边是空                 aaa[i+2][k]=3;                 aaa[i+1][k]=6;                 if(x==6){                   aaa[i][k]=5;                 }else{                   aaa[i][k]=1;                 }               }else{}//是其他则不能移动               break;             case 5://下边是花               if(x==6){                 aaa[i][k]=5;               }else{                 aaa[i][k]=1;               }               aaa[i+1][k]=6;               break;           }           flag=true;         }       }     }     postInvalIDate();     ifPass();   }    public voID ifPass(){     flag=false;     for(int i=0;i<15;i++){       if (flag){         break;       }       for(int k=0;k<15;k++) {         int x = aaa[i][k];         if (flag){           break;         }         if(x==3){//还有空箱子           flag=true;         }       }     }      if (!flag){//没有一个空箱子       mPasslitener.pass();     }   }    public voID moveBack(){     if(arrayList.size()>0){       this.aaa=(arrayList.get(arrayList.size()-1));       postInvalIDate();       arrayList.remove(arrayList.size()-1);     }   }   public voID addBack(){     if(kk>299){       arrayList.clear();       kk=0;     }     if(arrayList.size()>99){       arrayList.remove(0);     }     for(int i=0;i<15;i++){       for(int k=0;k<15;k++){         ccc[kk][i][k]=aaa[i][k];       }     }     arrayList.add(ccc[kk]);     kk++;   }  } 

activity  :

package com.jisai.materialdesigndemo.tuixiangzhi;  import androID.content.DialogInterface; import androID.graphics.color; import androID.os.Bundle; import androID.support.design.Widget.Snackbar; import androID.support.v7.app.AlertDialog; import androID.support.v7.app.AppCompatActivity; import androID.support.v7.Widget.Toolbar; import androID.vIEw.Menu; import androID.vIEw.MenuItem; import androID.vIEw.VIEw; import androID.Widget.button;  import com.jisai.materialdesigndemo.R;  import java.util.ArrayList;  public class MainActivity_tuixiangzhi extends AppCompatActivity     {   ArrayList<int[][]> arrayList=new ArrayList<>();       button up,down,left,right,back,nextup,nextdown;       VIEw_tuixiangzhi myImageVIEw_circle;       Toolbar mToolbar;       int   LV=1;   @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_tuixiangzhi);     LV_data lv_data=new LV_data();     arrayList=lv_data.arrayList;     mToolbar = (Toolbar) findVIEwByID(R.ID.toolbar);     // mToolbar.setBackgroundcolor(0xFF42B1CD);     mToolbar.setTitle("推箱子(第"+LV+"关)");    //设置标题     mToolbar.setTitleTextcolor(color.parsecolor("#ffffff"));  //设置标题颜色     setSupportActionbar(mToolbar);     getSupportActionbar().setdisplayHomeAsUpEnabled(true);     mToolbar.setNavigationOnClickListener(new VIEw.OnClickListener() {       @OverrIDe       public voID onClick(VIEw v) {         // onBackpressed();         finish();         //Toast.makeText(getApplicationContext(),"返回",Toast.LENGTH_SHORT).show();        }     });           myImageVIEw_circle= (VIEw_tuixiangzhi) findVIEwByID(R.ID.nnn);      myImageVIEw_circle.setPasslitener(new VIEw_tuixiangzhi.Passlitener() {       @OverrIDe       public voID pass() {          final AlertDialog.Builder normalDialog =             new AlertDialog.Builder(MainActivity_tuixiangzhi.this);         normalDialog.setTitle("恭喜你");         normalDialog.setMessage("真厉害,你已通过本关!");         normalDialog.setPositivebutton("下一关",new DialogInterface.OnClickListener() {               @OverrIDe               public voID onClick(DialogInterface dialog,int which) {                 if(LV<arrayList.size()){                   LV=LV+1;                   myImageVIEw_circle.setaaa(arrayList.get(LV-1));                   mToolbar.setTitle("推箱子(第"+LV+"关)");    //设置标题                 }else{                   final Snackbar snackbar = Snackbar.make(up,"已是最后一关",Snackbar                       .LENGTH_LONG);                   snackbar.show();                   snackbar.setAction("确定",new VIEw.OnClickListener() {                     @OverrIDe                     public voID onClick(VIEw vIEw) {                       snackbar.dismiss();                     }                   });                 }                }             });         // 显示         normalDialog.show();       }     });      up= (button) findVIEwByID(R.ID.shang);     down= (button) findVIEwByID(R.ID.xia);     left= (button) findVIEwByID(R.ID.zuo);     right= (button) findVIEwByID(R.ID.you);     back= (button) findVIEwByID(R.ID.back);     nextup= (button) findVIEwByID(R.ID.shangyi);     nextdown= (button) findVIEwByID(R.ID.xiayi);      up.setonClickListener(cccc);     down.setonClickListener(cccc);     left.setonClickListener(cccc);     right.setonClickListener(cccc);     back.setonClickListener(cccc);     nextup.setonClickListener(cccc);     nextdown.setonClickListener(cccc);     }       VIEw.OnClickListener cccc = new VIEw.OnClickListener() {         @OverrIDe         public voID onClick(VIEw v) {           switch (v.getID()){             case R.ID.shang:               myImageVIEw_circle.moveUp();               break;             case R.ID.xia:               myImageVIEw_circle.moveDown();               break;             case R.ID.zuo:               myImageVIEw_circle.moveleft();               break;             case R.ID.you:               myImageVIEw_circle.moveRight();               break;             case R.ID.back:               myImageVIEw_circle.moveBack();               break;             case R.ID.shangyi:               if(LV>1){                 LV=LV-1;                 myImageVIEw_circle.setaaa(arrayList.get(LV-1));                 mToolbar.setTitle("推箱子(第"+LV+"关)");    //设置标题               }               break;             case R.ID.xiayi:               if(LV<arrayList.size()){                 LV=LV+1;                 myImageVIEw_circle.setaaa(arrayList.get(LV-1));                 mToolbar.setTitle("推箱子(第"+LV+"关)");    //设置标题               }else{                 final Snackbar snackbar = Snackbar.make(up,Snackbar                     .LENGTH_LONG);                 snackbar.show();                 snackbar.setAction("确定",new VIEw.OnClickListener() {                   @OverrIDe                   public voID onClick(VIEw vIEw) {                     snackbar.dismiss();                   }                 });               }               break;           }          }       };        @OverrIDe       public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.         getMenuInflater().inflate(R.menu.menu_tuixiangzhi,menu);         return true;       }        @OverrIDe       public boolean onoptionsItemSelected(MenuItem item) {         // Handle action bar item clicks here. The action bar will         // automatically handle clicks on the Home/Up button,so long         // as you specify a parent activity in AndroIDManifest.xml.         int ID = item.getItemID();          //noinspection SimplifiableIfStatement         if (ID == R.ID.item1) {           myImageVIEw_circle.setaaa(arrayList.get(LV-1));            return true;         }          return super.onoptionsItemSelected(item);       }   } 

布局文件:

<?xml version="1.0" enCoding="utf-8"?> <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"   xmlns:app="http://schemas.androID.com/apk/res-auto"   tools:context="com.jisai.materialdesigndemo.caipu.CaipuItemActivity">      <androID.support.v7.Widget.Toolbar       androID:ID="@+ID/toolbar"       androID:layout_wIDth="match_parent"       androID:layout_height="?attr/actionbarSize"       app:layout_collapseMode="pin"       androID:elevation="6dp"       androID:background="?attr/colorPrimary"       app:theme="@style/themeOverlay.AppCompat.Dark.Actionbar"       app:popuptheme="@style/themeOverlay.AppCompat.light"       app:layout_scrollFlags="scroll|enteralways"       />    <relativeLayout     androID:layout_wIDth="match_parent"     androID:layout_height="match_parent"     androID:background="@mipmap/bg3"     >     <com.jisai.materialdesigndemo.tuixiangzhi.VIEw_tuixiangzhi       androID:ID="@+ID/nnn"       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content" />       <relativeLayout       androID:layout_alignParentBottom="true"       androID:layout_wIDth="match_parent"       androID:layout_height="150dp"       >       <button         androID:ID="@+ID/back"         androID:layout_wIDth="40dp"         androID:layout_centerInParent="true"         androID:layout_height="40dp"         androID:background="@drawable/back2"         androID:layout_margin="15dp"/>        <button         androID:ID="@+ID/shang"         androID:layout_wIDth="40dp"         androID:layout_height="40dp"         androID:background="@drawable/s_s"         androID:layout_above="@+ID/back"         androID:layout_alignleft="@+ID/back"/>       <button         androID:ID="@+ID/xia"         androID:layout_wIDth="40dp"         androID:layout_height="40dp"         androID:background="@drawable/d_s"         androID:layout_below="@+ID/back"         androID:layout_alignleft="@+ID/back"/>       <button         androID:ID="@+ID/zuo"         androID:layout_wIDth="40dp"         androID:layout_height="40dp"         androID:background="@drawable/l_s"         androID:layout_toleftOf="@+ID/back"         androID:layout_aligntop="@+ID/back"/>       <button         androID:ID="@+ID/you"         androID:layout_wIDth="40dp"         androID:layout_height="40dp"         androID:background="@drawable/r_s"         androID:layout_toRightOf="@+ID/back"         androID:layout_aligntop="@+ID/back"/>        <button         androID:ID="@+ID/shangyi"         androID:layout_wIDth="40dp"         androID:layout_height="40dp"         androID:background="@drawable/l_n"         androID:layout_marginBottom="40dp"         androID:layout_marginleft="15dp"         androID:layout_alignParentBottom="true"         androID:layout_alignParentleft="true"         />       <TextVIEw         androID:layout_wIDth="wrap_content"         androID:layout_height="30dp"         androID:layout_alignParentBottom="true"         androID:layout_alignParentleft="true"         androID:layout_marginleft="20dp"         androID:text="上一关"/>        <button         androID:ID="@+ID/xiayi"         androID:layout_wIDth="40dp"         androID:layout_height="40dp"         androID:background="@drawable/r_n"         androID:layout_marginBottom="40dp"         androID:layout_marginRight="15dp"         androID:layout_alignParentBottom="true"         androID:layout_alignParentRight="true"         />       <TextVIEw         androID:layout_wIDth="wrap_content"         androID:layout_height="30dp"         androID:layout_marginRight="20dp"         androID:layout_alignParentBottom="true"         androID:layout_alignParentRight="true"         androID:text="下一关"/>      </relativeLayout>     </relativeLayout>  </linearLayout> 

关卡数据(二位数组),我加了40关,太长这里就贴一点。

package com.jisai.materialdesigndemo.tuixiangzhi;  import java.util.ArrayList;  /**  * Created by administrator on 17-4-11.  */ public class LV_data {  final  ArrayList<int[][]>arrayList = new ArrayList<>();    LV_data(){     int[][] LV_1 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花         {0,0}     };     arrayList.add(LV_1);       int[][] LV_2 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,0}     };     arrayList.add(LV_2);      int[][] LV_3 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,0}     };     arrayList.add(LV_3);      int[][] LV_4 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,4,0}     };     arrayList.add(LV_4);      int[][] LV_5 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,0}     };     arrayList.add(LV_5);      int[][] LV_6 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,0}     };     arrayList.add(LV_6);      int[][] LV_7 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,0}     };     arrayList.add(LV_7);      int[][] LV_8 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,0}     };     arrayList.add(LV_8); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的android自定义view实现推箱子小游戏全部内容,希望文章能够帮你解决android自定义view实现推箱子小游戏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146439.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存