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

📄 map.java

📁 这个是早期学习时写的RPG游戏 包括地图相关 战斗相关 NPC与 存储等 功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      short Data_Map_Back000[][] = new short[][] {
                                   { 73, 74, 61, 78, 79, 80, 86, 87, 88, 89, 92 },
                              { 81, 61, 61, 86, 87, 88, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 58, 59, 60, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 66, 65, 68, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 75, 76, 77, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 },
                              { 61, 61, 61, 61, 61, 61, 61, 61, 61, 61, 61 }


       };


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

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

        this.tileWidth = tileWidth; //地图块的宽
        this.tileHeight = tileHeight; //地图块的高
        this.mc = mc; //画布对象
        myImage = new MyImage(mc);
    }

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

        switch (mapID) {
       case -1:  //开头剧情战斗场景
            this.img_map = myImage.map0;
             this.Data_Map_Back = this.Data_Map_Back000;
           break;
        case 0: //片頭對話場景
            this.img_map = myImage.map0;
                      this.Data_Map_Back = this.Data_Map_Back00;
                      this.Data_Map_Shelter = this.Data_Map_Shelter00;

            break;
        case 1://第一個村莊

            this.img_map = myImage.map1;
            this.Data_Map_Back = this.Data_Map_Back01;
            this.Data_Map_Shelter = this.Data_Map_Shelter01;
            this.Data_Map_Block = this.Data_Map_Block01;

            break;

        case 2: //武器商店
            this.img_map = myImage.map6;
            this.Data_Map_Back = this.Data_Map_Back02;
            this.Data_Map_Block = this.Data_Map_Block02;
            Data_Map_Shelter = null;
            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.screenHeight) % this.tileHeight != 0) {
            this.viewRowEnd = this.viewRowStart +
                              (mc.screenHeight) / this.tileHeight + 1;
        } else {
            this.viewRowEnd = this.viewRowStart +
                              (mc.screenHeight) / this.tileHeight;
        }

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

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

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

//有可能越界,所以我们需要加以控制
        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) {
        setScreen(); //设置屏幕
        //*********画整个地图***********
//      for (int i = 0; i <this.Data_Map_Back.length; i++) {
//       for (int j = 0; j < this.Data_Map_Back[0].length; j++)
//***********画屏幕里面的内容不画屏幕外面的内容
          for (int i = this.viewRowStart; i <= this.viewRowEnd; i++) {
              for (int j = this.viewColStart; j <= this.viewColEnd; j++)

              {
                  int row;
                  int col;

//              System.out.println("col==="+Data_Map_Back.length);
//              System.out.println("col==="+Data_Map_Back[0].length);

                  col = (this.Data_Map_Back[i][j] - 1) % this.totalImageCol;
                  row = (this.Data_Map_Back[i][j] - 1) / this.totalImageCol;

                  int row2;
                  int col2;
                  //切割图片画地图  画背景曾
                  mc.tools.drawPartImage(g, this.img_map,
                                         j * this.tileWidth - mc.viewX,
                                         i * this.tileHeight - mc.viewY,
                                         this.tileWidth * col,
                                         this.tileHeight * row,
                                         this.tileWidth, this.tileHeight);

              }

          }

    }
    public void paintMapShelter(Graphics g) {
    setScreen(); //设置屏幕
    //*********画整个地图***********
//      for (int i = 0; i <this.Data_Map_Back.length; i++) {
//       for (int j = 0; j < this.Data_Map_Back[0].length; j++)
//***********画屏幕里面的内容不画屏幕外面的内容
      for (int i = this.viewRowStart; i <= this.viewRowEnd; i++) {
          for (int j = this.viewColStart; j <= this.viewColEnd; j++)

          {
              int row;
              int col;

              col = (this.Data_Map_Shelter[i][j] - 1) % this.totalImageCol;
              row = (this.Data_Map_Shelter[i][j] - 1) / this.totalImageCol;

              int row2;
              int col2;
              //切割图片画地图  画背景曾
              mc.tools.drawPartImage(g, this.img_map,
                                     j * this.tileWidth - mc.viewX,
                                     i * this.tileHeight - mc.viewY,
                                     this.tileWidth * col,
                                     this.tileHeight * row,
                                     this.tileWidth, this.tileHeight);

          }

      }

}



}

⌨️ 快捷键说明

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