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

📄 tiledlayer.java

📁 一款直版动作过关游戏
💻 JAVA
字号:
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;


public class tiledLayer extends layer{
    //列数和行数
    int columns,rows;
    //图
    Image sourceImage;
    //每个tile宽高
    int tileWidth,tileHeight;
    //每个tile的位置
    int[] tileCoordsX,tileCoordsY;
    //tile的个数
    int tilesCount;
    //tile每行和每列有几个
    int tileWide,tileHigh;
    //地图数组
    int[][] tileMaps;
    //定义视点
    int xView,yView;
    public tiledLayer(int columns, int rows, Image image, int tileWidth, int tileHeight){
        super(columns * tileWidth , rows * tileHeight);
        if(image.getWidth() % tileWidth != 0 || image.getHeight() % tileHeight != 0){
            throw new IllegalArgumentException();
        } else{
            //创建tiles;
            createStaticTileSet(columns,rows,image,tileWidth,tileHeight);
        }
    }
    
    public void createStaticTileSet(int columns, int rows, Image image, int tileWidth, int tileHeight){
        this.columns = columns;
        this.rows = rows;
        this.sourceImage = image;
        this.tileHeight = tileHeight;
        this.tileWidth = tileWidth;
        tileWide = sourceImage.getWidth()/tileWidth;
        tileHigh = sourceImage.getHeight()/tileHeight;
        tilesCount = tileWide * tileHigh;
        tileMaps = new int[columns][rows];
        tileCoordsX = new int[tilesCount+1];
        tileCoordsY = new int[tilesCount+1];
        for(int i=0;i<tileHigh;i++){
            for(int j=0;j<tileWide;j++){
                int tempint = i*tileWide+j;
                tileCoordsX[tempint+1] = j*tileWidth;
                tileCoordsY[tempint+1] = i*tileHeight;
            }
        }
        tileCoordsX[0] = -tileWidth;
        tileCoordsY[0] = -tileHeight;
    }
    
    
    public void setCell(int col, int row, int tileIndex){
        if(col < 0 || col >= columns || row < 0 || row >= rows)
            throw new IndexOutOfBoundsException();
        if(tileIndex >= 0){
            tileMaps[col][row] = tileIndex;
        }
        //System.out.println(col+","+row);
    }
    
    public int getCell(int col, int row){
        if(col < 0 || col >= columns || row < 0 || row >= rows)
            throw new IndexOutOfBoundsException();
        else
            return tileMaps[col][row];
    }
    
    public final int getCellWidth()
    {
        return tileWidth;
    }

    public final int getCellHeight()
    {
        return tileHeight;
    }

    public final int getColumns()
    {
        return columns;
    }

    public final int getRows()
    {
        return rows;
    }
    
    
    public void paint(Graphics g){
        if(g == null)
            throw new NullPointerException();
        if(visible){
            //先算出要画几块,不要少画,所以用进一法;
            int endCol = Main.ScrWidth/tileWidth + 1;
            if(Main.ScrWidth%tileWidth!=0){
                endCol = endCol + 1;
            }
            int endRow = Main.ScrHeight/tileHeight + 1;
            if(Main.ScrHeight%tileHeight!=0){
                endRow = endRow + 1;
            }
            g.translate(-x,-y);
            //先算出一部分经常要运算的放循环外计算
            int tempWidth = x/tileWidth;
            int tempHeight = y/tileHeight;
            for(int i=0;i<endRow;i++){
                for(int j=0;j<endCol;j++){
                    //图块限制,不能小于0
                    if(j+tempWidth<0 || i+tempHeight<0 
                            || j+tempWidth>=columns || i+tempHeight>=rows){
                        continue;
                    }
                    //取出地图索引所对应的图块的坐标
                    int tempx = tileCoordsX[tileMaps[j+tempWidth][i+tempHeight]];
                    int tempy = tileCoordsY[tileMaps[j+tempWidth][i+tempHeight]];
                    //开始画显示区域,开区坐标为(列*宽,行*高),闭区为(列+1)*宽,(行+1)*高
                    g.setClip((j+tempWidth)*tileWidth ,(i+tempHeight)*tileHeight ,tileWidth,tileHeight);
                    //开始画图;
                    g.drawImage(sourceImage, (j+tempWidth)*tileWidth - tempx,
                       (i+tempHeight)*tileHeight - tempy,Graphics.TOP|Graphics.LEFT);
                    g.setClip(0,0,Main.ScrWidth, Main.ScrHeight);
                }
            }
            g.translate(x,y);
        }
    }
    
}

⌨️ 快捷键说明

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