J2me手机游戏回合RPG游戏基本构架
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class GameMID extends MIDlet{
/** MyRPGMIDlet对象,用于实现退出功能 */
static GameMID gmaemid;
myCanvas mc;
public GameMID() {
gmaemid=this;
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Displayable current = Display.getDisplay(this).getCurrent();
if(current == null)
{
mc = new myCanvas();
Display.getDisplay(this).setCurrent(mc);
}else{
Display.getDisplay(this).setCurrent(current);
}
}
public static void gameExit() {
gmaemid.notifyDestroyed();
try {
gmaemid.destroyApp(true);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
gmaemid = null;
}
}
然后是Canvas类:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
public class myCanvas extends Canvas implements Runnable, CommandListener {
/** 游戏是否处于运行状态,true代表处于运行状态 */
private boolean isRunning = true;
/** 游戏是否处于暂停状态,true代表处于暂停状态 */
private boolean isPaused = false;
private byte Gamestates;
private byte fps = 20;
/** 构造函数 */
public myCanvas() {
dataname = "FD"; // 存储仓库的名字
init(); // 初始化角色
// 缓冲屏幕 画笔
offScreen = Image.createImage(width, height);
gp = offScreen.getGraphics();
// 启动线程
gameThread = new Thread(this);
gameThread.start();
}
/** 对游戏角色进行初始化 */
private void init() {
// 加载角色图片
if (isFirst) {
// 初始化角色的物品 装备
} else {
// 读取所要读取的存档
}
}
/** 游戏开始或继续游戏 */
public void startGame() {
isPaused = false;
}
/** 游戏暂停 */
public void pauseGame() {
isPaused = true;
}
/**
* 画布界面
*/
public void render(Graphics g) {
switch (Gamestates) {
}
protected void paint(Graphics g) {
g.drawImage(offScreen, 0, 0, 0);
serviceRepaints();
}
public void run() {
try {// 尝试获得记录
rs = RecordStore.openRecordStore(dataname, false); // 如果不成功则不创建
try {// 成功后则尝试获取记录
actorData = rs.getRecord(1);
getRocod(actorData, actor);// 将记录转化为实际数据
isData1=true;
} catch (Exception e) {
System.out.println("没有获得记录" + e);
}
} catch (RecordStoreNotFoundException e) {// 如果没有成功获得
try {// 关闭记录
isData1=false;
System.out.println("没找到记录");
rs.closeRecordStore();
rs = null;
} catch (Exception ex) {
}
} catch (Exception e) {
}
while (isRunning) {
long startTime = System.currentTimeMillis();
update();// 游戏逻辑
render(gp); // 游戏绘制
long timeTaken = System.currentTimeMillis() - startTime;
if (timeTaken < 1000 / fps) {
try {
gameThread.sleep(1000 / fps - timeTaken);
} catch (Exception ex) {
}
}
}
}
/**
* 游戏逻辑
*/
private void update() {
switch (Gamestates) {
}
}
/** keyPressed按键事件 */
protected void keyPressed(int _keyCode) {
int keyCode = this.getGameAction(_keyCode);
switch (Gamestates) {
}
}
/** keyReleased按键事件 */
protected void keyReleased(int _keyCode) {
int keyCode = this.getGameAction(_keyCode);
switch (Gamestates) {
}
}
// 进行刷怪处理
private class NpcFlush extends TimerTask {
private Timer timer;
public NpcFlush(Timer timer) {
this.timer = timer;
}
public void run() {
time();
// timer.cancel();
}
}
public void start() {
NpcFlush nf = new NpcFlush(timer);
timer.schedule(nf, time * 1000);
}
}
}
可以看出:1.0下的游戏结构非常简单,基本只需要两个类便可以完成一个游戏,一个是程序入口的MID类一个是游戏绘制和显示的画布类,而又分按键、逻辑、绘制三个大的主块进行程序的书写,根据不同的标志量来进行相应的程序书写既可。