📄 blitzmidlet.java
字号:
/* This file was created by Nokia Developer's Suite for J2ME(TM) */
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
public class BlitzMIDlet extends MIDlet
{
private final Dictionary dictionary;
private final MainMenu mainMenu;
private final GameEffects gameEffects;
private GameManager gameManager = null;
private SettingsScreen settingsScreen = null;
private int hiScore = 0;
private int hiLevel = 1;
public BlitzMIDlet()
{
boolean bCont;
ByteArrayInputStream gameBais;
int iDummy;
dictionary =
new Dictionary(System.getProperty("microedition.locale"),
System.getProperty("microedition.encoding"));
gameEffects = makeGameEffects();
bCont = Settings.getContinue();
try
{
gameBais = Settings.getArrayValue(Settings.GM_MAIN_DATA);
DataInputStream inputStream = new DataInputStream(gameBais);
hiLevel = inputStream.readInt();
hiScore = inputStream.readInt();
}
catch (Exception e)
{
throw new IllegalArgumentException("Error in mainMenuNewGame:try setGameData(2)");
}
mainMenu = new MainMenu(this, dictionary, gameEffects, bCont);
}
protected void startApp( ) throws MIDletStateChangeException
{
Displayable current = Display.getDisplay(this).getCurrent();
if (current == null)
{
// Use a splash screen, the first time we are called.
// The main menu screen will be shown after the splash.
// First show the company splash screen
SplashScreen splashScreen = new SplashScreen(this, mainMenu);
Display.getDisplay(this).setCurrent(splashScreen);
splashScreen.start(); // disappear after a fixed time
}
else
{
Display.getDisplay(this).setCurrent(current);
if ((gameManager != null) && (current == gameManager.getCanvas()))
{
gameManager.resume();
}
}
}
protected void pauseApp( )
{
}
protected void destroyApp( boolean p1 ) throws MIDletStateChangeException
{
storeGame();
}
// SplashScreen callback
void splashScreenDone(Displayable next)
{
Display.getDisplay(this).setCurrent(next);
}
//**********************//
// GameManager callback //
//**********************//
void gameManagerMainMenu(boolean isGameOver, int currentHiScore)
{
ByteArrayOutputStream gameBaos = new ByteArrayOutputStream();
// If the game is over, remove the
// 'Continue' command from the MainMenu.
if (isGameOver)
{
Settings.setContinue(false);
mainMenu.deleteContinue();
try
{
gameBaos = gameManager.getGameData();
Settings.setValue(Settings.GM_MAIN_DATA, gameBaos);
}
catch(Exception e)
{
// We can not do anything to recover from this error,
// let the game proceed without using saved settings.
throw new IllegalArgumentException("Error in gameManagerMainMenu:try setValue (1)");
}
hiScore = currentHiScore;
}
else
{
Settings.setContinue(true);
// highlight 'Continue' in the main menu
mainMenu.selectContinue();
}
Display.getDisplay(this).setCurrent(mainMenu);
}
//********************//
// MainMenu callbacks //
//********************//
void mainMenuContinue()
{
if (gameManager != null)
Display.getDisplay(this).setCurrent(gameManager.getCanvas());
else
{
mainMenuNewGame(true);
}
}
void mainMenuNewGame(boolean getData)
{
// gameManager uses the canvas for repaints,
// determining canvas width, height, etc.
// Create a new game manager if one does not exist
if (gameManager == null)
{
Canvas closeableCanvas = makeCloseableCanvas();
gameManager = new GameManager(this, dictionary,
gameEffects, closeableCanvas);
// The canvas delegates drawing of the game and handling of
// keyPressed, keyReleased or 'closePressed' events to gameManager.
((SettableDelegate)closeableCanvas).setDelegate(gameManager);
}
else gameManager.init();
// If a saved game has been continued hen we need to upload the stored data
if (getData)
{
try
{
ByteArrayInputStream gameBais = Settings.getArrayValue(Settings.GM_MAIN_DATA);
ByteArrayInputStream planeBais = Settings.getArrayValue(Settings.GM_PLANE_DATA);
ByteArrayInputStream bombBais = Settings.getArrayValue(Settings.GM_BOMB_DATA);
ByteArrayInputStream buildingBais = Settings.getArrayValue(Settings.GM_BUILD_DATA);
gameManager.setGameData(gameBais);
gameManager.setPlaneData(planeBais);
gameManager.setBombData(bombBais);
gameManager.setBuildingData(buildingBais);
}
catch (Exception e)
{
throw new IllegalArgumentException("Error in mainMenuNewGame:try setGameData(1)");
}
}
else
{
// Just set the HiScore & HiLevel
try
{
ByteArrayInputStream gameBais = Settings.getArrayValue(Settings.GM_MAIN_DATA);
gameManager.setGameHi(gameBais);
}
catch (Exception e)
{
throw new IllegalArgumentException("Error in mainMenuNewGame:try setGameData(2)");
}
}
gameManager.start(getData);
// Set the display to be the game manager's canvas.
Display.getDisplay(this).setCurrent(gameManager.getCanvas());
// After the game manager's canvas is displayed and
// the game is running, the 'Continue' option will be
// needed from the main menu.
mainMenu.addContinue();
}
void mainMenuSettings()
{
if (settingsScreen == null)
{
settingsScreen = new SettingsScreen(this, dictionary, mainMenu);
}
Display.getDisplay(this).setCurrent(settingsScreen);
}
void mainMenuInstructions()
{
String title = dictionary.getString(Dictionary.LABEL_INSTRUCTIONS);
String back = dictionary.getString(Dictionary.LABEL_BACK);
String text = dictionary.getString(Dictionary.TEXT_INSTRUCTIONS);
TextScreen screen = new TextScreen(this, title, text, back);
Display.getDisplay(this).setCurrent(screen);
}
void mainMenuHiScore()
{
StringBuffer buff = new StringBuffer("");
buff.append(hiScore);
String score = new String(buff);
String title = dictionary.getString(Dictionary.LABEL_HISCORE);
String back = dictionary.getString(Dictionary.LABEL_BACK);
TextScreen aboutScreen = new TextScreen(this, title, score, back);
Display.getDisplay(this).setCurrent(aboutScreen);
}
void mainMenuExit()
{
// destroyApp(false);
storeGame();
notifyDestroyed();
}
// SettingEditor callbacks
void settingEditorSave(String name, boolean isOn)
{
// update game setting and settings screen
if (name.equals(dictionary.getString(Dictionary.LABEL_VIBRATION)))
{
Settings.setUseVibration(isOn);
settingsScreen.setUseVibration(isOn);
}
else if (name.equals(dictionary.getString(Dictionary.LABEL_SOUND)))
{
Settings.setUseSound(isOn);
settingsScreen.setUseSound(isOn);
}
Alert confirm = new Alert(null,
(name + " " + settingsScreen.onOffString(isOn)),
null, AlertType.INFO); // no title
confirm.setTimeout(4000); // show Alert for 4 seconds
Display.getDisplay(this).setCurrent(confirm, settingsScreen);
}
void settingEditorBack()
{
Display.getDisplay(this).setCurrent(settingsScreen);
}
// SettingsScreen callbacks
void settingsScreenBack(Displayable last)
{
settingsScreen = null; // It can be garbage collected now.
if ((gameManager != null) && (last == gameManager.getCanvas()))
{
gameManager.resume();
}
Display.getDisplay(this).setCurrent(last);
}
void settingsScreenEdit(String name, boolean isOn)
{
Display.getDisplay(this).setCurrent(
new SettingEditor(this, dictionary, name, isOn));
}
// TextScreen callbacks (i.e. About + Instructions)
void textScreenClosed()
{
Display.getDisplay(this).setCurrent(mainMenu);
}
// Factory-like methods
//
// The Nokia vendor-specific APIs FullCanvas, Sound, and DeviceControl
// are used if available. If not, use an alternative based on
// the capabilities of standard MIDP instead.
private GameEffects makeGameEffects()
{
try
{
Class.forName("com.nokia.mid.sound.Sound");
Class.forName("com.nokia.mid.ui.DeviceControl");
Class.forName("com.nokia.mid.ui.DirectUtils");
// If no exception was thrown, use the vendor-specific APIs.
Class clas = Class.forName("NokiaGameEffects");
return (GameEffects) clas.newInstance();
}
catch (Exception e)
{
// The vendor-specific APIs are not available,
// so use the 'stub' GameEffects instead.
return new GameEffects();
}
}
private Canvas makeCloseableCanvas()
{
try
{
Class.forName("com.nokia.mid.ui.FullCanvas");
// If no exception was thrown, use a 'closeable Canvas'
// based on the vendor-specific FullCanvas class.
Class clas = Class.forName("NokiaCloseableCanvas");
return (Canvas) clas.newInstance();
}
catch (Exception e)
{
// If the vendor-specific FullCanvas API isn't available,
// use the default CloseableCanvas (derived from Canvas).
String closeLabel = dictionary.getString(dictionary.LABEL_BACK);
return new CloseableCanvas(closeLabel);
}
}
void storeGame()
{
ByteArrayOutputStream gameBaos = new ByteArrayOutputStream();
ByteArrayOutputStream planeBaos = new ByteArrayOutputStream();
ByteArrayOutputStream bombBaos = new ByteArrayOutputStream();
ByteArrayOutputStream buildingBaos = new ByteArrayOutputStream();
if (gameManager != null)
{
// Set the continue flag depending on the game status
if (gameManager.getStatus() == 3 || gameManager.getStatus() == 6)
// Game was over so kill the continue flag
Settings.setContinue(false);
else
// A game was in progress so save the continue flag
Settings.setContinue(true);
try
{
gameBaos = gameManager.getGameData();
Settings.setValue(Settings.GM_MAIN_DATA, gameBaos);
}
catch(Exception e)
{
// We can not do anything to recover from this error,
// let the game proceed without using saved settings.
throw new IllegalArgumentException("Error in DestroyApp:try setValue (1)");
}
try
{
planeBaos = gameManager.getPlaneData();
Settings.setValue(Settings.GM_PLANE_DATA, planeBaos);
}
catch(Exception e)
{
// We can not do anything to recover from this error,
// let the game proceed without using saved settings.
throw new IllegalArgumentException("Error in DestroyApp:try setValue (2)");
}
try
{
bombBaos = gameManager.getBombData();
Settings.setValue(Settings.GM_BOMB_DATA, bombBaos);
}
catch(Exception e)
{
// We can not do anything to recover from this error,
// let the game proceed without using saved settings.
throw new IllegalArgumentException("Error in DestroyApp:try setValue (3)");
}
try
{
buildingBaos = gameManager.getBuildingData();
Settings.setValue(Settings.GM_BUILD_DATA, buildingBaos);
}
catch(Exception e)
{
// We can not do anything to recover from this error,
// let the game proceed without using saved settings.
throw new IllegalArgumentException("Error in DestroyApp:try setValue (3)");
}
gameManager.stop();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -