📄 milscore.java
字号:
/*import java.lang.System;import java.lang.Runnable;import java.lang.InterruptedException;*/import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;/*import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;*/ import javax.microedition.rms.RecordStore;import javax.microedition.rms.RecordStoreException;import javax.microedition.lcdui.*; /** * The WormScore keeps track of the high scores for each of the worm * levels. All access to the scores occur through static methods. There * can never be a WormScore object. * * We cache the scores and names for quick access during game play. The * calling application must first call openHighScores() to open the * score database. Finally, a closeHighScores() must be called to release * system resources. */public class MilScore { /** Array of high scores for each game level. */ public static int[] anRez = new int[5]; /** Array of player names for each high score level. */ public static String[] acRez = new String[5]; /** Current score for this game. */ private static RecordStore myStore; private static boolean highScoresHaveBeenInit = false; /** Default constructor can not be instantiated. */ private MilScore() { } /** * Initialize all high scores to 0. */ private static void initializeScores() { /* Initialize the score store */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); byte[] b; try { try { dos.writeInt(0); dos.writeUTF(""); b = baos.toByteArray(); dos.close(); } catch (IOException ioe) { throw new RecordStoreException(); } for (int i = 0; i < 5; i++) { myStore.addRecord(b, 0, b.length); anRez[i] = -1000; acRez[i] = "";// System.out.println("WWan="+anRez[i]+" - "+acRez[i]); } } catch (RecordStoreException rse) { /* Silently fail; exception to read high score is non-critical */ closeHighScores(); } } /** * Open the high score storage file. If the file doesn't exist, * initialize all high scores to 0. */ static void openHighScores() {// System.out.println("ide..."); try { myStore = RecordStore.openRecordStore("NajRez", true); if (highScoresHaveBeenInit) return; /* Intialize the internal score structures */ if (myStore.getNumRecords() == 0) { initializeScores();// System.out.println("init..."); } else {// System.out.println("read..."); /* Read high score store */ ByteArrayInputStream bais; DataInputStream dis; byte data[]; for (int i = 0; i < 5; i++) {// System.out.println("i="+i); data = myStore.getRecord(i+1); if (data != null) { try { bais = new ByteArrayInputStream(data); dis = new DataInputStream(bais); anRez[i] = dis.readInt(); acRez[i] = dis.readUTF();// System.out.println("an="+anRez[i]+" - "+acRez[i]); dis.close(); } catch (IOException ioe) { anRez[i] = -1000; acRez[i] = ""; // System.out.println( "upsREAD "+ioe.getMessage() ); } } else { anRez[i] = -1000; acRez[i] = "";// System.out.println("OOan="+anRez[i]+" - "+acRez[i]); } } } highScoresHaveBeenInit = true; } catch (RecordStoreException rse) {// System.out.println("ups"); /* Silently fail; exception to read high score is non-critical */ }} /** * Close the high score file */ static void closeHighScores() { if (myStore != null) { try { myStore.closeRecordStore(); } catch (RecordStoreException frse) { } myStore = null; } } /** * Save high score for posterity. * @param level current game level * @param newScore current game score to be recorded * @param name current user name to be recorded */ static void saveHighScore() { ByteArrayOutputStream baos; DataOutputStream das; byte[] data; for (int level = 0; level < 5; level++) { try { try { baos = new ByteArrayOutputStream(); das = new DataOutputStream(baos); das.writeInt((int)anRez[level]); das.writeUTF(acRez[level]); data = baos.toByteArray(); das.close(); } catch (IOException ioe) { throw new RecordStoreException(); } if (myStore == null) { openHighScores(); myStore.setRecord(level + 1, data, 0, data.length); closeHighScores(); } else { myStore.setRecord(level + 1, data, 0, data.length); } } catch (RecordStoreException rse) { /* Silently fail; exception to save high score is non-critical */ } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -