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

📄 gamescreen.java

📁 大量j2me源代码
💻 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 TankAttack theMidlet;

   private World world;
   private Tank playerTank;

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

   private boolean running;						// thread controller

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

      screenWidth = getWidth();
      screenHeight = getHeight();
      halfScreenWidth = screenWidth / 2;
      halfScreenHeight = screenHeight / 2;

      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
      {
         Tank.setup();
         defaultFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
         defaultFontHeight = defaultFont.getHeight();

         world = new World(screenWidth, screenHeight);
         playerTank = new Tank(world);
         playerTank.init(100, 100);
         world.setPlayerTank(playerTank);

         // add an enemy tank
         Tank t = new Tank(world);
         t.init(150, 120);
      }
      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;

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

            timeSinceStart = System.currentTimeMillis() - cycleStartTime;

            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.setView(playerTank.getCenterX() - halfScreenWidth, playerTank.getCenterY() - halfScreenHeight);
      world.render(graphics);
   }

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

      //#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) playerTank.setSpin(0);
      if (action == LEFT)  playerTank.setSpin(0);
   }

}

⌨️ 快捷键说明

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