饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機開發網

             :: 首頁 :: 聯系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks
          http://www.3geye.net/bbs/thread-164-1-1.html

          MIDP2.0中提供了游戲開發專用的API,比如GameCanvas等類。他們位于javax.microedition.lcdui.game包內。本文介紹GameCanvas的基本使用方法并實現一種滾動星空的效果。您可以參考Game Canvas Basic獲得更詳細的信息。

              GameCanvas是Canvas的子類,因此他同樣繼承了Canvas類的一些特性,比如showNotify()方法會在Canvas被顯示在屏幕的時候調用,而hideNotify()會在Canvas離開屏幕的時候被調用。我們可以把他們當作{8}來使用,用于初始化和銷毀資源。比如
              // When the canvas is shown, start a thread to
              // run the game loop.

              protected void showNotify()
              {
                  random = new Random();
                  thread = new Thread(this);
                  thread.start();
              }
              // When the game canvas is hidden, stop the thread.

              protected void hideNotify()
              {
                  thread = null;
              }

          在游戲開發中最重要的就是接受用戶觸發的事件然后重新繪制屏幕,通常我們使用getKeyStates()方法判斷哪個鍵被按下了,然后繪制屏幕,調用flushGraphics()。在GameCanvas中,系統事實上已經為我們實現了雙緩沖技術,因此每次我們繪制的時候就是在off- screen上繪制的。結束后通過flushGraphics把它復制到屏幕上去。下面是典型的接受事件、處理邏輯、繪制屏幕的代碼。
           // Get the Graphics object for the off-screen buffer
          Graphics g = getGraphics();

          while (true) {
                // Check user input and update positions if necessary
                int keyState = getKeyStates();
                if ((keyState & LEFT_PRESSED) != 0) {
                    sprite.move(-1, 0);
                }
                else if ((keyState & RIGHT_PRESSED) != 0) {
                    sprite.move(1, 0);
                }

          // Clear the background to white
          g.setColor(0xFFFFFF);
          g.fillRect(0,0,getWidth(), getHeight());

                // Draw the Sprite
                sprite.paint(g);

                // Flush the off-screen buffer
                flushGraphics();
          }

              下面開始實現我們滾動星空的效果,其實設計的思想非常簡單。我們啟動一個線程,使用copyArea()方法把屏幕的內容往下復制一個像素的距離。然后繪畫第一個空白的直線,隨機的在直線上繪畫點兒,這樣看起來就像星空一樣了。邏輯代碼如下:
              // The game loop.

              public void run()
              {
                  int w = getWidth();
                  int h = getHeight() - 1;
                  while (thread == Thread.currentThread())
                  {
                      // Increment or decrement the scrolling interval
                      // based on key presses
                      int state = getKeyStates();

                      if ((state & DOWN_PRESSED) != 0)
                      {
                          sleepTime += SLEEP_INCREMENT;
                          if (sleepTime > SLEEP_MAX)
                              sleepTime = SLEEP_MAX;
                      } else if ((state & UP_PRESSED) != 0)
                      {
                          sleepTime -= SLEEP_INCREMENT;
                          if (sleepTime < 0)
                              sleepTime = 0;
                      }

                      // Repaint the screen by first scrolling the
                      // existing starfield down one and painting in
                      // new stars...

                      graphics.copyArea(0, 0, w, h, 0, 1, Graphics.TOP | Graphics.LEFT);
                      graphics.setColor(0, 0, 0);
                      graphics.drawLine(0, 0, w, 0);
                      graphics.setColor(255, 255, 255);
                      for (int i = 0; i < w; ++i)
                      {
                          int test = Math.abs(random.nextInt()) % 100;
                          if (test < 5)
                          {
                              graphics.drawLine(i, 0, i, 0);
                          }
                      }
                      flushGraphics();

                      // Now wait...

                      try
                      {
                          Thread.currentThread().sleep(sleepTime);
                      } catch (InterruptedException e)
                      {
                      }
                  }
              }

          下面給出源代碼
          /*
           * License
           *
           * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
           *
           */

          import javax.microedition.lcdui.*;
          import javax.microedition.lcdui.game.*;
          import javax.microedition.midlet.*;

          public class GameCanvasTest extends MIDlet implements CommandListener
          {

              private Display display;

              public static final Command exitCommand = new Command("Exit", Command.EXIT,
                      1);

              public GameCanvasTest()
              {
              }

              public void commandAction(Command c, Displayable d)
              {
                  if (c == exitCommand)
                  {
                      exitMIDlet();
                  }
              }

              protected void destroyApp(boolean unconditional)
                      throws MIDletStateChangeException
              {
                  exitMIDlet();
              }

              public void exitMIDlet()
              {
                  notifyDestroyed();
              }

              public Display getDisplay()
              {
                  return display;
              }

              protected void initMIDlet()
              {
                  GameCanvas c = new StarField();
                  c.addCommand(exitCommand);
                  c.setCommandListener(this);

                  getDisplay().setCurrent(c);
              }

              protected void pauseApp()
              {
              }

              protected void startApp() throws MIDletStateChangeException
              {
                  if (display == null)
                  {
                      display = Display.getDisplay(this);
                      initMIDlet();
                  }
              }
          }
          /*
           * License
           *
           * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
           */

          import java.util.Random;
          import javax.microedition.lcdui.*;
          import javax.microedition.lcdui.game.GameCanvas;

          // A simple example of a game canvas that displays
          // a scrolling star field. Use the UP and DOWN keys
          // to speed up or slow down the rate of scrolling.

          public class StarField extends GameCanvas implements Runnable
          {

              private static final int SLEEP_INCREMENT = 10;

              private static final int SLEEP_INITIAL = 150;

              private static final int SLEEP_MAX = 300;

              private Graphics graphics;

              private Random random;

              private int sleepTime = SLEEP_INITIAL;

              private volatile Thread thread;

              public StarField()
              {
                  super(true);

                  graphics = getGraphics();
                  graphics.setColor(0, 0, 0);
                  graphics.fillRect(0, 0, getWidth(), getHeight());
              }

              // The game loop.

              public void run()
              {
                  int w = getWidth();
                  int h = getHeight() - 1;
                  while (thread == Thread.currentThread())
                  {
                      // Increment or decrement the scrolling interval
                      // based on key presses
                      int state = getKeyStates();

                      if ((state & DOWN_PRESSED) != 0)
                      {
                          sleepTime += SLEEP_INCREMENT;
                          if (sleepTime > SLEEP_MAX)
                              sleepTime = SLEEP_MAX;
                      } else if ((state & UP_PRESSED) != 0)
                      {
                          sleepTime -= SLEEP_INCREMENT;
                          if (sleepTime < 0)
                              sleepTime = 0;
                      }

                      // Repaint the screen by first scrolling the
                      // existing starfield down one and painting in
                      // new stars...

                      graphics.copyArea(0, 0, w, h, 0, 1, Graphics.TOP | Graphics.LEFT);
                      graphics.setColor(0, 0, 0);
                      graphics.drawLine(0, 0, w, 0);
                      graphics.setColor(255, 255, 255);
                      for (int i = 0; i < w; ++i)
                      {
                          int test = Math.abs(random.nextInt()) % 100;
                          if (test < 5)
                          {
                              graphics.drawLine(i, 0, i, 0);
                          }
                      }
                      flushGraphics();

                      // Now wait...

                      try
                      {
                          Thread.sleep(sleepTime);
                      } catch (InterruptedException e)
                      {
                      }
                  }
              }

              // When the canvas is shown, start a thread to
              // run the game loop.

              protected void showNotify()
              {
                  random = new Random();
                  thread = new Thread(this);
                  thread.start();
              }
              // When the game canvas is hidden, stop the thread.

              protected void hideNotify()
              {
                  thread = null;
              }
          }






          爬蟲工作室 -- 專業的手機軟件開發工作室
          3G視線 -- 專注手機軟件開發
          posted on 2007-09-23 21:30 3G工作室 閱讀(1045) 評論(0)  編輯  收藏 所屬分類: j2me
          主站蜘蛛池模板: 南京市| 大冶市| 泸水县| 昌都县| 山阳县| 荃湾区| 江西省| 交口县| 盘锦市| 绥德县| 新兴县| 肥西县| 高邑县| 嘉祥县| 陇西县| 色达县| 平定县| 潼南县| 友谊县| 海口市| 如东县| 确山县| 平定县| 分宜县| 山西省| 新宁县| 读书| 盱眙县| 九江县| 兴国县| 临潭县| 襄城县| 武穴市| 镇安县| 神池县| 高雄市| 鄂尔多斯市| 张北县| 乃东县| 扶沟县| 崇明县|