⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plat.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import java.io.*;

/**
 * 该类描述了每关游戏的初始地图,并提供了读取每关游戏数据的方法。
 */
public class Plat {
    public static final char WALL = '#';         //仓库围墙
    public static final char CHANNELS = '+';     //仓库的通道
    public static final char STORE = '*';        //仓库存储位置
    public static final char BOX = '@';          //货箱
    public static final char BOX_IN_STORE = '&'; //存储位置上的货箱
    public static final char PORTAL = 'P';       //搬运工的初始位置
    public static final char NIL = '0';
    
    private int columns = 0;    //地图宽
    private int rows = 0;       //地图高
    private char[] backgroundMap = new char[0];       //
    private int[] boxesLocation;    //箱子的初始位置
    private int boxesTotal = 0;     //箱子总数
    private int workLocation = 0;   //搬运工的初始位置
    
    //读取level关游戏的地图数据,如果level关数据不存在,则读取第一关的数据
    public int readLevel(int level) {
        InputStream is = getClass().getResourceAsStream("/level" + level +".map");
        if(is == null) {    //如果没有指定的资源,则加载第一关的游戏
            is = getClass().getResourceAsStream("/map/level1.map");
            level = 1;
        }
        InputStreamReader isr = new InputStreamReader(is);
        try {
            char[] chs = new char[8];
            isr.read(chs, 0, chs.length);
            columns = Integer.parseInt(new String(chs, 0, 2));  //地图宽
            rows = Integer.parseInt(new String(chs, 2, 2));     //地图高
            boxesTotal = Integer.parseInt(new String(chs, 4, 2));   //地图中箱子数
            backgroundMap = new char[columns*rows];
            boxesLocation = new int[boxesTotal];
            
            int row = 0;
            chs = new char[columns+2];
            int len = -1;
            int blIndex = 0;
            while(row < rows && ((len = isr.read(chs, 0, chs.length)) != -1)) {
                for(int i=0; i<chs.length-2; i++) {
                    if(chs[i] == NIL || chs[i] == WALL || chs[i] == CHANNELS || chs[i] == STORE) {
                        backgroundMap[row*columns+i] = chs[i];
                    }
                    else if(chs[i] == BOX) {
                        boxesLocation[blIndex++] = row*columns+i;
                        backgroundMap[row*columns+i] = CHANNELS;
                    }
                    else if(chs[i] == BOX_IN_STORE) {
                        boxesLocation[blIndex++] = row*columns+i;
                        backgroundMap[row*columns+i] = CHANNELS;
                    }
                    else if(chs[i] == PORTAL) {
                        workLocation = row*columns+i;
                        backgroundMap[row*columns+i] = CHANNELS;
                    }
                }
                row++;
            }
        }
        catch(IOException ioe) {
            System.out.println("err: " + ioe.toString());
        }
        
        return level;
    }
    
    //获取背景地图x,y位置的数据
    public char getValue(int x, int y) {
        if(x<0 || x>=columns ||y<0 || y>=rows) {
            return NIL;
        }
        return backgroundMap[columns*y+x];
    }
    
    //获取地图列数
    public int getColumns() {
        return columns;
    }
    
    //获取地图行数
    public int getRows() {
        return rows;
    }
    
    //获取箱子个数
    public int getBoxesTotal() {
        return boxesTotal;
    }
    
    //获取箱子的初始位置
    public void getBoxesInitLocation(int[] x, int[] y) {
        for(int i=0; i<boxesLocation.length; i++) {
            x[i] = getX(boxesLocation[i]);
            y[i] = getY(boxesLocation[i]);
        }
    }
    
    //获取工人的初始位置
    public void getWorkerInitLocation(int[] position) {
        position[0] = getX(workLocation);
        position[1] = getY(workLocation);        
    }
    
    private int getX(int index) {
        return index%columns;
    }
    
    private int getY(int index) {
        return index/columns;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -