欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-08-02 15:57:08
本文实例为大家分享了android推箱子游戏的具体实现代码,供大家参考,具体内容如下 自定义view: package com.jisai.materi...

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

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

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

自定义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,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,5,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,3,1,3,5,0,0,0,0,0}, 
      {0,0,0,0,5,3,2,1,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,3,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,5,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
      {0,0,0,0,0,0,0,0,0,0,0,0,0,0,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, attributeset attrs, 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,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,null,rect,p); 
            break; 
          case 2: 
            canvas.drawbitmap(bitmap2,null,rect,p); 
            break; 
          case 3: 
            canvas.drawbitmap(bitmap3,null,rect,p); 
            break; 
          case 4: 
            canvas.drawbitmap(bitmap4,null,rect,p); 
            break; 
          case 5: 
            canvas.drawbitmap(bitmap5,null,rect,p); 
            break; 
          case 6: 
            canvas.drawbitmap(bitmap6,null,rect,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,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,5,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,3,1,3,5,0,0,0,0,0}, 
        {0,0,0,0,5,3,2,1,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,3,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,5,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_1); 
 
 
    int[][] lv_2 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,2,1,1,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,1,3,3,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,1,3,1,0,0,0,5,0,0,0,0}, 
        {0,0,0,0,0,0,1,0,0,0,5,0,0,0,0}, 
        {0,0,0,0,0,0,1,1,1,1,5,0,0,0,0}, 
        {0,0,0,0,0,1,1,1,0,1,1,0,0,0,0}, 
        {0,0,0,0,0,1,1,1,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_2); 
 
    int[][] lv_3 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0}, 
        {0,0,0,0,0,3,0,0,0,1,1,1,0,0,0}, 
        {0,0,0,0,1,2,1,3,1,1,3,1,0,0,0}, 
        {0,0,0,0,1,5,5,0,1,3,1,0,0,0,0}, 
        {0,0,0,0,0,5,5,0,1,1,1,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_3); 
 
    int[][] lv_4 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,1,1,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,2,3,1,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,3,1,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,1,3,1,0,0,0,0,0,0}, 
        {0,0,0,0,0,5,3,1,1,0,0,0,0,0,0}, 
        {0,0,0,0,0,5,5,4,5,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_4); 
 
    int[][] lv_5 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,2,1,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,1,3,1,1,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,1,0,1,0,0,0,0,0,0}, 
        {0,0,0,0,5,0,1,0,1,1,0,0,0,0,0}, 
        {0,0,0,0,5,3,1,1,0,1,0,0,0,0,0}, 
        {0,0,0,0,5,1,1,1,3,1,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_5); 
 
    int[][] lv_6 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0}, 
        {0,0,1,1,1,5,0,0,0,1,0,0,0,0,0}, 
        {0,0,1,0,1,0,1,1,1,1,0,0,0,0,0}, 
        {0,0,1,0,1,3,1,3,0,5,1,0,0,0,0}, 
        {0,0,1,0,1,1,4,1,1,0,1,0,0,0,0}, 
        {0,0,1,5,0,3,1,3,1,0,1,0,0,0,0}, 
        {0,0,0,1,1,1,1,0,1,0,1,0,0,0,0}, 
        {0,0,0,1,0,0,0,5,1,1,1,1,2,0,0}, 
        {0,0,0,1,1,1,1,1,0,0,1,1,1,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_6); 
 
    int[][] lv_7 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,1,1,1,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,1,3,1,5,5,1,3,1,0,0,0,0}, 
        {0,0,0,0,3,1,5,5,3,3,2,0,0,0,0}, 
        {0,0,0,0,1,1,5,5,1,3,1,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_7); 
 
    int[][] lv_8 ={//0是墙,1是空,2是人,3是空箱子,4是满箱子,5是花,6是人加花 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,1,1,1,1,0,0,0,0}, 
        {0,0,0,0,0,5,1,3,0,0,1,0,0,0,0}, 
        {0,0,0,0,5,5,3,1,3,1,1,2,0,0,0}, 
        {0,0,0,0,5,5,1,3,1,3,1,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,1,1,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} 
    }; 
    arraylist.add(lv_8); 

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