服务热线:13616026886

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

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

用netbeans平台开发j2me游戏实例讲解2


  (3).建立draw类用来显示图形:
  
  public class draw {
  
  /** creates a new instance of draw */
  
  public draw(canvas canvas) {
  
  }
  
  public static boolean paint(graphics g, byte img, int x, int y) {
  
  //在地图的x,y点绘制img指定的图片
  
  try {
  
  paint(g, img, x, y, images.unit);//把地图x,y点转化成画布的绝对坐标,绘图
  
  return true;
  
  }
  
  catch (exception ex) {
  
  return false;
  
  }
  
  }
  
  public static boolean paint(graphics g, byte img, int x, int y, int unit) {
  
  try {
  
  switch (img) {
  
  case images.caocao://画曹操
  
  //变成绝对坐标,并做调整
  
  g.drawimage(images.image_caocao, images.left + x * unit,
  
  images.top + y * unit,graphics.top | graphics.left);
  
  break;
  
  case images.guanyu://画关羽
  
  g.drawimage(images.image_guanyu, images.left + x * unit,
  
  images.top + y * unit,graphics.top | graphics.left);
  
  break;
  
  case images.huangzhong://画黄忠
  
  g.drawimage(images.image_huangzhong, images.left + x * unit,
  
  images.top + y * unit,graphics.top | graphics.left);
  
  break;
  
  case images.machao://画马超
  
  g.drawimage(images.image_machao, images.left + x * unit,
  
  images.top + y * unit, graphics.top | graphics.left);
  
  break;
  
  case images.zhangfei://画张飞
  
  g.drawimage(images.image_zhangfei, images.left + x * unit,
  
  images.top + y * unit,graphics.top | graphics.left);
  
  break;
  
  case images.zhaoyun://画赵云
  
  g.drawimage(images.image_zhaoyun, images.left + x * unit,
  
  images.top + y * unit,
  
  graphics.top | graphics.left);
  
  break;
  
  case images.zu://画卒
  
  g.drawimage(images.image_zu, images.left + x * unit,
  
  images.top + y * unit, graphics.top | graphics.left);
  
  break;
  
  case images.blank://画空白
  
  g.drawimage(images.image_blank, images.left + x * unit,
  
  images.top + y * unit, graphics.top | graphics.left);
  
  break;
  
  case images.cursor://画光标
  
  g.drawrect(images.left + x * unit,
  
  images.top + y * unit,images.unit,images.unit);
  
  break;
  
  }
  
  return true;
  
  }catch (exception ex) {
  
  return false;
  
  }
  
  }
  
  }
  
  (4)建立map类来读取布局信息:
  
  
  package huarongdao;
  
  import java.io.inputstream;
  
  import javax.microedition.lcdui.*;
  
  /**
  
  *
  
  * @author lin
  
  */
  
  public class map {
  
  //处理游戏的地图,负责从外部文件加载地图数据,存放地图数据,并按照地图数据绘制地图
  
  public byte grid[][];//存放地图数据
  
  public map() {//构造函数,负责初始化地图数据的存储结构
  
  this.grid = new byte[images.height][images.width];
  
  //用二维数组存放地图数据,注意第一维是竖直坐标,第二维是水平坐标
  
  }
  
  public int[] read_map(int i) {
  
  //从外部文件加载地图数据,并存放在存储结构中,返回值是光标点的位置
  
  //参数是加载地图文件的等级
  
  int[] a = new int[2];//光标点的位置,0是水平位置,1是竖直位置
  
  try {
  
  inputstream is = getclass().getresourceasstream("/levels/level".concat(string.valueof(i)));
  
  if (is != null) {
  
  for (int k = 0; k < images.height; k++) {
  
  for (int j = 0; j < images.width; j++) {
  
  this.grid[k][j] = (byte) is.read();
  
  if ( this.grid[k][j] == images.cursor ) {
  
  //判断出光标所在位置
  
  a[0] = j;//光标水平位置
  
  a[1] = k;//光标竖直位置
  
  this.grid[k][j] = images.blank;//将光标位置设成空白背景
  
  }
  
  }
  
  is.read();//读取回车(13),忽略掉
  
  is.read();//读取换行(10),忽略掉
  
  }
  
  is.close();
  
  }else {
  
  //读取文件失败
  
  a[0] = -1;
  
  a[1] = -1;
  
  }
  
  }catch (exception ex) {
  
  //打开文件失败
  
  a[0] = -1;
  
  a[1] = -1;
  
  }
  
  return a;
  
  }
  
  public boolean draw_map(graphics g) {
  
  //调用draw类的静态方法,绘制地图
  
  try {
  
  for (int i = 0; i < images.height; i++) {
  
  for (int j = 0; j < images.width; j++) {
  
  draw.paint(g, this.grid[i][j], j, i);//绘制地图
  
  }
  
  }
  
  return true;
  
  }catch (exception ex) {
  
  return false;
  
  }
  
  }
  
  }
  
  注意这里的读文件操作的文件位置同样是相对于res文件夹的。
  
  (5) 建立主逻辑控制:
  
  package huarongdao;
  
  /**
  
  *
  
  * @author lin
  
  */
  
  import javax.microedition.lcdui.*;
  
  public class controllogic extends canvas implements commandlistener {
  
  private int[] loc = new int[2]; //光标的当前位置,0是水平位置,1是竖直位置
  
  private int[] selectarea = new int[4];//被选定的区域,即要移动的区域
  
  private int[] movearea = new int[4];//要移动到的区域
  
  private map mymap = new map();//地图类
  
  private boolean selected;//是否已经选中要移动区域的标志
  
  private int level;//当前的关
  
  public controllogic() {//构造函数
  
  try {
  
  jbinit();//jbuilder定义的初始化函数
  
  }catch (exception e) {
  
  e.printstacktrace();
  
  }
  
  }
  
  private void init_game(){
  
  //初始化游戏,读取地图,设置选择区域,清空要移动到的区域
  
  this.loc = mymap.read_map(this.level);//读取地图文件,并返回光标的初始位置
  
  //0为水平位置,1为竖直位置
  
  this.selectarea[0] = this.loc[0];//初始化选中的区域
  
  this.selectarea[1] = this.loc[1];
  
  this.selectarea[2] = 1;
  
  this.selectarea[3] = 1;
  
  this.movearea[0] = -1;//初始化要移动到的区域
  
  this.movearea[1] = -1;
  
  this.movearea[2] = 0;
  
  this.movearea[3] = 0;
  
  }
  
  private void jbinit() throws exception {//jbuilder定义的初始化函数
  
  //初始化实例变量
  
  this.selected = false;//设置没有被选中的要移动区域
  
  this.level = 1;
  
  images.init();//初始化图片常量
  
  init_game();//初始化游戏,读取地图,设置选择区域,清空要移动到的区域
  
  //setcommandlistener(this);//添加命令监听,这是displayable的实例方法
  
  //addcommand(new command("", command.exit, 1));//添加“退出”按钮
  
  }
  
  public void commandaction(command command, displayable displayable) {
  
  //命令处理函数
  
  if (command.getcommandtype() == command.exit) {//处理“退出”
  
  //huarongdaomidlet.quitapp();
  
  }
  
  }
  
  protected void paint(graphics g) {
  
  //画图函数,用于绘制用户画面,即显示图片,勾画选中区域和要移动到的区域
  
  try {
  
  g.drawimage(images.image_frame, 0, 0,graphics.top | graphics.left);//画背景
  
  mymap.draw_map(g);//按照地图内容画图
  
  if ( this.selected )
  
  g.setcolor(0,255,

扫描关注微信公众号