服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

使用midp2.0开发游戏(1)gamecanvas基础

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在屏幕中心,可以用上下左右键移动它。

扫描关注微信公众号