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

📄 gamescreen.java

📁 一个j2me的游戏的实现.需要添加一个mathfp类来支持浮点数.
💻 JAVA
字号:
import net.jscience.math.kvm.MathFP;

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;

   // Used to pan the view position based on which way the player is facing.
   private int currentViewPosX;
   private int currentViewPosY;
   private int pixelsPerMSFP = MathFP.div(25, 1000);
   private int panPixelsToMoveFP=0;

   private long msSinceLastCycle;
   private long lastCycleTime;

   /**
    * 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);

      lastCycleTime = System.currentTimeMillis();

      // Focus the view on the player's ship.
	   currentViewPosX = playerShip.getX() - 50;
      currentViewPosY = playerShip.getY() - 50;
   }

   /**
    * @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()
   {
      msSinceLastCycle = System.currentTimeMillis() - lastCycleTime;

      // Update the panning view
      panPixelsToMoveFP += MathFP.mul(pixelsPerMSFP,
                                 MathFP.toFP((int)msSinceLastCycle));
      // Figure out how many whole pixels to move.
      int wholePixels = MathFP.toInt(panPixelsToMoveFP);

      if (wholePixels > 0)
      {
         // Calculate the ideal position for the view based on the
         // direction the player's ship is facing.
         int[] targetViewPos = Actor.getProjectedPos(
                 playerShip.getX(), playerShip.getY(),
                 playerShip.getDirection(), getWidth()/3);

         // Adjust the current move slightly towards the ideal view
         // point.
         if (currentViewPosX < targetViewPos[0]) currentViewPosX += wholePixels;
         if (currentViewPosX > targetViewPos[0]) currentViewPosX -= wholePixels;
         if (currentViewPosY < targetViewPos[1]) currentViewPosY += wholePixels;
         if (currentViewPosY > targetViewPos[1]) currentViewPosY -= wholePixels;

         // Take away the pixels that were moved.
         panPixelsToMoveFP = MathFP.sub(panPixelsToMoveFP,
                                        MathFP.toFP(wholePixels));

         world.setView(currentViewPosX-(getWidth()/2),
                       currentViewPosY-(getHeight()/2));
      }

      world.cycle();
      lastCycleTime = System.currentTimeMillis();
   }

   /**
    * 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 + -