利用Java 开发的一个手机版的潜艇游戏。游戏实现的重点就是屏幕的绘制滚动技术和碰撞检测技术
源代码在线查看: controller.java~9~
/*
* Author: Huang ye(www.hyweb.net)
* 代码开源, 引用请注明出处
*
* 创建日期 2005-3-9
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package mysubmarine;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author user
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Controller {
private MysubmarineMIDlet subMIDlet;
private MySubmarineCanvas subCanvas;
private MenuUI menuUI;
private Display display;
public static final byte EVENT_START = 01;
public static final byte EVENT_ABOUT = 02;
public static final byte EVENT_EXIT = 03;
public static final byte EVENT_MENU = 101;
public static final byte EVENT_MENU_GAMEOVER = 102;
public static final byte EVENT_NEXTROUND = 103;
public Controller(MysubmarineMIDlet subMIDlet) {
this.subMIDlet = subMIDlet;
display = Display.getDisplay(subMIDlet);
//menuUI = new MenuUI(this);
this.EventHandler(EVENT_MENU);
}
public void EventHandler(byte eventID) {
switch (eventID) {
case EVENT_START: {
//开始游戏
if (subCanvas == null) {
subCanvas = new MySubmarineCanvas(this);
} else {
}
display.setCurrent(subCanvas);
break;
}
case EVENT_ABOUT: {
//说明文件
Alert alert = new Alert("About", "This is a Demo Edition Game .", null,
AlertType.INFO);
alert.setTimeout(3000);
display.setCurrent(alert, menuUI);
break;
}
case EVENT_NEXTROUND: {
//开启一个新的线程,显示下一轮字样
//原先画布线程则更换地图信息
Thread t = new Thread() {
public void run() {
StringBuffer sbf = new StringBuffer("Round " +
(MySubmarineCanvas.nLevel));
display.setCurrent(new InfoCanvas(sbf) {});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
display.setCurrent(subCanvas);
subCanvas.active();
}
};
t.start();
break;
}
case EVENT_EXIT: {
//退出程序
subMIDlet.destroyApp(false);
subMIDlet.notifyDestroyed();
break;
}
case EVENT_MENU: {
display.setCurrent(menuUI);
break;
}
case EVENT_MENU_GAMEOVER: {
StringBuffer sbf = new StringBuffer("Game Over");
display.setCurrent(new InfoCanvas(sbf) {});
subCanvas = null;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
display.setCurrent(menuUI);
break;
}
default:
break;
}
}
}