<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    3G 手機(jī)開發(fā)網(wǎng)

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

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

        GameCanvas是Canvas的子類,因此他同樣繼承了Canvas類的一些特性,比如showNotify()方法會(huì)在Canvas被顯示在屏幕的時(shí)候調(diào)用,而hideNotify()會(huì)在Canvas離開屏幕的時(shí)候被調(diào)用。我們可以把他們當(dāng)作{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;
        }

    在游戲開發(fā)中最重要的就是接受用戶觸發(fā)的事件然后重新繪制屏幕,通常我們使用getKeyStates()方法判斷哪個(gè)鍵被按下了,然后繪制屏幕,調(diào)用flushGraphics()。在GameCanvas中,系統(tǒng)事實(shí)上已經(jīng)為我們實(shí)現(xiàn)了雙緩沖技術(shù),因此每次我們繪制的時(shí)候就是在off- screen上繪制的。結(jié)束后通過flushGraphics把它復(fù)制到屏幕上去。下面是典型的接受事件、處理邏輯、繪制屏幕的代碼。
     // 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();
    }

        下面開始實(shí)現(xiàn)我們滾動(dòng)星空的效果,其實(shí)設(shè)計(jì)的思想非常簡(jiǎn)單。我們啟動(dòng)一個(gè)線程,使用copyArea()方法把屏幕的內(nèi)容往下復(fù)制一個(gè)像素的距離。然后繪畫第一個(gè)空白的直線,隨機(jī)的在直線上繪畫點(diǎn)兒,這樣看起來就像星空一樣了。邏輯代碼如下:
        // 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;
        }
    }






    爬蟲工作室 -- 專業(yè)的手機(jī)軟件開發(fā)工作室
    3G視線 -- 專注手機(jī)軟件開發(fā)
    posted on 2007-09-23 21:30 3G工作室 閱讀(1042) 評(píng)論(0)  編輯  收藏 所屬分類: j2me
    主站蜘蛛池模板: 亚洲第一黄片大全| 亚洲国产综合91精品麻豆| a级黄色毛片免费播放视频| 久久亚洲私人国产精品vA| 中文字幕亚洲码在线| 8x8x华人永久免费视频| 久久精品国产亚洲AV果冻传媒| 国产成人精品免费大全| 亚洲人成无码网WWW| 99久久99久久精品免费观看| 亚洲综合精品成人| 亚洲成AV人片在线观看WWW| 少妇性饥渴无码A区免费| 亚洲国产精品VA在线看黑人| 成人午夜免费福利| 国产午夜精品免费一区二区三区 | 2021免费日韩视频网| 羞羞的视频在线免费观看| 久久精品国产亚洲AV麻豆网站| 99在线观看免费视频| 亚洲欧洲日产韩国在线| 亚洲国产一区二区三区| 成**人免费一级毛片| 无码精品一区二区三区免费视频 | 亚洲资源最新版在线观看| 成人免费午夜视频| 国产情侣久久久久aⅴ免费| 国产精品日本亚洲777| 亚洲人成国产精品无码| 成年轻人网站色免费看| 日本免费一区二区三区 | 亚洲中文字幕伊人久久无码| 免费福利网站在线观看| 免费A级毛片无码A∨中文字幕下载| 亚洲小视频在线播放| 日韩亚洲人成在线综合日本| 亚洲av日韩片在线观看| 黄a大片av永久免费| 精品国产污污免费网站入口在线| 一区国严二区亚洲三区| 麻豆69堂免费视频|