Ericl的j2me手机游戏之旅(五)主要游戏代码

Ericl的j2me手机游戏之旅(五)主要游戏代码

下面是Text.java的代码及注释。

=================================================================

import javax.microedition.lcdui.*;

 

public class Text extends Form implements CommandListener {//继承From容器,实现按钮监听接口

       public Text(int i) {

              super("");//From名称为“”

              if(i == 1) {//这里I的含义为当Begin.java生成新的Text对象时所输入的参数

                     try {//参数1的时候显示飞行的最远距离

                     Save.rs = Save.open(Save.dbName);//调用Save类获得Save对象rs

                     append("最远距离: "+Save.travelRS(Save.rs)+" ");//在屏幕上输出数据库里飞行的最远记录的数据

                     Save.rs.closeRecordStore();//关闭数据库

                     }catch(Exception e) {

                     }

              }else if(i == 2) {//参数2的时候显示帮助文档

                     append(new StringItem("帮助:"," 2键向上飞",Item.PLAIN));//创建StringItem对象,并且显示在屏幕上

                     append(new StringItem("版权声明: ","made in zy",Item.PLAIN));

              }

              addCommand(new Command("返回",Command.BACK,1));//创建按钮

              setCommandListener(this);//监听From

       }

       public void commandAction(Command c, Displayable dis) {

              if(c.getLabel()=="返回") {//如果“返回”按钮按下则显示到Beginl对象

                     Begin.display.setCurrent(Begin.l);

              }

       }

}

==============================================================

 

      恩,恩?这个代码好少呢。完全对不起一篇的长度。那我就再接再励,顺手继续写Save.JavaSave.java其实基本上照抄王森老师的《(书名忘记了)》中的一段程序。嘿嘿~~

 ============================================================

import javax.microedition.rms.*;

 

public class Save {

      

       static RecordStore rs;

       static String dbName = "feidb";

      

       public static RecordStore open(String rsname) {

              RecordStore rs = null;

              if(rsname.length() > 32)

                     return null;

              try {

                     rs = RecordStore.openRecordStore(rsname,true);

                     return rs;

              }catch(Exception e){

                     return null;

              }

       }

 

       public static int writeChar2RS(RecordStore rs,char data) {

              byte[]temp = new byte[2];

             temp[0] = (byte)(0xff&(data >> 8));

             temp[1] = (byte)(0xff&(data >> 0));

              try {

                     return rs.addRecord(temp,0,temp.length);

              }catch(Exception e){

              }

              return -1;

       }

      

       public static int writeInt2RS(RecordStore rs,int data) {

              byte[]temp = new byte[4];

             temp[0] = (byte)(0xff&(data >> 24));

             temp[1] = (byte)(0xff&(data >> 16));

             temp[2] = (byte)(0xff&(data >> 8));

             temp[3] = (byte)(0xff&(data >> 0));

              try {

                     return rs.addRecord(temp,0,temp.length);

              }catch(Exception e){

              }

              return -1;

       }

      

       public static char readChar4RS(RecordStore rs,int recordID) {

              byte[]temp = new byte[2];

              try {

                     temp = rs.getRecord(recordID);

              }catch(Exception e) {

              }

              char result = (char)(temp[0]&0x00ff);

              result = (char)((result << 8) + (char)(temp[1]&0x00ff));

              return result;

       }

      

       public static int readInt4RS(RecordStore rs,int recordID) {

              byte[]temp = new byte[4];

              try {

                     temp = rs.getRecord(recordID);

              }catch(Exception e) {

              }

              int result = (temp[0]&0x00ff);

              result = (result << 8) + (temp[1]&0x00ff);

              result = (result << 8) + (temp[2]&0x00ff);

              result = (result << 8) + (temp[3]&0x00ff);

              return result;

       }

      

       public static int travelRS(RecordStore rs) {

              int point1 = 0,point2 = 0,i=0;

              byte[] temp;

              try {

                     RecordEnumeration re = rs.enumerateRecords(null,null,false);

                     while(re.hasNextElement()) {

                            temp = re.nextRecord();

                            point1 = (temp[0]&0x00ff);

                            point1 = (point1 << 8) + (temp[1]&0x00ff);

                            point1 = (point1 << 8) + (temp[2]&0x00ff);

                            point1 = (point1 << 8) + (temp[3]&0x00ff);

                            if(point2 < point1)//就这里是我写的,也。

                                   point2 = point1;//比较分数,把数据库里最大的return

                     }

              }catch(Exception e) {

              }

              return point2;

       }

      

       public static void save(int point) {//这也是我写的哦,就是把分数point存入数据库,完全没有技术含量的方法~~

              rs = Save.open(dbName);

              try {

                     Save.writeInt2RS(rs,point);

                     rs.closeRecordStore();

              }catch(Exception e) {

              }

       }

}

==============================================================

       完了,下次写Game类了。Save没有写多少注释。如果有人有需要的话,留言,我写。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值