gamemap.java~14~

来自「基于J2ME的手机游戏软件。可以控制游戏人物在地图上上下左右行走;可以在地图上放」· JAVA~14~ 代码 · 共 259 行

JAVA~14~
259
字号
public class GameMap {
  /*
   *	public static final int TILE0 = 0;
   public static final int TILE1 = 1;
   public static final int TILE2 = 2;
   public static final int ACTOR = 3;
   public static final int ENEMY1 = 4;
   public static final int ENEMY2 = 5;
   public static final int BOMB = 6;
   *
   */

  public static final int A = World.TILE0; //none
  public static final int B = World.TILE1; //metal
  public static final int C = World.TILE2; //brick
  public static final int D = World.ACTOR; //actor
  public static final int E = World.ENEMY1; //enemy1
  public static final int F = World.ENEMY2; //enemy2
  public static final int G = World.GATE;
  public static final int H = World.ENEMY4;
  public static final int I = World.BOSS; //boss
  public static final int J = World.ENEMY3;
  public static final int K = World.KEY;

  private int[][][] map;
  private int[] tileWidth;
  private int[] tileHeight;
  private int[][] gate;
  private int[] time;
  private int stageNum; //关卡总数
  private int currentStage; //当前关卡

  public GameMap()
  {
    stageNum = 3;
    map = new int[stageNum][][];
    tileWidth = new int[stageNum];
    tileHeight = new int[stageNum];
    gate = new int[stageNum][2];
    time = new int[stageNum];
    currentStage = 0;
    int count = 0;
    //stage_1
    int[][] stage_1 =
        {
        {B,B,B,B,B,B,B,B,B},
        {B,D,0,0,0,0,0,0,B},
        {B,0,B,0,B,0,B,0,B},
        {B,E,0,C,C,C,0,E,B},
        {B,0,B,0,B,0,B,0,B},
        {B,0,0,0,0,0,0,0,B},
        {B,B,B,B,B,B,B,B,B}
    };
    int stage_tileWidth = 32;
    int stage_tileHeight = 32;
    map[count] = stage_1;
    tileWidth[count] = stage_tileWidth;
    tileHeight[count] = stage_tileHeight;
    gate[count][0] = stage_tileWidth * 4;
    gate[count][1] = stage_tileHeight * 2;
    time[count] = 5 * 60 * 20;
    count++;
    //stage_2
    int[][] stage_2 =
        {
        {B,B,B,B,B,B,B,B,B},
        {B,D,C,C,C,C,C,0,B},
        {B,0,0,0,0,0,0,0,B},
        {B,0,B,C,B,C,B,E,B},
        {B,0,0,0,0,0,0,0,B},
        {B,0,C,C,C,C,C,0,B},
        {B,B,B,B,B,B,B,B,B}
    };
    stage_tileWidth = 32;
    stage_tileHeight = 32;
    map[count] = stage_2;
    tileWidth[count] = stage_tileWidth;
    tileHeight[count] = stage_tileHeight;
    gate[count][0] = stage_tileWidth * 4;
    gate[count][1] = stage_tileHeight * 3;
    time[count] = 5 * 60 * 20;
    count++;
    //stage_3
    int[][] stage_3 =
        {
        {B,B,B,B,B,B,B,B,B},
        {B,D,0,0,0,0,0,0,B},
        {B,0,0,C,B,C,0,0,B},
        {B,0,C,B,B,B,C,0,B},
        {B,0,0,C,B,C,0,0,B},
        {B,0,0,0,0,0,0,E,B},
        {B,B,B,B,B,B,B,B,B}
    };
    stage_tileWidth = 32;
    stage_tileHeight = 32;
    map[count] = stage_3;
    tileWidth[count] = stage_tileWidth;
    tileHeight[count] = stage_tileHeight;
    gate[count][0] = stage_tileWidth * 4;
    gate[count][1] = stage_tileHeight * 3;
    time[count] = 5 * 60 * 20;
    count++;
  }

  public int[][] getMap(int stage) { //获得一个地图数组的副本
    if (stage > stageNum)
    {
      return null;
    }
    else
    {
      int[][] bufMap = new int[map[stage - 1].length][map[stage - 1][0].length];
      for (int i = 0; i < bufMap.length; i++)
      {
        for (int j = 0; j < bufMap[0].length; j++)
        {
          bufMap[i][j] = map[stage - 1][i][j];
        }
      }
      currentStage = stage;
      return bufMap;
    }
  }

  public int getTileWidth(int stage)
  {
    if (stage > stageNum)
    {
      return -1;
    }
    else {
      return tileWidth[stage - 1];
    }
  }

  public int getTileHeight(int stage)
  {
    if (stage > stageNum)
    {
      return -1;
    }
    else {
      return tileHeight[stage - 1];
    }
  }

  public int getGateX(int stage)
  {
    if (stage > stageNum)
    {
      return -1;
    }
    else
    {
      return gate[stage - 1][0];
    }
  }

  public int getGateY(int stage)
  {
    if (stage > stageNum)
    {
      return -1;
    }
    else
    {
      return gate[stage - 1][1];
    }
  }

  public int getTime(int stage)
  {
    if (stage > stageNum)
    {
      return 1;
    }
    else
    {
      return time[stage - 1];
    }
  }

  public static int getActorPosX(int[][] bufMap, int tileWidth)
  {
    for (int i = 0; i < bufMap.length; i++)
    {
      for (int j = 0; j < bufMap[i].length; j++)
      {
        if (bufMap[i][j] == World.ACTOR)
        {
          return j * tileWidth;
        }
      }
    }
    return -100;
  }

  public static int getActorPosY(int[][] bufMap, int tileHeight)
  {
    for (int i = 0; i < bufMap.length; i++)
    {
      for (int j = 0; j < bufMap[i].length; j++)
      {
        if (bufMap[i][j] == World.ACTOR)
        {
          return i * tileHeight;
        }
      }
    }
    return -100;
  }

  public static int getEnemyNum(int[][] bufMap)
  {
    int result = 0;
    for (int i = 0; i < bufMap.length; i++)
    {
      for (int j = 0; j < bufMap[i].length; j++)
      {
        if (bufMap[i][j] >= World.ENEMY1 && bufMap[i][j] <= World.ENEMY4)
        {
          result++;
        }
      }
    }
    return result;
  }

  public static int[][] getEnemyPos(int[][] bufMap, int tWidth, int tHeight)
  {
    int[][] result = new int[getEnemyNum(bufMap)][3];
    int num = 0;
    for (int i = 0; i < bufMap.length; i++)
    {
      for (int j = 0; j < bufMap[i].length; j++)
      {
        if (bufMap[i][j] >= World.ENEMY1 && bufMap[i][j] <= World.ENEMY4)
        {
          result[num][0] = tWidth * j;
          result[num][1] = tHeight * i;
          result[num][2] = bufMap[i][j];
          num++;
        }
      }
    }
    return result;
  }

  public int getCurrentStage()
  {
    return currentStage;
  }

  public int getTotalStageNum()
  {
    return stageNum;
  }
}

⌨️ 快捷键说明

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