博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pushbox(2)
阅读量:5061 次
发布时间:2019-06-12

本文共 4405 字,大约阅读时间需要 14 分钟。

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

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

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
0) canvas.drawBitmap(picture[map[i][j]], oxOff+picSize*j, oyOff+picSize*i, paint); } } super.onDraw(canvas); }}

运行结果:

转载于:https://www.cnblogs.com/jianfengyun/p/3729444.html

你可能感兴趣的文章
PHP上传RAR压缩包并解压目录
查看>>
codeforces global round 1题解搬运
查看>>
python os模块
查看>>
Codeforces 719B Anatoly and Cockroaches
查看>>
jenkins常用插件汇总
查看>>
c# 泛型+反射
查看>>
第九章 前后查找
查看>>
Python学习资料
查看>>
多服务器操作利器 - Polysh
查看>>
[LeetCode] Candy
查看>>
Jmeter学习系列----3 配置元件之计数器
查看>>
jQuery 自定义函数
查看>>
jq 杂
查看>>
jquery datagrid 后台获取datatable处理成正确的json字符串
查看>>
作业一
查看>>
AJAX
查看>>
ActiveMQ与spring整合
查看>>
web服务器
查看>>
Leetcode(7)-反转整数
查看>>
堆栈的分配效率问题
查看>>