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

📄 indnxmidlet.java

📁 我用J2ME写的一个手机游戏
💻 JAVA
字号:
/*
 *游戏中的一些方法名和变量名一定用拼音组成,
 * 变量的开头2个字母用大写有实质意义的字母用大写;
 *  方法名开头用小写 ,有实质意义的字母用大写 ; 
 *  类名开头字母用大写,有实质意义的字母用大写;
 * 对象名要在有实质意义的子动中间加”_”(例如: mu_hou).  
 *  所有方法,类,变量在声明时要注释作用;
 */
package indnx;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;


import javax.microedition.rms.RecordStoreNotOpenException;



/**
 * 
 */
public class IndnxMIDlet extends MIDlet implements CommandListener {
    Display display;
    protected Form f ,f1,f2;
    static Image splashLogo;
 	private List caiDan ;
    public Command ok , back,exit,faPai;
    static Alert alert;
    public Hui24Canvas canvas;
    protected IndnxMIDlet midlet;
    private RecordStore rs;
	/**
	 * 
	 */
	public IndnxMIDlet() {
		super();
		display= Display.getDisplay( this);
		caiDan = new List("",Choice.IMPLICIT);
		f = new Form("");
		f1=new Form("高分榜");
		f2=new Form("相关说明");
		CaiDan();
		try{
			splashLogo=Image.createImage( "/p4.png");
		}catch (IOException e){e.printStackTrace();}
		alert(display,caiDan,splashLogo);

		caiDan.setCommandListener(this);
        f1.setCommandListener(this);
        f2.setCommandListener(this);
        f=null;splashLogo=null;
        faPai =new Command("发牌",Command.HELP,1);
	    exit =new Command ("退出",Command.EXIT,1);
	}

	/* 
	 */
	protected void startApp(boolean b) throws MIDletStateChangeException {
		 canvas =new Hui24Canvas(midlet);
		canvas.addCommand( this.exit);
		canvas.addCommand( this.faPai);
		canvas.setCommandListener(this);
		display.setCurrent(canvas);

	}

	/* 
	 */
	protected void pauseApp() {
	

	}

	/* 
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	

	}

	/* 监听菜单按键!给于相应回应
	 */
	public void commandAction(Command c, Displayable d) {
		int cmd = c.getCommandType();
		if(cmd==Command.OK ){
		switch(caiDan.getSelectedIndex()){
		 case 0: 
		 	try {
					startApp(true);
				} catch (MIDletStateChangeException e) {
				
					e.printStackTrace();
				}
		 	break;
		 case 1: YouXiCaoZuo() ;
		 	break;
		 case 2: XiangGuanShuoMing() ;
		 	break;
		 case 3: TuiChuYouXi();
		 	break;}}else if(cmd==Command.BACK ){		 		
		 		Cai();
	}else if(cmd==Command.HELP){
		if(canvas.FA_PAI){canvas.RunTimer();}		
	}
	else if(cmd==Command.EXIT ){
		try {
			this.recordStore();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		canvas.tuiChu();
	     Cai();	
	}
		}
	//退出游戏
   public void exit(){
   	try {
		destroyApp(false);
	} catch (MIDletStateChangeException e) {
	
		e.printStackTrace();
	}
   	notifyDestroyed();
   }


  	//游戏splashLogo
   public static  final void alert(Display display,List f,Image li){
	alert = new Alert("","",li,AlertType.INFO);
	alert.setTimeout(3000);

	display.setCurrent(alert,f);
}
  
   
//   	构造函数,事例化菜单选相与到入数据
   	public void CaiDan(){
   		 ok = new Command("Ok",Command.OK,1);
   		 back=new Command("Back",Command.BACK ,1);
   		 caiDan.append("开始游戏",null);
   		 caiDan.append(" 高分榜",null);
   		 caiDan.append("相关说明",null);
   		 caiDan.append("退出游戏",null);
   		 caiDan.addCommand( ok);
   		 caiDan.addCommand( back);   		
   	}

   	public void Cai(){
   		
   		display.setCurrent(caiDan);
   	}
    
   	
//显示相关操作
   	protected void YouXiCaoZuo(){  		   		
   	
   	     display.setCurrent(f1); 	   
   	}
//   	显示相关说明
   	protected void XiangGuanShuoMing(){		  		
   		
   	    display.setCurrent(f2);
   	}
//   	退出游戏
   	protected void TuiChuYouXi(){   		
   		exit();
   	}
   	public void recordStore() throws RecordStoreNotOpenException, InvalidRecordIDException, IOException, RecordStoreException{
   		
			rs=RecordStore.openRecordStore("gaoFen",true );
			for(int i=5;i>0;i--){
			int pig=IndnxMIDlet.InputData(rs.getRecord(i));
				if(pig<Hui24Canvas.FenShu){
					byte[]cat=OutputData(Hui24Canvas.FenShu);
				rs.setRecord(i,cat,0,cat.length );
				break;
				}
			}
			
			rs.closeRecordStore();
		
	
   	}
   	public void  OpenRecord(){
   		try {
			rs=RecordStore.openRecordStore("gaoFen",false );
		} catch (Exception e) {
			try {
				rs=RecordStore.openRecordStore("gaoFen",true );
				int pi=500,pi2=1000,pi3=2000,pi4=5000,pi5=10000;
				
					rs.addRecord(OutputData(pi),0,OutputData(pi).length );
					rs.addRecord(OutputData(pi2),0,OutputData(pi2).length );
					rs.addRecord(OutputData(pi3),0,OutputData(pi3).length );
					rs.addRecord(OutputData(pi4),0,OutputData(pi4).length );
					rs.addRecord(OutputData(pi5),0,OutputData(pi5).length );
			} catch (Exception e1) {
				e1.printStackTrace();
			} 
		} 
		try {
			rs.closeRecordStore();
		} catch (Exception e1) {	
			e1.printStackTrace();
		} 
   	}
	private void DuQuShuJu() throws Exception{
   		rs=RecordStore.openRecordStore("gaoFen",true );
   		f1.append("第一名 : "+InputData(rs.getRecord(5))+"分 \n");
   		f1.append("第二名 : "+InputData(rs.getRecord(4))+"分 \n");
   		f1.append("第三名 : "+InputData(rs.getRecord(3))+"分 \n");
   		f1.append("第四名 : "+InputData(rs.getRecord(2))+"分 \n");
   		f1.append("第五名 : "+InputData(rs.getRecord(1))+"分 \n");
   		rs.closeRecordStore();
   	}
   	private static  final byte[] OutputData(int k){
   		ByteArrayOutputStream peter =new ByteArrayOutputStream();
   		DataOutputStream bo =new DataOutputStream(peter);
   		try {
			bo.writeInt(k);
			bo.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	     byte[]Data=peter.toByteArray();
		return Data;	
    }
   	private static  final int InputData(byte[] Data) throws IOException{
   		DataInputStream bo =new DataInputStream(new ByteArrayInputStream(Data));
   		int intFen=bo.readInt();
		return intFen;		
   	}
protected void startApp() throws MIDletStateChangeException {
try {

	        OpenRecord();
			DuQuShuJu();
		} catch (Exception e) {
			e.printStackTrace();
		}
		 f1.addCommand(this.back);
//	     f1.append(lis);
	StringItem list = new StringItem("","游戏的规则:\n 以牌面的数值为大小,进行加减乘除。每张牌只能用一次,符号的优先级分别是(),*,/,+,-\n" +
			"游戏的操作:\n 1,2,3,4 对应的是相应牌面的数字,5键是括号,6键是“+,-,*,/”运算符 “*”号是确定,“#”是无答案和删除");
	f2.addCommand(this.back);
    f2.append(list);
}
   	}

⌨️ 快捷键说明

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