📄 ghostcanvas.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.io.*;
import java.util.*;
public class GhostCanvas extends GameCanvas implements Runnable{
static final int screenWidth=176,screenHeight=208;//屏幕宽、高
/**************************************************
*
*游戏状态
*
*************************************************/
int status=0; //主菜单被选中项0-百宝箱显示(包括公司LOGO) 1-主菜单 2-游戏中 3-重新开始 4-分数排行 5-游戏帮助 6-退出游戏
int g_status=0; //游戏状态
int p_status=0; //子状态
int s_status=0; //子状态下的状态
int sta=0;//下一个状态
int g_sta=0;
int p_sta=0;
int i_status=0;//装载状态
public static boolean playPause=false;
/****************************************************
*
*主面布相关
*
****************************************************/
//Download by http://www.codefans.net
boolean conti; //游戏运行标志
Random random=new Random();
public static svPoint svp=null;
boolean keyPre=false;//是否有键按下
/****************************************************
*
*游戏相关
*
***************************************************/
//--------------------主菜单-------------------------
public Image menuImg=null;
public String[] menuSelPng={"/res/m0.png","/res/m1.png","/res/m2.png","/res/m3.png","/res/m4.png"};
public Image[] menuSelImg=new Image[5];
//--------------------游戏中-------------------------
public Image panelImg=null;
static boolean g_pause=false; //游戏暂停标志
public static Man man=null;
public static Vector mons=null;
public static Prop prop=null;
public Image gameOverImg=null;
public static int gateTime=1000;//本关关卡时间
public String[] gateNumPng={"/res/n0.png","/res/n1.png","/res/n2.png","/res/n3.png","/res/n4.png","/res/n5.png","/res/n6.png","/res/n7.png","/res/n8.png","/res/n9.png"};
public Image[] gateNumImg=new Image[10];
public Image gateImg=null;
int t_gate=0;
int t_point=0;
int showWinTime=0;
Image winImg=null;
Image congImg=null;
//--------------------帮助文字-----------------------
String[] helpMe=null;
//===================================================
int i,j;
GhostCanvas() {
super(false);
setFullScreenMode(true);
init();
start();
}
public void start() {
Thread t=new Thread(this);
t.start();
}
public static int rate=200;//每帧播放时间为1/10秒
public static long startTime=0;//记录开始游戏经过的时间
/***************************************************************************
*
*主线程run方法,控制FPS
*
**************************************************************************/
public void run() {
conti=true;
long st=0;
long et=0;
Graphics g=getGraphics();
while(conti) {
if (!playPause){
st=System.currentTimeMillis();
startTime++;
control();
render(g);
//if (startTime%10==0) System.gc();
et=System.currentTimeMillis();
if (rate>(et-st)) {
try{
Thread.sleep(rate-(et-st));
}catch(Exception exp){}
}
}
}
unInit();
}
/************************************************************************
*
*根据当前状态时的按键调整游戏状态
*
************************************************************************/
private void control() {
int keystate=getKeyStates();
switch(status) {
case 0://百宝动画
status=1;
g_status=0;
break;
case 1://主菜单
switch(g_status){
case 0:
menuInit();
g_status=1;
p_status=0;
break;
case 1://菜单选择界面
if (v_GameCode==Canvas.UP && keyPre){
p_status--;
if (p_status<0){
p_status=4;
}
} else if (v_GameCode==Canvas.DOWN && keyPre){
p_status++;
if (p_status>4){
p_status=0;
}
}
if (v_GameCode==Canvas.FIRE && keyPre){
unMenu();
switch(p_status){
case 0://开始游戏
status=2;
g_status=0;
break;
case 1://重新开始
status=3;
break;
case 2://分数排行
status=4;
g_status=0;
break;
case 3://游戏帮助
status=5;
g_status=0;
break;
case 4://退出游戏
exit();
status=-1;
break;
}
}
break;
}
break;
case 2: //游戏画面
switch(g_status){
case 0://载入游戏
gameInit(10);
g_status=4;
p_status=0;
break;
case 1://载入本关
startGate();
g_pause=false;
if (i_status>10){
g_status=3;
i_status=0;
}
break;
case 3://游戏中
if(v_keyCode==-7 && keyPre) {
endGate();
unGame();
status=1;
g_status=0;
p_status=0;
} else if(v_keyCode==-6 && keyPre) {
g_pause=!g_pause;
} else{
int s_status=isEndGate();
if (s_status!=0){
if (s_status==1){//捉到全部鬼
if (t_gate==17){
//通关
man.point=man.point+(TollGate.curTollGate+1)*1000;//基本分
man.point=man.point+GhostCanvas.gateTime/1000*100;//提前完成的分数
t_point=Man.point;
t_gate=TollGate.curTollGate;
g_sta=6;
p_sta=0;
g_status=-3;
p_status=0;
} else{
//过关
man.point=man.point+(TollGate.curTollGate+1)*1000;//基本分
man.point=man.point+GhostCanvas.gateTime/1000*100;//提前完成的分数
TollGate.curTollGate++;
g_sta=4;
p_sta=0;
endGate();
g_status=-3;
p_status=0;
}
} else if(s_status==2 || s_status==3){
//时间到或被怪物碰到
t_point=Man.point;
t_gate=TollGate.curTollGate;
g_status=5;
p_status=0;
}
}
if(!g_pause){
if ((keystate & UP_PRESSED)!=0){
if (!man.startMove || man.curDir!=0){
man.changeDir(0);
} else{
man.Move(0);
}
} else if ((keystate & DOWN_PRESSED)!=0){
if (!man.startMove || man.curDir!=1){
man.changeDir(1);
} else{
man.Move(1);
}
} else if ((keystate & LEFT_PRESSED)!=0){
if (!man.startMove || man.curDir!=2){
man.changeDir(2);
} else{
man.Move(2);
}
} else if ((keystate & RIGHT_PRESSED)!=0){
if (!man.startMove || man.curDir!=3){
man.changeDir(3);
} else{
man.Move(3);
}
} else{
man.stopMove();
}
for (i=0;i<mons.size();i++) {
Monster ms=(Monster)mons.elementAt(i);
ms.move();
if (ms.curType==2){
ms.creatTime+=rate;
if (ms.creatTime>30000){
ms.creatTime=0;
Monster.creatMonster(ms);
}
}
}
gateTime-=rate;
if (man.showSos){
man.showVTime+=rate;
}
if (man.showVTime>1500){
man.showSos=false;
man.showVTime=0;
man.showVImg=null;
}
man.unUseProp();
}
}
break;
case -3://胜利
switch(p_status){
case 0:
showWinTime=0;
if (winImg==null){
try{
winImg=Image.createImage("/res/win.png");
}catch(Exception e){}
}
p_status=1;
break;
case 1:
showWinTime+=rate;
if (showWinTime>1500){
p_status=2;
}
break;
case 2:
winImg=null;
g_status=g_sta;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -