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

📄 starassault.java

📁 J2ME Game Programming的隋书源代码
💻 JAVA
字号:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;

/**
 * The application class for the game StarAssault.
 */

public class StarAssault extends MIDlet
{
   private static StarAssault theApp;
   private static Image generalGraphics;		// static graphics pack (used by many classes)

   // options
   private boolean optionAutoFire = false;
   private boolean optionVibrate = true;
   private boolean optionStarField = true;
   private int leftKeyNum = 4;
   private int rightKeyNum = 6;
   private int fireKeyNum = 5;

   private GameScreen gs;
   private boolean hasAGameRunning;
   private Displayable currentDisplay;

   public StarAssault()
   {
      theApp = this;

      loadSettings();	// this can fail if they have never saved anything
      currentDisplay = new Splash(this);

      // start the init of the graphics
      Ship.setup();
      Bullet.setup(getGeneralGraphics());

      // init the gamescreen
      gs = new GameScreen(this);
   }

   public static StarAssault getApp()
   {
      return theApp;
   }

   public static Image getGeneralGraphics()
   {
      if (generalGraphics == null)
         generalGraphics = ImageSet.loadClippedImage("/general.png", 0, 0);
      return generalGraphics;
   }


   public void startApp() throws MIDletStateChangeException
   {
      activateDisplayable(currentDisplay);

      if (gs != null)
      {
         if (gs.getState() == GameScreen.PAUSED)
            gs.resume();
      }
   }

   public void pauseApp()
   {
      if (gs != null)
      {
         gs.pause();
      }
   }

   public void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

   public void close()
   {
      try
      {
         destroyApp(true);
         notifyDestroyed();
      }

      catch (MIDletStateChangeException e)
      {
      }
   }

   public GameScreen getGS()
   {
      return gs;
   }

   public void activateMenu()
   {
      currentDisplay = new DynaMenu();
      activateDisplayable(currentDisplay);
   }

   public void createNewGame()
   {
      if (gs == null)
         gs = new GameScreen(getApp());
      gs.startNewGame();
      activateGameScreen();
   }

   public void loadGame()
   {
      if (gs == null)
         gs = new GameScreen(getApp());
      gs.loadGame();
      activateGameScreen();
   }

   public void activateGameScreen()
   {
      if (gs == null || gs.getState() == GameScreen.GAME_DONE)
         createNewGame();

      hasAGameRunning = true;
      gs.resume();
      currentDisplay = gs;
      activateDisplayable(gs);
   }

   public boolean hasAGameRunning()
   {
      if (!hasAGameRunning) return false;

      if (gs.getState() != GameScreen.GAME_DONE)
         return true;
      else
         return false;
   }

   public static void activateDisplayable(Displayable s)
   {
      try
      {
         Display.getDisplay(getApp()).setCurrent(s);
      }
      catch (Exception e)
      {
         System.out.println("App exception: " + e);
         e.printStackTrace();
      }
   }

   public static void activateAlert(Alert a, Displayable fallBackTo)
   {
      try
      {
         Display.getDisplay(getApp()).setCurrent(a, fallBackTo);
      }
      catch (Exception e)
      {
         System.out.println("App exception: " + e);
         e.printStackTrace();
      }
   }

   public boolean isOptionAutoFire()
   {
      return optionAutoFire;
   }

   public void setOptionAutoFire(boolean optionAutoFire)
   {
      this.optionAutoFire = optionAutoFire;
   }

   public boolean isOptionStarField()
   {
      return optionStarField;
   }

   public void setOptionStarField(boolean optionStarField)
   {
      this.optionStarField = optionStarField;
   }

   public boolean isOptionVibrate()
   {
      return optionVibrate;
   }

   public void setOptionVibrate(boolean optionVibrate)
   {
      this.optionVibrate = optionVibrate;
   }

   /**
    * Write the options and other settings to RMS. Returns false if an
    * error occured.
    */
   public boolean saveSettings()
   {
      RecordStore settings = null;
      try
      {
         try
         {
            RecordStore.deleteRecordStore("Settings");
         }
         catch (RecordStoreNotFoundException rse)
         {
         }

         settings = RecordStore.openRecordStore("Settings", true);

         ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
         DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
         dataOutputStream.writeBoolean(optionAutoFire);
         dataOutputStream.writeBoolean(optionVibrate);
         dataOutputStream.writeBoolean(optionStarField);
         dataOutputStream.writeInt(leftKeyNum);
         dataOutputStream.writeInt(rightKeyNum);
         dataOutputStream.writeInt(fireKeyNum);

         dataOutputStream.flush();

         byte[] recordOut = byteOutputStream.toByteArray();
         try
         {
            settings.setRecord(1, recordOut, 0, recordOut.length);
         }
         catch (InvalidRecordIDException ir)
         {
            settings.addRecord(recordOut, 0, recordOut.length);
         }
         dataOutputStream.close();
         byteOutputStream.close();

         return true;
      }

      catch (IOException io)
      {
         System.out.println("IOException: " + io);
         return false;
      }
      catch (RecordStoreException rse)
      {
         System.out.println("RSException: " + rse);
         return false;
      }

      finally
      {
         try
         {
            if (settings != null) settings.closeRecordStore();
         }

         catch (RecordStoreNotOpenException e)
         {
         }

         catch (RecordStoreException e)
         {
         }
      }
   }


   /**
    * Read all the current user settings and option (if any) from rms.
    * Returns false if a problem occurred.
    */
   public boolean loadSettings()
   {
      RecordStore settings = null;
      try
      {
         settings = RecordStore.openRecordStore("Settings", true);

         ByteArrayInputStream byteInputStream = new ByteArrayInputStream(settings.getRecord(1));
         DataInputStream dataInputStream = new DataInputStream(byteInputStream);

         setOptionAutoFire(dataInputStream.readBoolean());
         setOptionVibrate(dataInputStream.readBoolean());
         setOptionStarField(dataInputStream.readBoolean());
         setLeftKeyNum(dataInputStream.readInt());
         setRightKeyNum(dataInputStream.readInt());
         setFireKeyNum(dataInputStream.readInt());

         dataInputStream.close();
         byteInputStream.close();

         settings.closeRecordStore();
         return true;
      }

      catch (IOException io)
      {
         System.out.println("IOException: " + io);
         return false;
      }
      catch (RecordStoreException rse)
      {
         System.out.println("RSException: " + rse);
         return false;
      }

      finally
      {
         try
         {
            if (settings != null) settings.closeRecordStore();
         }

         catch (RecordStoreNotOpenException e)
         {
         }

         catch (RecordStoreException e)
         {
         }
      }

   }

   public static final int getKeyCodeFromNum(int n)
   {
      switch (n)
      {
         case 0:
            return Canvas.KEY_NUM0;
         case 1:
            return Canvas.KEY_NUM1;
         case 2:
            return Canvas.KEY_NUM2;
         case 3:
            return Canvas.KEY_NUM3;
         case 4:
            return Canvas.KEY_NUM4;
         case 5:
            return Canvas.KEY_NUM5;
         case 6:
            return Canvas.KEY_NUM6;
         case 7:
            return Canvas.KEY_NUM7;
         case 8:
            return Canvas.KEY_NUM8;
         case 9:
            return Canvas.KEY_NUM9;
      }
      return Canvas.KEY_NUM0;
   }


   public int getFireKeyNum()
   {
      return fireKeyNum;
   }

   public void setFireKeyNum(int fireKeyNum)
   {
      this.fireKeyNum = fireKeyNum;
      if (gs != null) gs.setKeyBindings();
   }

   public int getLeftKeyNum()
   {
      return leftKeyNum;
   }

   public void setLeftKeyNum(int leftKeyNum)
   {
      this.leftKeyNum = leftKeyNum;
      if (gs != null) gs.setKeyBindings();
   }

   public int getRightKeyNum()
   {
      return rightKeyNum;
   }

   public void setRightKeyNum(int rightKeyNum)
   {
      this.rightKeyNum = rightKeyNum;
      if (gs != null) gs.setKeyBindings();
   }
}




⌨️ 快捷键说明

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