首页 > 代码库 > pushbox(2)

pushbox(2)

利用二维数组不同数字表示不同含义,然后在根据数字画出该位置的图像。

MapList.java(工具包,能够得到总共有多少关,得到各关的二维数组)

package edu.phshbox;

public class MapList {
    /**
     * 用不同的数字表示不同的含义,用三维数组表示各个关卡,
     * 其中每个二维数组表示一关
     * 0 nothing 
     * 1 wall 
     * 2 goal 
     * 3 road 
     * 4 box 
     * 5 box at goal 
     * 6 worker
     * */
    public static int[][][] map =
        {
            { 
                    { 0, 0, 1, 1, 1, 0, 0, 0 },
                    { 0, 0, 1, 2, 1, 0, 0, 0 },
                    { 0, 0, 1, 3, 1, 1, 1, 1 },
                    { 1, 1, 1, 4, 3, 4, 2, 1 },
                    { 1, 2, 3, 4, 6, 1, 1, 1 },
                    { 1, 1, 1, 1, 4, 1, 0, 0 },
                    { 0, 0, 0, 1, 2, 1, 0, 0 }, 
                    { 0, 0, 0, 1, 1, 1, 0, 0 } 
            },
            {     
                    { 1, 1, 1, 1, 1, 0, 0, 0, 0 },
                    { 1, 6, 3, 3, 1, 0, 0, 0, 0 },
                    { 1, 3, 4, 4, 1, 0, 1, 1, 1 },
                    { 1, 3, 4, 3, 1, 0, 1, 2, 1 },
                    { 1, 1, 1, 3, 1, 1, 1, 2, 1 },
                    { 0, 1, 1, 3, 3, 3, 3, 2, 1 },
                    { 0, 1, 3, 3, 3, 1, 3, 3, 1 },
                    { 0, 1, 3, 3, 3, 1, 1, 1, 1 },
                    { 0, 1, 1, 1, 1, 1, 0, 0, 0 } 
            }
        };
    //总共有多少关
    public static int count = map.length;

    public static int getCount() {
        return count;
    }
    //得到第几关的图
    public static int[][] getMap(int grade) {
        if(grade<0&&grade>=count)
            grade=0;
        int row=map[grade].length;
        int col=map[grade][0].length;
        
        int[][] result=new int[row][col];
        for(int i=0;i<row;i++){
            System.arraycopy(map[grade], 0, result, 0,col);
        }
        return result;
    }
}

GameMain.java

package edu.phshbox;

import android.os.Bundle;
import android.app.Activity;

public class GameMain extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_main);
    }
}

GameView.java

package edu.phshbox;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

public class GameView extends View {
    int width = 0;
    int oxOff=15;
    int oyOff=30;
    GameMain gameMain = null;
    
    public final int WALL=1;
    public final int GOAL=2;
    public final int ROAD=3;
    public final int BOX=4;
    public final int BOX_AT_GOAL=5;
    public final int WORKER=6;
    
    public static int picSize=30;
    private int map[][]=null;
    private int row=0;
    private int col=0;
    private static int grade=0;
    private Bitmap picture[]=null;
    
    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gameMain = (GameMain) context;
        WindowManager win = gameMain.getWindowManager();
        width = win.getDefaultDisplay().getWidth();
        initMap();
        
        reset();
        initPicture();
    }
    public void reset(){
        row=map.length;
        col=map[0].length;
        picSize=(int)Math.floor((width-oxOff-oyOff)/col);
        
        Log.d("width",width+"");
        Log.d("pageSize",picSize+"");
    }
    //初始化地图
    private void initMap() {
        map=MapList.getMap(grade);
        //resetRowCol();
    }
    private void initPicture() {
        //resetPicSize();
        picture=new Bitmap[7];
        loadPicture(WALL,R.drawable.wall);
        loadPicture(GOAL,R.drawable.goal);
        loadPicture(ROAD,R.drawable.road);
        loadPicture(BOX,R.drawable.box);
        loadPicture(BOX_AT_GOAL,R.drawable.box_at_goal);
        loadPicture(WORKER,R.drawable.worker);
    }
    //加载图片
    private void loadPicture(int key, int pictureId) {
        Drawable drawable=getResources().getDrawable(pictureId);
        Bitmap bitMap=Bitmap.createBitmap(picSize,picSize, Bitmap.Config.ARGB_8888);
        
        drawable.setBounds(0, 0, picSize, picSize);
        Canvas canvas=new Canvas(bitMap);
        drawable.draw(canvas);
        picture[key]=bitMap;
    }
    //根据所在位置的数字不同,画不同的图片到相应的位置上
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint=new Paint();
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(map[i][j]>0)
                canvas.drawBitmap(picture[map[i][j]], oxOff+picSize*j, oyOff+picSize*i, paint);
            }
        }        
        super.onDraw(canvas);
    }
}

运行结果: