MIDP2.0提供了對(duì)游戲的強(qiáng)有力支持,通過javax.microedition.lcdui.game包,原來在MIDP1.0中很多需要自己寫的功能現(xiàn)在都被當(dāng)作標(biāo)準(zhǔn)API實(shí)現(xiàn)了,包括GameCanvas,SPRite,Layer等等。
我們將使用MIDP2.0編寫一個(gè)坦克大戰(zhàn)的手機(jī)游戲,我也是初學(xué)J2ME不久,預(yù)備邊看書邊做,爭取把這個(gè)游戲做出來!J2ME高手請(qǐng)多指點(diǎn),和我一樣學(xué)習(xí)中的朋友歡迎多多交流!
我們的開發(fā)環(huán)境為Windows xp SP1 + J2DK1.4 + J2ME WTK2.1 + Eclipse 3.0 + EclipseMe,關(guān)于如何配置Eclipse的J2ME開發(fā)環(huán)境,請(qǐng)參考:
http://blog.csdn.net/mingjava/archive/2004/06/23/24022.aspx
下面是一個(gè)最簡單的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.TOPGraphics.LEFT);
flushGraphics();
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注