文章来源:J2ME开发网
MIDP2.0中推出了Game开发包,为开发者提供了游戏开发的便利。javax.microedition.lcdui.game包内总共有五各类,分别是GameCanvas、Layer、Sprite、TiledLayer和LayerManager。其中Sprite和TiledLayer是Layer的子类,作用是构建游戏中的两个重要的元素,Sprite代表游戏中运动的主题,比如动作游戏中的坦克,而TiledLayer主要是为游戏提供背景。LayerManager方便对游戏中的各个层进行管理。GameCanvas是继承了Canvas类的,针对游戏开发特别定制的。如果使用Canvas类开发游戏的时候代码的结构通常如下,
public class MicroTankCanvas extends Canvas
implements Runnable
{
public void run()
{
while (true)
{
// Update the game state.
repaint();
// Delay one time step.
}
}
public void paint(Graphics g)
{
// Painting code goes here.
}
protected void keyPressed(int keyCode)
{
// Respond to key presses here.
}
}
这样的结构存在一些问题,事件处理、游戏绘制的动作放在不同的线程内处理,对游戏可能产生不可预料的影响。GameCanvas的出现很好的解决了这个问题,你可以在应用程序的线程中获得Graphics对象,这时候系统会在off-screen的缓冲区内进行绘制,当你调用flushGraphics()的时候会马上绘制到手机屏幕上去。只有当屏幕被更新后者个方法才会返回,而调用repaint()的话,方法马上就返回你就不知道什么时候paint()才被调用。另外GameCanvas通过getKeyStates()方法来判断用户的输入事件。这样你就不用等待系统调用keyPressed()方法了,getKeyStates()可以马上得到现在按键的状态。
如果我们采用GameCanvas做游戏的时候,通常游戏的结构如下:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class SimpleGameCanvas extends GameCanvas implements Runnable
{
private volatile boolean mTrucking;
private long mFrameDelay;
private int mX, mY;
private int mState;
public SimpleGameCanvas()
{
super(true);
mX = getWidth() / 2;
mY = getHeight() / 2;
mState = 0;
mFrameDelay = 20;
}
public void start()
{
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void stop()
{
mTrucking = false;
}
public void run()
{
Graphics g = getGraphics();
while (mTrucking == true)
{
tick();
input();
render(g);
try
{
Thread.sleep(mFrameDelay);//这里我们也可以通过定义每桢的时间来处理,更合理些。
} catch (InterruptedException ie)
{
stop();
}
}
}
private void tick()
{
mState = (mState + 1) % 20;
}
private void input()
{
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
mX = Math.max(0, mX - 1);
if ((keyStates & RIGHT_PRESSED) != 0)
mX = Math.min(getWidth(), mX + 1);
if ((keyStates & UP_PRESSED) != 0)
mY = Math.max(0, mY - 1);
if ((keyStates & DOWN_PRESSED) != 0)
mY = Math.min(getHeight(), mY + 1);
}
private void render(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0000ff);
g.drawLine(mX, mY, mX - 10 + mState, mY - 10);
g.drawLine(mX, mY, mX + 10, mY - 10 + mState);
g.drawLine(mX, mY, mX + 10 - mState, mY + 10);
g.drawLine(mX, mY, mX - 10, mY + 10 - mState);
flushGraphics();
}
}
mport javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class SimpleGameMIDlet
extends MIDlet
implements CommandListener {
private Display mDisplay;
private SimpleGameCanvas mCanvas;
private Command mExitCommand;
public void startApp() {
if (mCanvas == null) {
mCanvas = new SimpleGameCanvas();
mCanvas.start();
mExitCommand = new Command("Exit", Command.EXIT, 0);
mCanvas.addCommand(mExitCommand);
mCanvas.setCommandListener(this);
}
mDisplay = Display.getDisplay(this);
mDisplay.setCurrent(mCanvas);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {
mCanvas.stop();
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
destroyApp(true);
notifyDestroyed();
}
}
}
在游戏开发中我们通常都要设置背景,TiledLayer就是为了解决这个问题而出现的。TiledLayer可以把一个整图分割成若干个你指定尺寸的小图,而你按照小图的编号来安排你的背景样子。例如下面的图片的大小为64*48
通过下面的代码我们可以创建一个这样的Tile
Image image = Image.createImage("/board.png");
TiledLayer tiledLayer = new TiledLayer(10, 10, image, 16, 16);
现在我们就可以按照我们的需要来布局背景了,通过方法setCell()。例如
int[] map = { 1, 1, 1, 1, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 11, 0, 0, 0,
0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0 };
for (int i = 0; i < map.length; i++)
{
int column = i % 10;
int row = (i - column) / 10;
tiledLayer.setCell(column, row, map[i]);
}
这样我们就可以得到如下所示的背景了。
TiledLayer使用比较简单,而且还可以支持动画。下面看看另一个Layer的子类Sprite。和TiledLayer不同,它是用来构建游戏主体元素的,比如游戏中的坦克,它的用法同样很简单,但是有几个概念比较重要,他们是视窗、参考点和翻转。读者请参考MIDP API DOC来解决这些问题。使用Sprite非常简单,通过构造器我们就可以得到Sprite了,系统会把给定图片分成若干个小图片并排列成桢序列,我们可以通过Sprite提供的方法方便的调用。在Sprite中还提供了碰撞检测的函数,你可以选择像素级别的检测或者矩形边框界别的检测。通常后者比较简单,但是粗糙一些。前者精确但是速度慢。
下面通过一个简单的游戏例子来介绍如何使用这些类来开发J2ME 2D游戏,您可以参考SUN的文章Creating 2D Actions Games with the game API。
在这个程序中主要有两个对象,一个是坦克一个是背景,我们分别采用Sprite和TiledLayer类来构建。通常我们在Sprite派生出来的类中定义好动作,这样我们在GameCanvas里面可以很容易接受用户的输入事件然后处理了。
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MicroTankSprite extends Sprite
{
private int mDirection;
private int mKX, mKY;
private int mLastDelta;
private boolean mLastWasTurn;
private static final int[] kTransformLookup = { Sprite.TRANS_NONE,
Sprite.TRANS_NONE, Sprite.TRANS_NONE, Sprite.TRANS_MIRROR_ROT90,
Sprite.TRANS_ROT90, Sprite.TRANS_ROT90, Sprite.TRANS_ROT90,
Sprite.TRANS_MIRROR_ROT180, Sprite.TRANS_ROT180,
Sprite.TRANS_ROT180, Sprite.TRANS_ROT180,
Sprite.TRANS_MIRROR_ROT270, Sprite.TRANS_ROT270,
Sprite.TRANS_ROT270, Sprite.TRANS_ROT270, Sprite.TRANS_MIRROR };
private static final int[] kFrameLookup = { 0, 1, 2, 1, 0, 1, 2, 1, 0, 1,
2, 1, 0, 1, 2, 1 };
private static final int[] kCosLookup = { 0, 383, 707, 924, 1000, 924, 707,
383, 0, -383, -707, -924, -1000, -924, -707, -383 };
private static final int[] kSinLookup = { 1000, 924, 707, 383, 0, -383,
-707, -924, -1000, -924, -707, -383, 0, 383, 707, 924 };
public MicroTankSprite(Image image, int frameWidth, int frameHeight)
{
super(image, frameWidth, frameHeight);
defineReferencePixel(frameWidth / 2, frameHeight / 2);
mDirection = 0;
}
public void turn(int delta)
{
mDirection += delta;
if (mDirection < 0)
mDirection += 16;
if (mDirection > 15)
mDirection %= 16;
setFrame(kFrameLookup[mDirection]);
setTransform(kTransformLookup[mDirection]);
mLastDelta = delta;
mLastWasTurn = true;
}
public void forward(int delta)
{
fineMove(kCosLookup[mDirection] * delta, -kSinLookup[mDirection]
* delta);
mLastDelta = delta;
mLastWasTurn = false;
}
public void undo()
{
if (mLastWasTurn)
turn(-mLastDelta);
else
forward(-mLastDelta);
}
private void fineMove(int kx, int ky)
{
// First initialize mKX and mKY if they're
// not close enough to the actual x and y.
int x = getX();
int y = getY();
int errorX = Math.abs(mKX - x * 1000);
int errorY = Math.abs(mKY - y * 1000);
if (errorX > 1000 || errorY > 1000)
{
mKX = x * 1000;
mKY = y * 1000;
}
// Now add the deltas.
mKX += kx;
mKY += ky;
// Set the actual position.
setPosition(mKX / 1000, mKY / 1000);
}
}
在GameCanvas中我们的程序流程如下
public void run()
{
Graphics g = getGraphics();
int timeStep = 80;
while (mTrucking)
{
long start = System.currentTimeMillis();
tick();
input();
render(g);
long end = System.currentTimeMillis();
int duration = (int) (end - start);
if (duration < timeStep)
{
try
{
Thread.sleep(timeStep - duration);
} catch (InterruptedException ie)
{
stop();
}
}
}
}
基本的思路就是接受用户事件,重新绘制屏幕。
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MicroTankCanvas extends GameCanvas implements Runnable
{
private volatile boolean mTrucking;
private MicroTankSprite mTank;
private TiledLayer mBoard;
private LayerManager mLayerManager;
public MicroTankCanvas() throws IOException
{
super(true);
mTank = createTank();
mTank.setPosition(0, 24);
mBoard = createBoard();
mLayerManager = new LayerManager();
mLayerManager.append(mTank);
mLayerManager.append(mBoard);
}
private MicroTankSprite createTank() throws IOException
{
Image image = Image.createImage("/tank.png");
return new MicroTankSprite(image, 32, 32);
}
private TiledLayer createBoard() throws IOException
{
Image image = Image.createImage("/board.png");
TiledLayer tiledLayer = new TiledLayer(10, 10, image, 16, 16);
int[] map = { 1, 1, 1, 1, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 11, 0, 0, 0,
0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 0, 0, 7, 6, 0, 0, 0 };
for (int i = 0; i < map.length; i++)
{
int column = i % 10;
int row = (i - column) / 10;
tiledLayer.setCell(column, row, map[i]);
}
return tiledLayer;
}
public void start()
{
mTrucking = true;
Thread t = new Thread(this);
t.start();
}
public void run()
{
Graphics g = getGraphics();
int timeStep = 80;
while (mTrucking)
{
long start = System.currentTimeMillis();
tick();
input();
render(g);
long end = System.currentTimeMillis();
int duration = (int) (end - start);
if (duration < timeStep)
{
try
{
Thread.sleep(timeStep - duration);
} catch (InterruptedException ie)
{
stop();
}
}
}
}
private void tick()
{
if (mTank.collidesWith(mBoard, true))
mTank.undo();
}
private void input()
{
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
mTank.turn(-1);
else if ((keyStates & RIGHT_PRESSED) != 0)
mTank.turn(1);
else if ((keyStates & UP_PRESSED) != 0)
mTank.forward(2);
else if ((keyStates & DOWN_PRESSED) != 0)
mTank.forward(-2);
}
private void render(Graphics g)
{
int w = getWidth();
int h = getHeight();
g.setColor(0xffffff);
g.fillRect(0, 0, w, h);
int x = (w - 160) / 2;
int y = (h - 160) / 2;
mLayerManager.paint(g, x, y);
g.setColor(0x000000);
g.drawRect(x, y, 160, 160);
flushGraphics();
}
public void stop()
{
mTrucking = false;
}
}