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

📄 gamescreen.java

📁 j2me游戏编程光盘源码
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import java.util.Vector;

/**
 * GameScreen class now modified to support a World class. The main
 * difference is the world takes care of cycling, rendering and
 * handling actors (so there's no more Actor vector required).
 */

public class GameScreen extends Canvas
{
   private Ship playerShip;
   private static GameScreen theGameScreen;   // the one and only
   private World world;

   /**
    * Constructs a new GameScreen which in turn constructs a new world 10 by
    * 10 tiles in size with a view port the size of the screen. A player ship
    * is then constructed and added to this world.
    */
   public GameScreen()
   {
      // Set a reference to ourselves (singleton).
      theGameScreen = this;

      // Construct a new world 10 by 10 tiles in size using a viewport the
      // size of the screen.
      world = new World(10, 10, getWidth(), getHeight());

      // Create the player ship and add it to the world.
      playerShip = new Ship(world, false, 20, 20);
      world.addActor(playerShip);
      world.setPlayerShip(playerShip);

      world.generateLevel(1);
   }

   /**
    * @return The player's Ship object.
    */
   public Ship getPlayerShip()
   {
      return playerShip;
   }

   /**
    * A static method to return the one and only instance of the GameScreen
    * object.
    */
   public final static GameScreen getGameScreen()
   {
      return theGameScreen;
   }

   /**
    * Canvas paint method used to render the world.
    * @param graphics The graphics context upon which to draw the Actors.
    */
   protected void paint(Graphics graphics)
   {
      graphics.setColor(0);
      graphics.fillRect(0, 0, getWidth(), getHeight());

      world.render(graphics);
   }

   /**
    * Called by the run method to set the viewport to center on the player's
    * ship and then cycle the world.
    */
   public void cycle()
   {
      world.setView(playerShip.getCenterX() - getWidth()/2,
                    playerShip.getCenterY() - getHeight()/2);
      world.cycle();
   }

   /**
    * React to keys pressed by the user.
    * @param keyCode The code of the key the players pressed.
    */
   protected void keyPressed(int keyCode)
   {
      int action = getGameAction(keyCode);

      if (action == RIGHT)
         playerShip.setNegPeakSpin();
      if (action == LEFT)
         playerShip.setPeakSpin();
      if (action == UP)
         playerShip.setFiring(true);
   }

   /**
    * React to key being released. For this example the code stops the spin.
    * @param keyCode The code for the key that was released.
    */
   protected void keyReleased(int keyCode)
   {
		int action = getGameAction(keyCode);
		if (action == RIGHT)
			playerShip.setSpin(0);
		if (action == LEFT)
			playerShip.setSpin(0);
      if (action == UP)
         playerShip.setFiring(false);
   }
}



⌨️ 快捷键说明

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