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

📄 gamescreen.java

📁 《J2ME Game Programming》随书光盘源代码
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 * A GameScreen (Canvas with game logic) that creates five Ships then flies them
 * off in random directions. If a collision occurs the Ship will bounce off
 * before continuing.
 * @author Martin J. Wells
 */
public class GameScreen extends Canvas
{
   private Ship[] ships;

   /**
    * Constructor for the GameScreen that creates the five Ships.
    */
   public GameScreen()
   {
      // Create 5 ships positioning them in a vertical line down the center of
      // the screen.
      ships = new Ship[5];
      for (int i = 0; i < ships.length; i++)
         ships[i] = new Ship(getWidth()/2, 20*i, this);
   }

   /**
    * Canvas paint implementation which clears the screen and then draws
    * the ships at their current positions.
    * @param graphics The graphics context on which to draw.
    */
   protected void paint(Graphics graphics)
   {
      graphics.setColor(0);
      graphics.fillRect(0, 0, getWidth(), getHeight());

      // Cycles through the array of Ships and draws them all on the canvas.
      for (int i = 0; i < ships.length; i++)
         ships[i].render(graphics);
   }

   /**
    * Calls the cycle method on all the Ships in the array.
    */
   public void cycle()
   {
      for (int i = 0; i < ships.length; i++)
      {
         Ship s = ships[i];
         s.cycle();
      }
   }

   /**
    * Called by the Ship class to test whether a collision has occured between
    * one ship (the s paramater) and any other Ship in the array.
    * @param s The Ship to test against.
    * @return True if the Ship s is colliding with another.
    */
   public boolean checkCollision(Ship s)
   {
      // did this ship hit another?
      for (int j = 0; j < ships.length; j++)
      {
         Ship another = ships[j];
         // First we test that Ship s is not the element of the array being
         // tested.
         if (another != s &&
             Tools.isIntersectingRect(s.getX(), s.getY(), 16, 16,
                                      another.getX(), another.getY(), 16, 16))
            return true;
      }
      return false;
   }

}

⌨️ 快捷键说明

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