📄 gamemidlet.java
字号:
import javax.microedition.midlet.* ;
import javax.microedition.lcdui.* ;
import javax.microedition.rms.*;
import java.io.*;
public class GameMIDlet extends MIDlet
implements CommandListener
{
Display display ;
Alert alert;
int flag = 0;
private RecordStore rs;
private ByteArrayInputStream bis;
private DataInputStream dis;
private ByteArrayOutputStream bos;
private DataOutputStream dos;
private byte b[];
private int recordID;
private int i;
int Score = MyGameWithColliCanvas.Score;
public GameMIDlet()
{
display = Display.getDisplay(this) ;
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
b = new byte[256];
bis = new ByteArrayInputStream(b);
dis = new DataInputStream(bis);
}
//MyGameWithTiledLayerCanvas mgc ;
//MyGameWithSpriteRotateCanvas mgc ;
//MyGameWithSpriteRotateCanvas2 mgc ;
//MyGameWithSpriteRotateCanvas3 mgc ;
MyGameWithColliCanvas mgc ;
public void startApp()
{
if(mgc==null)
{
//mgc = new MyGameWithTiledLayerCanvas();
//mgc = new MyGameWithSpriteRotateCanvas();
//mgc = new MyGameWithSpriteRotateCanvas2();
//mgc = new MyGameWithSpriteRotateCanvas3();
mgc = new MyGameWithColliCanvas();
mgc.addCommand(new Command("继续", Command.OK, 1));
mgc.addCommand(new Command("结束", Command.EXIT, 2));
mgc.addCommand(new Command("重新开始", Command.OK, 1));
mgc.addCommand(new Command("保存", Command.OK, 1));
alert = new Alert("游戏进度"," Score : "+Score+"\n"+" 保存成功!",null,AlertType.INFO);
alert.setTimeout(1000);
mgc.setCommandListener(this);
display.setCurrent(mgc);
}
}
public void commandAction(Command c,Displayable s)
{
String cmd = c.getLabel() ;
if(cmd.equals("重新开始"))
{
MyGameWithColliCanvas.Score = 10;
mgc.start() ;
}else if(cmd.equals("结束"))
{
mgc.exit() ;
notifyDestroyed() ;
}
else if (cmd.equals("继续"))
{
flag = 1;
try
{
rs = RecordStore.openRecordStore("score",true);
for (i = 1; i <= rs.getNumRecords()-1; i++)
{
rs.getRecord(i, b, 0);
bis.reset();
}
bis.reset();
rs.getRecord(i, b, 0);
String temp;
temp = dis.readUTF();
Score = Integer.parseInt(temp);
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage() );
}
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage() );
}
mgc.start() ;
}
else if (cmd.equals("保存"))
{
try
{
rs = RecordStore.openRecordStore("score",true);
dos.writeUTF(String.valueOf(Score));
dos.flush();
b = bos.toByteArray();
recordID = rs.addRecord(b,0,b.length);
display.setCurrent(alert);
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage() );
}
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
System.out.println("Error: "+e.getMessage() );
}
}
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
try{
bis.close();
bos.close();
dis.close();
dos.close();
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -