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

📄 map.java

📁 java开发的手机游戏 泡泡堂 可以在手机上运行
💻 JAVA
字号:
package game;

import javax.microedition.lcdui.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

//通用地图模块,只负责基本的地图绘制功能
public class Map extends Canvas {

  String sMapPath; //地图路径
  String sMapPath2; //建筑层地路径
  Image imgMap; //地图图片
  Image imgMap2;//建筑层//暂和地面分开,以后改善.
  int nImgWidth = 16, nImgHeight = 16; //地图图片元素的高宽
  byte byImgCols = 8, byImgRows = 8; //地图资源文件所有行列
  byte byScreenCols = 8, byScreenRows = 6; //屏幕所有行列
  int nScreenWidth = 128, nScreenHeight = 96;
  int nFloorCurrentFrame = -1;
  int nBuildCurrentFrame = -1;
  public int xStartPosit = 0, yStartPosit = 16;

//  地图偏移的位置
  byte byFloor[][] = { //地面层数据
      //1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15列数
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 1
      {1, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1}, // 2
      {1, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1}, // 3
      {1, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1}, // 4
      {1, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1}, // 5
      {3, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1}, // 6
      {1, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1}, // 7
      {3, 4, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1}, // 8
      {1, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1}, // 9
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 10
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 11
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // 12
      {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3}};// 13行数
  byte byMapCols = 12, byMapRows = 11; //地图数据所有行列
  byte byBuild[][] = { //建筑层数据
      //1,2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15列数
      {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0}, // 1
      {2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0}, // 2
      {2, 0, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0}, // 3
      {2, 0, 1, 0, 1, 1, 0, 1, 0, 2, 0, 0, 0, 0, 0}, // 4
      {2, 0, 2, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0}, // 5
      {2, 0, 2, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0}, // 6
      {2, 0, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0}, // 7
      {2, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0}, // 8
      {2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0}, // 9
      {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0}, // 10
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 11
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, // 12
      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}};// 13


  public static class Posit{//地面层坐标
    int ix,iy;
    byte byBuild,byFloor;
    Posit(){
      ix=0;iy=0;
    }
    Posit(int x,int y){
      ix = x;iy = y;
    }
  }
  
  //定义byMapRows行byMapCols列的地面坐标
  protected Posit[][] mapPosit = new Posit[byMapRows+1][byMapCols+1];//

  public void initPosit(){//初始化地面层坐标
    mapPosit[0][0] = new Posit(-this.nImgWidth,-this.nImgHeight);
    for (int i=0;i<byMapRows;i++)
      for(int j=0;j<byMapCols;j++){//从第一行一列开始
        mapPosit[i+1][j+1] = new Posit(xStartPosit+j*this.nImgWidth,
                                   yStartPosit+i*this.nImgHeight);
      }
  }


  public Map(String s,String s2) {
    try {
      sMapPath = s;
      sMapPath2 = s2;
      imgMap = Image.createImage(sMapPath); //加载地图
      imgMap2 = Image.createImage(sMapPath2);
    } catch (Exception e) {
      //System.out.println(e.getMessage());
    }
    initPosit();
  }
  /*------------------------------
   绘制地面层地图
   -------------------------------*/
  public void paintFloor(Graphics g, int row, int col) {

    int x = 0, y = 0, xIndex, yIndex;
    //容错
    if(col<=0)col=1;
    if(row<=0)row=1;
    //数据运算
    x = this.xStartPosit + this.nImgWidth * (col-1); //col最小是1;
    y = this.yStartPosit + this.nImgHeight * (row-1); //row最小是1;
    yIndex = getImgYIndex(byFloor,row,col); //求出地面层索引值图片在地图中Y的偏移量
    xIndex = getImgXIndex(byFloor,row,col); //求出地面层索引值图片在地图中X的偏移量
    //绘制开始
    g.setClip(x,y - nImgHeight,nImgWidth,nImgHeight);
    g.drawImage(imgMap, x - xIndex,
                y - yIndex - nImgHeight,
                g.LEFT | g.TOP); //绘制地面层地图
    g.setClip(0, 0, getWidth(), getHeight());
  }


  /*------------------------------
   绘制建筑层地图
   -------------------------------*/
  public void paintBuild(Graphics g,int row,int col){

    int x=0,y=0,xIndex,yIndex;
    //容错
    if(col<0)col=0;
    if(row<0)row=0;
      //数据运算
    x=this.xStartPosit +this.nImgWidth*(col-1);//col最小是1;
    y=this.yStartPosit+this.nImgHeight*(row-1);//row最小是1;
    yIndex = getImgYIndex(byBuild,row,col);//求出建筑层索引值图片在地图中Y的偏移量,因为是排除0的建筑层所以要加1,
    xIndex = getImgXIndex(byBuild,row,col);//求出建筑层索引值图片在地图中X的偏移量
    //绘制开始
    if(byBuild[row][col]>0){
      g.setClip(x,y-nImgHeight, nImgWidth,nImgHeight);
      g.drawImage(imgMap2,x - xIndex,
                  y - yIndex-nImgHeight,
                  g.LEFT | g.TOP); //绘制建筑层地图
      g.setClip(0,0,getWidth(),getHeight());
    }
  }

  /*------------------------------
   绘制所有地面层地图
   -------------------------------*/
  public void paintAllFloor(Graphics g){
    for(int i=1;i<9;i++){
      for(int j=1;j<9;j++){
        this.paintFloor(g,i,j);
      }
    }

  }
  
  public void paintAllBuild(Graphics g){
    for(int i=1;i<9;i++){
      for(int j=1;j<9;j++){
        this.paintBuild(g,i,j);
      }
    }
  }
  /*------------------------------
   绘制整张地图
   -------------------------------*/
  public void  paintAllMap(Graphics g){
  }

  protected void paint(Graphics g) {
  }

  /*-----------------------------------------
     //求出图层索引值图片在地图中Y的偏移量;
   ------------------------------------------*/
  public int getImgYIndex(byte Array[][],int row,int col){
    return (Array[row][col]-1)/byImgCols*nImgHeight;
  }
  /*-----------------------------------------
      //求出图层索引值图片在地图中X的偏移量
   ------------------------------------------*/
  public int getImgXIndex(byte Array[][],int row,int col){
    return (Array[row][col]-1)%byImgCols*nImgWidth;
  }
  //设置row行col列为建筑层bd
  public void setLayer(int row,int col,byte bd){
    byBuild[row][col] = bd;

  }

}

⌨️ 快捷键说明

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