服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

j2me游戏开发中的地图设计与绘制


  在开发很多类型的游戏中,地图系统都需要良好的设计,直观的说,我们需要的地图系统仅仅是一个2d数组,然后用最快的方式将数组影射到屏幕上。
   游戏中的地图通常不是由程序员用键盘输入到程序里然后再在程序中修改然后再修改的狂乱过程,而是一般先由程序员做一个地图编辑器,在这个地图编辑器中用鼠标点点点,再保存的过程,或者是从网络上下载的一些成熟编辑器比如:mappy这样的工具生成地图,再用脚本语言为mappy写一个应该保存成什么样格式的程序。通常地图分为45度角,侧视角和俯视角等等,45度角的也有很多种,这种视角相对俯视角和侧视叫较复杂,我们主要讨论俯视角,其实侧视叫和俯视角主要的区别是图片的表现风格不一样,比如雷电这样的空战就是俯视角,mario这样的游戏就是侧视角,可以用相同的地图编辑器做出来。综上,你要知道游戏地图不是程序员用程序写出来的,你喜欢写也可以,修改起来较麻烦,也不能像资源一样动态管理而是一次性读入到内存里,比较不爽。

   在这个文章里面,我们假设我们的2d数组是通过,资源读取出来的,内容如下:

  public static byte[][] b_maze_2d_array = {
   {
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0}
   , {
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}
   , {
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

   };

  这个地图中一共0,1两种byte数,0代表一种图块,1代表一种图块,也可以是动画图块,你绘制的时候区别一下就可以了。因为我们程序里面想把这整个图形绘制出来的话有n多种方案,我给出两个比较合理的方案,当然第二种是比较优化的.我们假设你的主角一直在屏幕中央,当你主角移动的时候,地图相应的变化,就是说主角为参照物,地图动.我们知道地图的大小要超过屏幕的,我们需要设定一个坐标系统,我的方法是,以左上角为0,0也就是和我们常用的canvas的坐标系统是相同的,我们的图块大小为:element_width, element_height,所以我们整个地图的面积(绝对面积)是 element_width * 横坐标的块数 * element_height*纵坐标的块数。因此,我们把这么大的题图画在屏幕上时,需要把需要画的坐标面积(也就是屏幕面积)从这个地图中拿出来,其他地方被切除,这就比较的高效了。
  方法一:循环整个2维数组,不需要的地方不绘制只绘需要的部分:

   int ki = 0;// 表示
   int kj = 0;
  //nelestartedx, nelestartedy表示从2d array哪个位置开始绘制地图
   for (int i = nelestartedy; i < b_maze_2d_array.length; i++) {
   kj = 0;
  //是否需要绘制
   boolean isdrawed = false;
   for (int j = nelestartedx; j < b_maze_2d_array[i].length; j++) {
  //绘制需要的面积,n_maze_element_width,n_maze_element_height表示图块宽高
   int bx = nmapstartedx + j * n_maze_element_width;
   int by = nmapstartedy + i * n_maze_element_height;
  //screen_width,screen_height屏幕大小
   if (bx <= screen_width
   &&
   by <= screen_height
   &&
   bx >= -n_maze_element_width
   &&
   by >= -n_maze_element_height
   ) {
   g.drawimage(mapimages[b_maze_2d_array[i][j]], bx,
   by,
   graphics.top | graphics.left);//绘制图块
   isdrawed = true;
   kj++;
  // n_max_maze_item_x , n_max_maze_item_y屏幕面积内图块的最大值
   if (kj > n_max_maze_item_x + 2) {
   break;
   }

   }
   }
   if (isdrawed) {
   ki++;
   }
   if (ki > n_max_maze_item_y + 2) {
   break;
   }

   }

  方法二:事先找到需要绘制的横坐标纵坐标的图块编号(2darray的数组下标),循环屏幕面积大小的数组:

   // 需要绘制的2darray左上角位置,nmapstartedx,nmapstartedy在地图绝对面积上的坐标
   int narrayi = ( -n_maze_element_height - nmapstartedy) /
   n_maze_element_height;
   int narrayj = ( -n_maze_element_width - nmapstartedx) /
   n_maze_element_width;
   for (int i = narrayi;
   i < screen_height / n_maze_element_height + 2; i++) {
   for (int j = narrayj;
   j < screen_width / n_maze_element_width + 2; j++) {

   if (i < 0 || j < 0 || i > b_maze_2d_array.length ||
   j > b_maze_2d_array[0].length) {
   continue;
   }
   else {
   int bx = nmapstartedx + j * n_maze_element_width;
   int by = nmapstartedy + i * n_maze_element_height;
   g.drawimage(mapimages[b_maze_2d_array[i][j]], bx,
   by,
   graphics.top | graphics.left);

   }
  }
   }

  根据我的测试,方法一的地图面积越大fps掉的越为厉害,而方法二基本上不会掉fps,强烈推荐方法二.

  地图系统做好了之后,你就可以使用地图做更多的表现力了,只要改变nmapstartedx,nmapstartedy,就可以绘制出地图上的相应部分,代码的复用效率非常的高。rpg, slg, puzzle等游戏类型都可以使用.欢迎跟我探讨更多的游戏制作技术,我还将写一个关于动画的相关东东,不过最近没什么时间

扫描关注微信公众号