escapeeMIDlet.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:8k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.rms.*;
- import java.util.*;
- import java.io.*;
- public class escapeeMIDlet extends MIDlet implements Runnable
- {
- private static final String RS_NAME = "BESTTIME";
- private MenuList menuList;
- private escapeeCanvas myCanvas;
- private boolean initDone = false;
- private static final Random random = new Random();
- private boolean hasBestTime = false;
- private long bestTime;// 最好时间
- public escapeeMIDlet()
- {
- }
- public void startApp()
- {
- Displayable current = Display.getDisplay(this).getCurrent();
- if (current == null)
- {
- // first time we've been called
- Display.getDisplay(this).setCurrent(new SplashScreen(this));
- }
- else
- {
- if (current == myCanvas)
- {
- myCanvas.start(); // start its animation thread
- }
- Display.getDisplay(this).setCurrent(current);
- }
- }
- public void pauseApp()
- {
- Displayable current = Display.getDisplay(this).getCurrent();
- if (current == myCanvas)
- {
- myCanvas.stop(); // kill its animation thread
- }
- }
- public void destroyApp(boolean unconditional)
- {
- if (myCanvas != null)
- {
- myCanvas.stop(); // kill its animation thread
- }
- }
- private void quit()
- {
- destroyApp(false);
- notifyDestroyed();
- }
- public void run()
- {
- init();
- }
- private synchronized void init()
- {
- if (!initDone)
- {
- readRecordStore();
- SoundEffects.getInstance();
- menuList = new MenuList(this);
- myCanvas = new escapeeCanvas(this);
- initDone = true;
- }
- }
- void splashScreenPainted()
- {
- new Thread(this).start(); // start background initialization
- }
- void splashScreenDone()
- {
- init(); // if not already done
- Display.getDisplay(this).setCurrent(menuList);
- }
- void menuListContinue()
- {
- Display.getDisplay(this).setCurrent(myCanvas);
- myCanvas.start();
- }
- void menuListNewGame()
- {
- myCanvas.init();
- Display.getDisplay(this).setCurrent(myCanvas);
- myCanvas.start();
- }
- void menuListInstructions()
- {
- // create and discard a new Instructions screen each time, to
- // avoid keeping heap memory for it when it's not in use
- Display.getDisplay(this).setCurrent(new InstructionsScreen(this));
- }
- void menuListHighScore()
- {
- // create and discard a new High Score screen each time, to
- // avoid keeping heap memory for it when it's not in use
- Display.getDisplay(this).setCurrent(new HighScoreScreen(this));
- }
- void menuListQuit()
- {
- quit();
- }
- void GameCanvasMenu()
- {
- myCanvas.stop();
- menuList.setGameActive(true);
- Display.getDisplay(this).setCurrent(menuList);
- }
- void GameCanvasGameOver(long time,int BULLETS_NUM)
- {
- myCanvas.stop();
- menuList.setGameActive(false);
- Display.getDisplay(this).setCurrent(new GameOverScreen(this, time, BULLETS_NUM));
- }
- void gameOverDone()
- {
- Display.getDisplay(this).setCurrent(menuList);
- }
- void instructionsBack()
- {
- Display.getDisplay(this).setCurrent(menuList);
- }
- void highScoreBack()
- {
- Display.getDisplay(this).setCurrent(menuList);
- }
- // method needed by lots of classes, shared by putting it here
- static Image createImage(String filename)
- {
- Image image = null;
- try
- {
- image = Image.createImage(filename);
- }
- catch (java.io.IOException ex)
- {
- // just let return value be null
- }
- return image;
- }
- // only the MIDlet has access to the display, so put this method here
- void flashBacklight(int millis)
- {
- Display.getDisplay(this).flashBacklight(millis);
- }
- // only the MIDlet has access to the display, so put this method here
- void vibrate(int millis)
- {
- Display.getDisplay(this).vibrate(millis);
- }
- long getBestTime()
- {
- return hasBestTime ? bestTime : -1;
- }
- //****************************************
- boolean checkBestTime(long time)
- {
- if (!hasBestTime || (time > bestTime))
- {
- hasBestTime = true;
- bestTime = time;
- writeRecordStore();
- return true;
- }
- else
- {
- return false;
- }
- }
- private void readRecordStore()
- {
- hasBestTime = false;
- RecordStore rs = null;
- ByteArrayInputStream bais = null;
- DataInputStream dis = null;
- try
- {
- rs = RecordStore.openRecordStore(RS_NAME, false);
- byte[] data = rs.getRecord(1);
- bais = new ByteArrayInputStream(data);
- dis = new DataInputStream(bais);
- bestTime = dis.readLong();
- hasBestTime = true;
- }
- catch (IOException ex)
- {
- // hasBestTime will still be false
- }
- catch (RecordStoreException ex)
- {
- // hasBestTime will still be false
- }
- finally
- {
- if (dis != null)
- {
- try
- {
- dis.close();
- }
- catch (IOException ex)
- {
- // no error handling necessary here
- }
- }
- if (bais != null)
- {
- try
- {
- bais.close();
- }
- catch (IOException ex)
- {
- // no error handling necessary here
- }
- }
- if (rs != null)
- {
- try
- {
- rs.closeRecordStore();
- }
- catch (RecordStoreException ex)
- {
- // no error handling necessary here
- }
- }
- }
- }
- // this will only be called when we have a best time
- private void writeRecordStore()
- {
- RecordStore rs = null;
- ByteArrayOutputStream baos = null;
- DataOutputStream dos = null;
- try
- {
- rs = RecordStore.openRecordStore(RS_NAME, true);
- baos = new ByteArrayOutputStream();
- dos = new DataOutputStream(baos);
- dos.writeLong(bestTime);
- byte[] data = baos.toByteArray();
- if (rs.getNumRecords() == 0)
- {
- // new record store
- rs.addRecord(data, 0, data.length);
- }
- else
- {
- // existing record store: will have one record, id 1
- rs.setRecord(1, data, 0, data.length);
- }
- }
- catch (IOException ex)
- {
- // just leave the best time unrecorded
- }
- catch (RecordStoreException ex)
- {
- // just leave the best time unrecorded
- }
- finally
- {
- if (dos != null)
- {
- try
- {
- dos.close();
- }
- catch (IOException ex)
- {
- // no error handling necessary here
- }
- }
- if (baos != null)
- {
- try
- {
- baos.close();
- }
- catch (IOException ex)
- {
- // no error handling necessary here
- }
- }
- if (rs != null)
- {
- try
- {
- rs.closeRecordStore();
- }
- catch (RecordStoreException ex)
- {
- // no error handling necessary here
- }
- }
- }
- }
- }