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

📄 map.java~60~

📁 这个是最早期的一个飞行射击游戏 简单实现了地图滚动 子弹碰撞 与 UI等
💻 JAVA~60~
字号:
package newgame;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.InputStream;

public class Map {
    Canvas1 mc;
    Image img_map; //地图图片
    int tileWidth; //小图块的宽
    int tileHeight; //小图块的宽高
    int totalRow, totalCol; //总行数和列数
    int mapWidth; //地图的宽
    int mapHeight; //地图的高
    int viewColStart, viewColEnd; //从屏幕的列开始,到哪一列结束
    int viewRowStart, viewRowEnd; //从屏幕的行开始画到屏幕的下边结束(只画屏幕的里面的图像)


    short Data_Map_Back[][];
    short Data_Map_Block[][];
    short Data_Map_Back00[][] = new short[][] {

                                {18, 25, 23, 23, 23, 23, 1, 23}, {18, 25, 23,
                                23, 23, 29, 1, 1}, {18, 25, 23, 23, 41, 29, 1,
                                1}, {2, 21, 23, 23, 41, 23, 1, 1}, {2, 21, 23,
                                23, 41, 23, 23, 23}, {2, 21, 23, 1, 41, 23, 23,
                                1}, {2, 21, 23, 23, 41, 23, 23, 23}, {2, 21, 23,
                                23, 41, 41, 23, 23}, {2, 21, 23, 23, 41, 41, 23,
                                23}, {2, 21, 23, 1, 41, 41, 1, 23}, {2, 21, 23,
                                23, 41, 41, 1, 23}, {2, 21, 23, 41, 23, 41, 1,
                                23}, {2, 21, 23, 41, 23, 41, 1, 23}, {2, 21, 23,
                                41, 23, 41, 23, 23}, {2, 21, 23, 41, 23, 41, 23,
                                23}, {23, 23, 23, 23, 41, 41, 23, 23}, {23, 29,
                                23, 23, 23, 23, 23, 23}, {23, 29, 23, 23, 23,
                                23, 23, 23}, {23, 29, 23, 23, 23, 23, 23, 23},
                                {23, 29, 23, 23, 23, 23, 23, 23}, {23, 29, 18,
                                18, 18, 23, 23, 23}, {23, 23, 18, 23, 18, 23,
                                23, 23}, {23, 23, 18, 23, 18, 23, 23, 23}, {23,
                                23, 18, 23, 18, 23, 23, 23}, {23, 23, 18, 18,
                                18, 23, 23, 23}, {23, 23, 23, 18, 23, 23, 23,
                                23}, {23, 23, 23, 18, 23, 23, 23, 23}, {23, 23,
                                23, 18, 18, 18, 18, 18}, {23, 23, 23, 23, 23,
                                23, 23, 23}, {23, 23, 23, 23, 23, 23, 23, 23},
                                {23, 29, 23, 23, 23, 23, 23, 23}, {23, 29, 29,
                                23, 23, 23, 23, 23}, {23, 29, 29, 23, 23, 23,
                                23, 23}, {23, 23, 29, 23, 23, 23, 23, 23}, {23,
                                18, 18, 18, 18, 18, 18, 23}, {23, 18, 23, 23,
                                18, 18, 18, 23}, {23, 18, 23, 23, 23, 23, 18,
                                23}, {23, 18, 23, 23, 23, 23, 18, 23}, {23, 23,
                                18, 23, 23, 23, 18, 23}, {23, 23, 18, 18, 23,
                                23, 18, 23}, {23, 23, 23, 18, 18, 18, 18, 18},
                                {23, 23, 18, 18, 23, 23, 23, 23}, {23, 23, 23,
                                23, 23, 23, 23, 23}, {23, 23, 23, 23, 23, 23,
                                23, 23}

    };


    int totalImageRow, totalImageCol; //图片的行数与列数

    /**
     * 构造函数
     * @param img_map Image
     * @param tileWidth int
     * @param tileHeight int
     * @param mc MyCanvas
     */
    public Map(int tileWidth, int tileHeight, Canvas1 mc) {

        this.tileWidth = tileWidth; //地图块的宽
        this.tileHeight = tileHeight; //地图块的高
        this.mc = mc; //画布对象
    }

    /**
     * 转换地图
     * @param mapID byte
     */
    public void readmap(int mapID) {

        switch (mapID) {
        case 1:

        this.img_map = mc.tool.getImage("/Tiles0.png");

            this.Data_Map_Back = this.Data_Map_Back00;

            //this.Data_Map_Block = this.Data_Map_Block01;

            break;
        case 0:

            break;
        case 2:
            break;
        case 3:
            break;
        }

        int width = img_map.getWidth(); //整张图片的宽
        int height = img_map.getHeight(); //得到整张图片的高
        this.totalImageCol = width / this.tileWidth; //图片的格子列数
        this.totalImageRow = height / this.tileHeight; //图片的格子行数
        //得到整个地图的宽和高
        this.mapWidth = this.Data_Map_Back[0].length * this.tileWidth; //地图的宽
        this.mapHeight = Data_Map_Back.length * this.tileHeight; //地图的高

    }

    /**
     * 从文件中读取地图数组
     * @param pach String
     * @return short[][]
     */
    public short[][] InputMapData(String pach) {

        InputStream is; //字节流
        DataInputStream dis; //带格式数据流
        short mapData[][] = null;
        int rows = 0, cols = 0;
        is = this.getClass().getResourceAsStream(pach); //is对象赋值
        dis = new DataInputStream(is);
        try {
            rows = dis.readInt(); //读取行数
            cols = dis.readInt(); //读取列数
            mapData = new short[rows][cols]; //创建数组空间
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {

                    mapData[i][j] = dis.readShort(); //从流中读取数据一个short 类型

                }

            }

            is.close(); //
            dis.close();
        } catch (Exception e) {
            System.out.println("read file fail! ");
        }

        return mapData;
    }

    /**
     * 设置屏幕
     */
    private void setScreen() {
        this.viewColStart = mc.viewX / this.tileWidth; //屏幕的左边界哪一列上
        this.viewRowStart = mc.viewY / this.tileHeight; //屏幕的上边界哪一行上



        if ((mc.SCy) % this.tileHeight != 0) {
            this.viewRowEnd = this.viewRowStart +
                              (mc.SCy) / this.tileHeight + 1;
        } else {
            this.viewRowEnd = this.viewRowStart +
                              (mc.SCy) / this.tileHeight;
        }

        if (this.mc.SCx % this.tileWidth != 0) {

            {
                this.viewColEnd = this.viewColStart +
                                  mc.SCx / this.tileWidth + 1;
            }

        } else {
            this.viewColEnd = this.viewColStart +
                              mc.SCx / this.tileWidth;
        }

//有可能越界,所以我们需要加以控制

        if(viewColEnd<0)
        {
         viewColEnd =0;
        }
        if(this.viewRowEnd<0)
        {
        this.viewRowEnd =0;
        }
        if (this.viewColEnd > this.Data_Map_Back[0].length - 1) {
            // this.viewColEnd--;
            this.viewColEnd = Data_Map_Back[0].length - 1;
        }

        if (this.viewRowEnd > this.Data_Map_Back.length - 1) {
            //viewRowEnd--;

            viewRowEnd = this.Data_Map_Back.length - 1;
        }
//可以看到第几行(行)开始到行(列)结束
           System.out.println("start Col == " + this.viewColStart);

           System.out.println("end Col == " + this.viewColEnd);
           System.out.println("start row == " + this.viewRowStart);

          System.out.println("end rend == " + this.viewRowEnd);



    }

    public void paintMap(Graphics g) {



    }


}

⌨️ 快捷键说明

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