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

📄 gamescreen.java

📁 《J2ME Game Programming》随书光盘源代码
💻 JAVA
字号:
/**
 * The central game control class managing: rendering, cycling of world (which
 * includes actors) and other game state.
 */

import com.nokia.mid.ui.FullCanvas;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDletStateChangeException;

public class GameScreen extends FullCanvas implements Runnable
{
   private static GameScreen theGameScreen;	// "there can be only one..."

   private Raycaster theMidlet;

   private World world;

   // Rendering setup
   private Image osb;
   private Graphics osg;
   private Font defaultFont;
   private int defaultFontHeight;
   private int screenWidth;
   private int screenHeight;

   private Player player;

   private boolean running;						// thread controller

   public GameScreen(Raycaster midlet)
   {
      theGameScreen = this;
      theMidlet = midlet;
      running = true;

      screenWidth = getWidth();
      screenHeight = getHeight();

      initResources();

      // create the game thread
      Thread t = new Thread(this);
      t.start();
   }

   public final static GameScreen getGameScreen()
   {
      return theGameScreen;
   }

   private void initResources()
   {
      //System.out.println("1: freemem=" + Runtime.getRuntime().freeMemory());
      try
      {
         defaultFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
         defaultFontHeight = defaultFont.getHeight();
         player = new Player();
         player.init();
         world = new World(screenWidth, screenHeight, player);
      }
      catch (Exception e)
      {
         System.out.println("App exception: " + e);
         e.printStackTrace();
      }

   }

   private static final int MAX_CPS = 100;
   private static final int MS_PER_FRAME = 1000 / MAX_CPS;
   private long cycleStartTime;
   private long timeSinceStart;

   private static int cyclesThisSecond;
   private static int cps;
   private static long lastCPSTime;

   public void run()
   {
      try
      {
         while (running)
         {
            cycleStartTime = System.currentTimeMillis();
            world.cycle();
            repaint();

            timeSinceStart = System.currentTimeMillis() - cycleStartTime;

            // update the CPS
            if (System.currentTimeMillis() - lastCPSTime > 1000)
            {
               lastCPSTime = System.currentTimeMillis();
               cps = cyclesThisSecond;
               cyclesThisSecond = 0;
            }
            else
               cyclesThisSecond++;

            if (timeSinceStart < MS_PER_FRAME)
            {
               synchronized (this)
               {
                  wait(MS_PER_FRAME - timeSinceStart);
               }
            }
            else
               Thread.yield();
         }

         theMidlet.destroyApp(true);
      }

      catch (Exception e)
      {
         System.out.println("App exception: " + e);
         e.printStackTrace();
      }
   }

   protected void paint(Graphics graphics)
   {
      graphics.setColor(0);
      graphics.fillRect(0, 0, screenWidth, screenHeight);
      world.render(graphics);

      graphics.setColor(0x00ffcc66);
      graphics.setFont(defaultFont);
      graphics.drawString("" + cps, getWidth() - 30, getHeight() - defaultFontHeight, Tools.GRAPHICS_TOP_LEFT);
   }

   protected void keyPressed(int keyCode)
   {
      int action = getGameAction(keyCode);
      if (action == UP) player.setThrust(10);
      if (action == DOWN) player.setThrust(0);
      if (action == RIGHT) player.setSpin(-10);
      if (action == LEFT) player.setSpin(10);

      //#ifdef nokia
      if (keyCode == FullCanvas.KEY_SOFTKEY1 || keyCode == FullCanvas.KEY_SOFTKEY2)
      {
         try
         {
            theMidlet.destroyApp(true);
         }
         catch(MIDletStateChangeException me) { }
      }
      //#endif
   }

   protected void keyReleased(int keyCode)
   {
      int action = getGameAction(keyCode);
      if (action == RIGHT) player.setSpin(0);
      if (action == LEFT) player.setSpin(0);
      if (action == UP || action == DOWN)
      {
         player.setThrust(0);
         player.setVel(0);
      }

   }

}

⌨️ 快捷键说明

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