程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 使用MIDP2.0開發游戲(1)GameCanvas基礎

使用MIDP2.0開發游戲(1)GameCanvas基礎

編輯:J2ME
MIDP2.0開發游戲(1)GameCanvas基礎 作者:asklxf    文章來源:轉載    更新時間:2006-12-1 19:39:41 196

MIDP2.0提供了對游戲的強有力支持,通過Javax.microedition.lcdui.game包,原來在MIDP1.0中很多需要自己寫的功能現在都被當作標准API實現了,包括GameCanvas,Sprite,Layer等等。

我們將使用MIDP2.0編寫一個坦克大戰的手機游戲,我也是初學J2ME不久,准備邊看書邊做,爭取把這個游戲做出來!J2ME高手請多指點,和我一樣學習中的朋友歡迎多多交流!

我們的開發環境為Windows XP SP1 + J2DK1.4 + J2ME WTK2.1 + Eclipse 3.0 + EclipseMe,關於如何配置Eclipse的J2ME開發環境,請參考:

http://blog.csdn.Net/mingJava/archive/2004/06/23/24022.ASPx

下面是一個最簡單的GameCanvas的例子,出自《J2ME  & Gaming》:

// MyGameCanvas.Java
// 編寫Canvas類
import Javax.microedition.lcdui.*;
import Javax.microedition.lcdui.game.*;

public class MyGameCanvas extends GameCanvas implements Runnable {
    private boolean isPlay; // Game Loop runs when isPlay is true
    private long delay; // To give thread consistency
    private int currentX, currentY; // To hold current position of the 'X'
    private int width; // To hold screen width
    private int height; // To hold screen height

    // Constructor and initialization
    public MyGameCanvas() {
        super(true);
        width = getWidth();
        height = getHeight();
        currentX = width / 2;
        currentY = height / 2;
        delay = 20;
    }

    // Automatically start thread for game loop
    public void start() {
        isPlay = true;
        new Thread(this).start();
    }

    public void stop() { isPlay = false; }

    // Main Game Loop
    public void run() {
        Graphics g = getGraphics();
        while (isPlay) {
            input();
            drawScreen(g);
            try {
                Thread.sleep(delay);
            }
            catch (InterruptedException IE) {}
        }
    }

    // Method to Handle User Inputs
    private void input() {
        int keyStates = getKeyStates();
        // Left
        if ((keyStates & LEFT_PRESSED) != 0)
            currentX = Math.max(0, currentX - 1);
        // Right
        if ((keyStates & RIGHT_PRESSED) !=0 )
            if ( currentX + 5 < width)
                currentX = Math.min(width, currentX + 1);
        // Up
        if ((keyStates & UP_PRESSED) != 0)
            currentY = Math.max(0, currentY - 1);
        // Down
        if ((keyStates & DOWN_PRESSED) !=0)
            if ( currentY + 10 < height)
                currentY = Math.min(height, currentY + 1);
    }

    // Method to Display Graphics
    private void drawScreen(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);
        g.drawString("X",currentX,currentY,Graphics.TOP|Graphics.LEFT);
        flushGraphics();
    }
}

// GameMIDlet.Java
// 編寫MIDlet
import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;

public class GameMIDlet extends MIDlet {
    private Display display;
    public void startApp() {
        display = Display.getDisplay(this);
        MyGameCanvas gameCanvas = new MyGameCanvas();
        gameCanvas.start();
        display.setCurrent(gameCanvas);
    }

    public Display getDisplay() {
        return display;
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        exit();
    }

    public void exit() {
        System.gc();
        destroyApp(false);
        notifyDestroyed();
    }
}

編譯後就可以在模擬器中運行了,一個X在屏幕中心,可以用上下左右鍵移動它。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved