疯狂拼图完整源代码

CrazyPuzzle的完整源代码,整个架构已经基本打好,但因为某些原因,部分功能尚未实现,在此一并贴出来.如果各位朋友有兴趣,可自行完善相应功能.

// ------------------------------
// MyGameCanvas.java
// ------------------------------

package src;

import javax.microedition.lcdui.game. * ;
import javax.microedition.lcdui. * ;
import javax.microedition.media. * ; // 西门子C65不支持,故无法以此方法播放声音,在此不插入
import java.util. * ;
import java.io. * ;

public class MyGameCanvas extends GameCanvas implements CommandListener
... {

/***//**代表标题显示状态的常量*/
privatestaticfinalshortSTAGE_TITLE=0;
/***//**代表关卡开始显示状态的常量*/
privatestaticfinalshortSTAGE_START=1;
/***//**代表关卡显示状态的常量*/
privatestaticfinalshortSTAGE_PLAYING=2;
/***//**代表游戏中显示完整图案的常量*/
privatestaticfinalshortSTAGE_PLAYING_SEE_PERFECT_PIC=3;
/***//**代表关卡结束显示状态的常数*/
privatestaticfinalshortSTAGE_OVER=5;
/***//**代表过关显示状态的常量*/
privatestaticfinalshortSTAGE_CLEAR=7;
/***//**代表全关卡过关显示状态的常量*/
privatestaticfinalshortALL_STAGE_CLEAR=9;
/***//**代表游戏结束的常量*/
privatestaticfinalshortGAME_END=10;
/***//**最大关卡数*/
privatestaticfinalshortMAX_STAGE=5;
/***//**正常模式常量*/
publicstaticfinalshortMODE_NORMAL=1;
/***//**极限模式常量*/
publicstaticfinalshortMODE_UTMOST=2;

/***//**储存现在之显示状态的变量*/
privateshortgameState;

/***//**储存游戏模式的变量*/
privateshortgameMode;

/***//**储存目前的关卡*/
privateshortstageNum;

/***//**储存已过关的关卡*/
privateshortstagePassedNum;

/***//**储存剩余的时间*/
privatelonglastTime;

/***//**储存损耗的时间,普通模式用到*/
privatelongusedTime;

/***//**储存单关通过的最短时间*/
privatelongquicklyPassOneStageTime;

/***//**游戏过程暂停状态所用的标志*/
privatebooleanisGamePaused;

/***//**储存显示的对象*/
privateDisplaydisplay;

/***//**储存音乐的对象*/
privatePlayerplayer;//西门子C65不支持

/***//**储存主类的对象,方便进行退出之类的操作*/
privateCrazyPuzzleMIDletmainMIDlet;

/***//**层管理*/
privateLayerManagerlm;

/***//**背景*/
privateTiledLayerpuzzlePic;

/***//**建一个画图对象*/
privateGraphicsg;

/***//**时间倒计时,类为继承*/
privateTimeCounterTasktimeCounter;

/***//**储存各关卡谜题资讯的变量*/
privatestaticfinalshort[][]PUZZLE_MAP=newshort[][]...{
...{//关卡1的谜题
1,5,9,13,
2,6,10,14,
3,7,11,15,
4,8,12,0
}
,
...{//关卡2的谜题
4,3,2,1,
8,7,6,5,
12,11,10,9,
15,13,14,0
}
,
...{//关卡3的谜题
12,8,4,1,
15,9,5,2,
13,10,6,3,
14,11,7,0
}
,
...{//关卡4的谜题
14,15,12,13,
8,9,10,11,
4,5,6,7,
1,2,3,0
}
,
...{//关卡5的谜题
14,12,15,13,
11,10,9,8,
7,6,5,4,
3,2,1,0
}

}
;

//norains:*构造函数*
publicMyGameCanvas(Displaydisp,CrazyPuzzleMIDletm,shortmode)
...{
super(false);
this.display=disp;
this.mainMIDlet=m;
this.gameMode=mode;
stageNum
=1;
stagePassedNum
=0;
lastTime
=0;
usedTime
=0;
quicklyPassOneStageTime
=0;
isGamePaused
=false;
g
=this.getGraphics();
gameState
=STAGE_TITLE;
this.setFullScreenMode(true);//全屏幕
//增加命令
//this.addCommand(newCommand("重新开始",Command.OK,1));
this.addCommand(newCommand("取消",Command.BACK,1));
this.addCommand(newCommand("返回列表",Command.BACK,1));
this.addCommand(newCommand("退出游戏",Command.EXIT,1));
this.setCommandListener(this);
render();
}


publicvoidcommandAction(Commandcommand,Displayablearg1)
...{
//TODOAuto-generatedmethodstub
Stringcmd=command.getLabel();
if(cmd.equals("重新开始"))
...{
//不知为何,下面这段代码无法正常关闭音乐流,无论在模拟器还是在实体机上
//造成的后果是两首音乐一起播放,以致于系统变慢,无法正常游戏
//估计产生原因是上一首歌还没退出来,下一首就播放了
//关闭本节声音播放,以方便下一关播放新音乐
gameState=STAGE_TITLE;
try
...{
player.stop();
}

catch(Exceptionexp)
...{}



render();

}

elseif(cmd.equals("返回列表"))
...{
if(player!=null)
...{
//关闭本节声音播放
try
...{
player.stop();
}

catch(Exceptionexp)
...{}

}

ListChoiceScreenlcs
=newListChoiceScreen(display,mainMIDlet);
display.setCurrent(lcs);
}

elseif(cmd.equals("退出游戏"))
...{
mainMIDlet.notifyDestroyed();
}

elseif(cmd.equals("取消"))
...{
//什么都不做,返回原画面
}

}


//*主要是用来绘图
publicvoidrender()
...{
switch(gameState)
...{
caseSTAGE_TITLE:
doTitlePaint();
break;
caseSTAGE_START:
doStartPaint();
break;
caseSTAGE_PLAYING:
doPlayingPaint();
break;
caseSTAGE_PLAYING_SEE_PERFECT_PIC:
doPlayingSeePerfectPicPaint();
break;
caseSTAGE_OVER:
doStageOverPaint();
break;
caseSTAGE_CLEAR:
doStageClearPaint();
break;
caseALL_STAGE_CLEAR:
doStageAllClearPaint();
break;
caseGAME_END:
doGameEndPaint();
break;
}


}


//*画出关卡画面*
privatevoiddoTitlePaint()
...{
intdispWidth=this.getWidth();
intdispHeight=this.getHeight();
g.setColor(
255,255,255);
g.fillRect(
0,0,getWidth(),getHeight());
Imageimg1
=creatImage("/res/ready.png");
g.drawImage(img1,(dispWidth
-img1.getWidth())/2,(dispHeight-img1.getHeight())/2-4,0);
Imageimg2
=creatImage("/res/num"+stageNum+".png");
g.drawImage(img2,(dispWidth
-img2.getWidth())/2,(dispHeight-img1.getHeight())/2+img1.getHeight()-1,0);
this.flushGraphics();
}


//*关卡开始前
booleanfirstStart=true;//是否首次运行标志
shortmusicNum=1;//因为音乐不够,以此变量做两首歌更换标志
privatevoiddoStartPaint()
...{
//*关卡初始化
lm=newLayerManager();
puzzlePic
=creatPuzzlePic();
//*测试用
//puzzlePic=creatTestPuzzlePic();//测试用
lm.append(puzzlePic);


//播放声音
//西门子模拟器不支持下面这段代码,但实体机子完美支持
try
...{
InputStreamin;
//in=getClass().getResourceAsStream("/res/music"+stageNum+".mid");这是有5首音乐的时候使用的
//只有两首歌的时候使用
if(musicNum==1)
...{
in
=getClass().getResourceAsStream("/res/music1.mid");
musicNum
=2;
}

else
...{
in
=getClass().getResourceAsStream("/res/music2.mid");
musicNum
=1;
}


player
=Manager.createPlayer(in,"audio/midi");
player.setLoopCount(
10);
player.start();
}

catch(IOExceptione)...{}
catch(MediaExceptione)...{}



gameState
=STAGE_PLAYING;
//创建并启动计时器
if(firstStart==true||gameMode==MODE_NORMAL)
...{
if(gameMode==MODE_NORMAL)
...{
timeCounter
=newTimeCounterTask(300);
}

elseif(gameMode==MODE_UTMOST)//此段代码只在第一次时运行
...{
timeCounter
=newTimeCounterTask(900);
}

Timertimer
=newTimer();
timer.schedule(timeCounter,
1000,1000);
firstStart
=false;
}

else
...{
timeCounter
=newTimeCounterTask(lastTime);
Timertimer
=newTimer();
timer.schedule(timeCounter,
1000,1000);
firstStart
=false;
System.out.println(
"first");
}

//
render();
}


//*游戏进行中
privatevoiddoPlayingPaint()
...{
//*设置计时器的字体
Fontfont;
font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
g.setColor(
1,16,57);
g.fillRect(
0,0,this.getWidth(),this.getHeight());
lm.setViewWindow(
0,0,this.getWidth(),this.getHeight());
lm.paint(g,(
this.getWidth()-112)/2,font.getHeight()+2);
//*画格子
g.setColor(255,255,255);
intx1,y1,x2,y2;
//*横线
y1=y2=font.getHeight()+2;
x1
=(this.getWidth()-112)/2;
x2
=((this.getWidth()-112)/2)+112;
for(inti=0;i<5;i++)
...{
g.drawLine(x1,y1,x2,y2);
y1
=y2=y2+28;
}


//*直线
x1=x2=(this.getWidth()-112)/2;
y1
=font.getHeight()+2;
y2
=font.getHeight()+2+112;
for(inti=0;i<5;i++)
...{
g.drawLine(x1,y1,x2,y2);
x1
=x2=x2+28;
}

g.setColor(
255,255,255);
g.drawString(timeCounter.timeString,(
this.getWidth()-font.stringWidth(timeCounter.timeString))/2,1,0);

flushGraphics();
}


//*游戏进行中观看全图
privatevoiddoPlayingSeePerfectPicPaint()
...{
//*在游戏中切换到完整图案
g.setColor(1,16,57);
g.fillRect(
0,0,this.getWidth(),this.getHeight());
Fontfont;
font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
Imageimg
=creatImage("/res/stage"+stageNum+".png");
//*画出计时器
g.setColor(255,255,255);
g.drawString(timeCounter.timeString,(
this.getWidth()-font.stringWidth(timeCounter.timeString))/2,1,0);
g.drawImage(img,(
this.getWidth()-112)/2,font.getHeight()+2,0);
//*画出右下角的小缺口
g.setColor(1,16,57);
g.fillRect((
this.getWidth()-112)/2+82,font.getHeight()+2+82,30,30);
//*画出图片的方框
g.setColor(255,255,255);
g.drawRect((
this.getWidth()-112)/2,font.getHeight()+2,111,111);


flushGraphics();
}


//*游戏过一关
privatevoiddoStageClearPaint()
...{
g.setColor(
255,255,255);
g.fillRect(
0,0,this.getWidth(),this.getHeight());
Imageimg
=creatImage("/res/clear.png");
g.drawImage(img,(
this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/3,0);
//*描绘字体
g.setColor(1,16,57);
Fontfont;
font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
if(gameMode==MODE_NORMAL)
...{
StringwriteString
="最快过关:"+numToTime(quicklyPassOneStageTime);
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,(this.getHeight()-img.getHeight())/3+img.getHeight()+2,0);
writeString
="全部耗费:"+numToTime(usedTime);
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,(this.getHeight()-img.getHeight())/3+img.getHeight()+font.getHeight()+2,0);
}

elseif(gameMode==MODE_UTMOST)
...{
StringwriteString
="已经通过"+stagePassedNum+"关!坚持下去!";
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,(this.getHeight()-img.getHeight())/3+img.getHeight()+2,0);
}


//关闭本节声音播放,以方便下一关播放新音乐
try
...{
player.stop();
}

catch(Exceptionexp)
...{}

//重画
flushGraphics();
}

//*游戏全部过关
privatevoiddoStageAllClearPaint()
...{
g.setColor(
255,255,255);
g.fillRect(
0,0,this.getWidth(),this.getHeight());
//Imageimg=creatImage("/res/clear.png");
//g.drawImage(img,(this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/2,0);
g.setColor(255,0,0);
Fontfont;
font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_LARGE);
if(gameMode==MODE_NORMAL)
...{
StringwriteString
="顺利过关!";
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,this.getHeight()/2-font.getHeight()-1,0);
writeString
="最快过关:"+numToTime(quicklyPassOneStageTime);
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,this.getHeight()/2,0);
writeString
="全部耗费:"+numToTime(usedTime);
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,this.getHeight()/2+font.getHeight()+1,0);
}

elseif(gameMode==MODE_UTMOST)
...{
StringwriteString
="时间到!";
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,this.getHeight()/2-font.getHeight()-1,0);
writeString
="总过关数:"+stageNum;
g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,this.getHeight()/2,0);
}

//关闭本节声音播放,以方便下一画面播放新音乐
try
...{
player.stop();
}

catch(Exceptionexp)
...{}
//重画
flushGraphics();

}

//*游戏结束
privatevoiddoStageOverPaint()
...{
g.setColor(
255,255,255);
g.fillRect(
0,0,this.getWidth(),this.getHeight());
Imageimg
=creatImage("/res/gameover.png");
g.drawImage(img,(
this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/2,0);

flushGraphics();
}


//全部通关
privatevoiddoGameEndPaint()
...{
//返回选择画面
ListChoiceScreenlcs=newListChoiceScreen(display,mainMIDlet);
display.setCurrent(lcs);
}

//过关检查
privatevoiddoStageClearCheck()
...{
for(inti=0,k=1;i<4;i++)
for(intj=0;j<4;j++)
...{
if(i!=3||j!=3)
...{
if(puzzlePic.getCell(j,i)!=k)
...{
return;//直接跳出函数
}

k
++;
}

}


if(gameMode==MODE_NORMAL)//普通模式
...{
if(stageNum==MAX_STAGE)//达到最大关数
...{
gameState
=ALL_STAGE_CLEAR;
timeCounter.cancel();
//过关,停止计时器
if(quicklyPassOneStageTime>(300-lastTime))//判断耗费的最少时间
...{
quicklyPassOneStageTime
=(300-lastTime);
}

usedTime
=usedTime+(300-lastTime);
}

else//未达到最大关数
...{
gameState
=STAGE_CLEAR;
stageNum
++;//进入下一关
timeCounter.cancel();//过关,停止计时器
//如果保存时间至变量
if(quicklyPassOneStageTime==0)
...{
//此为第一关,quicklyPassOneStageTime中未存数据
quicklyPassOneStageTime=300-lastTime;//300为普通模式默认的时间
usedTime=300-lastTime;//总耗时
}

else//不是第一关,比较quicklyPassOneStageTime里数字大小
...{
if(quicklyPassOneStageTime>(300-lastTime))
...{
quicklyPassOneStageTime
=(300-lastTime);
}

usedTime
=usedTime+(300-lastTime);
}


}

}


elseif(gameMode==MODE_UTMOST)//极限模式
...{
if(stageNum==MAX_STAGE)//达到最大关卡数
...{
gameState
=STAGE_CLEAR;
stageNum
=1;//重新进入第一关
timeCounter.cancel();//过关,停止计时器
}

else//未到最大关数
...{
gameState
=STAGE_CLEAR;
stageNum
++;//进入下一关
timeCounter.cancel();//过关,停止计时器
//如果是普通模式则保存时间至变量
}

stagePassedNum
++;//过关数增加1
}


render();
//重新绘画
}

//*创建图片
privateImagecreatImage(StringfileName)
...{
Imageimg
=null;
try
...{
img
=Image.createImage(fileName);
}

catch(Exceptionexp)
...{
System.out.println(
"打开图片出错:"+fileName);
}

returnimg;

}


//*创建随机背景拼图
privateTiledLayercreatPuzzlePic()
...{
Imageimg
=null;
try
...{
img
=Image.createImage("/res/stage"+stageNum+".png");
}

catch(Exceptionexp)
...{}
TiledLayertiledLayer
=newTiledLayer(4,4,img,28,28);
//*产生随机数选择地图分散方式
Randomrdm=newRandom();
intmapNum=(rdm.nextInt()>>>1)%4;

for(inti=0;i<PUZZLE_MAP[mapNum].length;i++)
...{
intcolumn=i%4;
introw=(i-column)/4;
tiledLayer.setCell(column,row,PUZZLE_MAP[mapNum][i]);
}

returntiledLayer;
}

//*创建简单背景拼图,测试用
privateTiledLayercreatTestPuzzlePic()
...{
Imageimg
=null;
try
...{
img
=Image.createImage("/res/stage"+stageNum+".png");
}

catch(Exceptionexp)
...{}
TiledLayertiledLayer
=newTiledLayer(4,4,img,28,28);
//*产生随机数选择地图分散方式
int[]map=
...{
1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,0,15
}
;
for(inti=0;i<map.length;i++)
...{
intcolumn=i%4;
introw=(i-column)/4;
tiledLayer.setCell(column,row,map[i]);
}

returntiledLayer;
}

//*游戏中按下左键
privatevoiddoRight()
...{
booleancanChanged=true;
for(inti=0;i<4;i++)
...{
//*判断最空格是否在最左边
if(puzzlePic.getCell(0,i)==0)
...{
canChanged
=false;
break;
}

}

if(canChanged==true)
...{
//*可以移动方块
intspaceColumn,spaceRow,picCell;
spaceColumn
=spaceRow=0;
for(inti=0;i<puzzlePic.getColumns();i++)
for(intj=0;j<puzzlePic.getRows();j++)
...{
if(puzzlePic.getCell(i,j)==0)
...{
spaceColumn
=i;
spaceRow
=j;
break;
}

}

//*交换空格位置
picCell=puzzlePic.getCell(spaceColumn-1,spaceRow);
puzzlePic.setCell(spaceColumn,spaceRow,picCell);
puzzlePic.setCell(spaceColumn
-1,spaceRow,0);
//*绘图
render();
doStageClearCheck();
}



}

//*游戏中按下右键
privatevoiddoLeft()
...{
booleancanChanged=true;
for(inti=0;i<4;i++)
...{
//*判断最空格是否在最右边
if(puzzlePic.getCell(3,i)==0)
...{
canChanged
=false;
break;
}

}

if(canChanged==true)
...{
//*可以移动方块
intspaceColumn,spaceRow,picCell;
spaceColumn
=spaceRow=0;
for(inti=0;i<puzzlePic.getColumns();i++)
for(intj=0;j<puzzlePic.getRows();j++)
...{
if(puzzlePic.getCell(i,j)==0)
...{
spaceColumn
=i;
spaceRow
=j;
break;
}

}

//*交换空格位置
picCell=puzzlePic.getCell(spaceColumn+1,spaceRow);
puzzlePic.setCell(spaceColumn,spaceRow,picCell);
puzzlePic.setCell(spaceColumn
+1,spaceRow,0);
//*绘图
render();
doStageClearCheck();
}



}

//*游戏中按下上键
privatevoiddoDown()
...{
booleancanChanged=true;
for(inti=0;i<4;i++)
...{
//*判断最空格是否在最上边
if(puzzlePic.getCell(i,0)==0)
...{
canChanged
=false;
break;
}

}

if(canChanged==true)
...{
//*可以移动方块
intspaceColumn,spaceRow,picCell;
spaceColumn
=spaceRow=0;
for(inti=0;i<puzzlePic.getColumns();i++)
for(intj=0;j<puzzlePic.getRows();j++)
...{
if(puzzlePic.getCell(i,j)==0)
...{
spaceColumn
=i;
spaceRow
=j;
break;
}

}

//*交换空格位置
picCell=puzzlePic.getCell(spaceColumn,spaceRow-1);
puzzlePic.setCell(spaceColumn,spaceRow,picCell);
puzzlePic.setCell(spaceColumn,spaceRow
-1,0);
//*绘图
render();
doStageClearCheck();
}



}

//*游戏中按下下键
privatevoiddoUp()
...{
booleancanChanged=true;
for(inti=0;i<4;i++)
...{
//*判断最空格是否在最下边
if(puzzlePic.getCell(i,3)==0)
...{
canChanged
=false;
break;
}

}

if(canChanged==true)
...{
//*可以移动方块
intspaceColumn,spaceRow,picCell;
spaceColumn
=spaceRow=0;
for(inti=0;i<puzzlePic.getColumns();i++)
for(intj=0;j<puzzlePic.getRows();j++)
...{
if(puzzlePic.getCell(i,j)==0)
...{
spaceColumn
=i;
spaceRow
=j;
break;
}

}

//*交换空格位置
picCell=puzzlePic.getCell(spaceColumn,spaceRow+1);
puzzlePic.setCell(spaceColumn,spaceRow,picCell);
puzzlePic.setCell(spaceColumn,spaceRow
+1,0);
//*绘图
render();
doStageClearCheck();
}

}


//*按下键盘时;
protectedvoidkeyPressed(intkeyCode)
...{

if(gameState==STAGE_TITLE)
...{
gameState
=STAGE_START;
render();
}

elseif(gameState==STAGE_PLAYING&&isGamePaused!=true)
...{

switch(getGameAction(keyCode))
...{
caseCanvas.FIRE:
gameState
=STAGE_PLAYING_SEE_PERFECT_PIC;
render();
break;
caseCanvas.DOWN:
doDown();
break;
caseCanvas.UP:
doUp();
break;
caseCanvas.LEFT:
doLeft();
break;
caseCanvas.RIGHT:
doRight();
break;
}

}

elseif(gameState==STAGE_CLEAR)
...{
gameState
=STAGE_TITLE;
render();
}

elseif(gameState==ALL_STAGE_CLEAR)
...{
gameState
=GAME_END;
render();
}


}


//*松开键盘时
protectedvoidkeyReleased(intkeyCode)
...{
if(gameState==STAGE_PLAYING_SEE_PERFECT_PIC&&isGamePaused!=true&&getGameAction(keyCode)==Canvas.FIRE)
...{
gameState
=STAGE_PLAYING;
render();
}


}


//*时间转换函数
privateStringnumToTime(longtimeLong)
...{
inthour=(int)(timeLong/3600);
intminute=(int)((timeLong-(hour*3600))/60);
StringminuteSt
=String.valueOf(minute);
minuteSt
=(minuteSt.length()<2?"0"+minuteSt:minuteSt);//加上0变成两位数
intsec=(int)(timeLong-(hour*3600)-(minute*60));
StringsecSt
=String.valueOf(sec);
secSt
=(secSt.length()<2?"0"+secSt:secSt);//加上0变成两位数傞
StringtimeString=String.valueOf(hour)+":"+minuteSt+":"+secSt;
returntimeString;
}


//*计时器
publicclassTimeCounterTaskextendsTimerTask
...{
publicStringtimeString;
privatelongtimeLong;
publicTimeCounterTask(longtime)
...{
timeLong
=time;
changeTime();
}

publicvoidrun()
...{
changeTime();
render();
//重新绘图
}

privatevoidchangeTime()
...{
timeLong
=timeLong-1;//倒计时
if(timeLong==0)
...{
gameState
=STAGE_OVER;//时间到
}

else
...{
timeString
=numToTime(timeLong);
//把剩余时间储存到类外面变量(因为类的声明在doStartPainting()函数中,在函数外无法调用所建立的对象)
lastTime=timeLong;
}

}

}

}


// ------------------------------
// CrazyPuzzleMIDlet.java
// ------------------------------

package src;

import javax.microedition.midlet. * ;
import javax.microedition.lcdui. * ;

public class CrazyPuzzleMIDlet extends MIDlet ... {

privateDisplaydisplay;
publicCrazyPuzzleMIDlet()
...{
super();
display
=Display.getDisplay(this);
WelcomeCanvaswelcomeCanvas
=newWelcomeCanvas(display,this);
display.setCurrent(welcomeCanvas);
}

protectedvoidstartApp()throwsMIDletStateChangeException...{
//norains:TODOAuto-generatedmethodstub
}


protectedvoidpauseApp()...{
//TODOAuto-generatedmethodstub

}


protectedvoiddestroyApp(booleanarg0)throwsMIDletStateChangeException...{
//TODOAuto-generatedmethodstub

}


}


// ------------------------------
// ListChoiceScreen.java
// ------------------------------

package src;
import javax.microedition.lcdui. * ;
public class ListChoiceScreen extends List implements CommandListener
... {
privateDisplaydisplay;
privateCrazyPuzzleMIDletmainMIDlet;
//传入TestLatencyMIDlet主要是为了能进行退出程序的操作.
publicListChoiceScreen(Displaydisplay,CrazyPuzzleMIDletm)
...{
super("疯狂拼图",Choice.IMPLICIT);
mainMIDlet
=m;
this.display=display;
Imageimg1,img2,img3,img4;
img1
=img2=img3=img4=null;
try
...{
img1
=Image.createImage("/res/lis1.png");
img2
=Image.createImage("/res/lis2.png");
img3
=Image.createImage("/res/lis3.png");
img4
=Image.createImage("/res/lis4.png");
}

catch(Exceptionerro)
...{}
this.append("普通模式",img1);
this.append("极限模式",img2);
//this.append("高手排行",img2);
this.append("游戏说明",img3);
this.append("退出游戏",img4);
this.setCommandListener(this);
}


publicvoidcommandAction(Commandc,Displayables)
...{
if(c==List.SELECT_COMMAND)
...{
Listtmp
=(List)s;
intselected=tmp.getSelectedIndex();
Stringlist
=tmp.getString(selected);
if(list.equals("普通模式"))
...{
MyGameCanvasmyGameCanvas
=newMyGameCanvas(display,mainMIDlet,MyGameCanvas.MODE_NORMAL);
display.setCurrent(myGameCanvas);
}

elseif(list.equals("极限模式"))
...{
MyGameCanvasmyGameCanvas
=newMyGameCanvas(display,mainMIDlet,MyGameCanvas.MODE_UTMOST);
display.setCurrent(myGameCanvas);
}

elseif(list.equals("高手排行"))
...{

}

elseif(list.equals("游戏说明"))
...{
InfoScreeninfoScreen
=newInfoScreen(display,this);
display.setCurrent(infoScreen);
}

elseif(list.equals("退出游戏"))
...{
mainMIDlet.notifyDestroyed();
}

}

}

}


// ------------------------------
// RecordScreen.java
// ------------------------------

package src;

public class RecordScreen ... {

}


// ------------------------------
// InfoScreen.java
// ------------------------------

package src;
import javax.microedition.lcdui. * ;

public class InfoScreen extends Form implements CommandListener
... {
DisplayablenextDisp;
Displaydisplay;
publicInfoScreen(Displaydisplay,DisplayablenextDisp)
...{
super("");
this.nextDisp=nextDisp;
this.display=display;
this.append("游戏:疯狂拼图 "+"版本:1.0 "+"型号:泡泡堂 "+"作者:norains "+"联系:norains@163.com "
+"说明:游戏有两种模式选择,普通模式每关都重新计时,极限模式计算时间完毕前通过的关数.在游戏中按住5键可以切换到完整图案.");
this.addCommand(newCommand("返回",Command.BACK,0));
this.setCommandListener(this);
}

publicvoidcommandAction(Commandc,Displayables)
...{
Stringcmd
=c.getLabel();
if(cmd.equalsIgnoreCase("返回"))
...{
display.setCurrent(nextDisp);
}

}

}


// ------------------------------
// RMSUtil.java
// ------------------------------

package src;
import javax.microedition.rms. * ;
// 数据库操作公共类
public class RMSUtil
... {
publicstaticRecordStoreopenRSAnyway(Stringrsname)
...{
RecordStorers
=null;
if(rsname.length()>32)
...{
returnnull;
}

else
...{
try
...{
rs
=RecordStore.openRecordStore(rsname,true);
returnrs;
}

catch(Exceptionerro)
...{
returnnull;
}

}

}



publicstaticRecordStoreopenRSExisted(Stringrsname)
...{
RecordStorers
=null;
if(rsname.length()>32)
...{
returnnull;
}

else
...{
try
...{
rs
=RecordStore.openRecordStore(rsname,false);
returnrs;
}

catch(Exceptionerro)
...{
returnnull;
}

}

}



publicstaticbooleandeletRS(Stringrsname)
...{
if(rsname.length()>32)
...{
returnfalse;
}

else
...{
try
...{
RecordStore.deleteRecordStore(rsname);
returntrue;
}

catch(Exceptionerro)
...{
returnfalse;
}

}

}



}




// ------------------------------
// WelcomeCanvas.java
// ------------------------------

package src;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.media. * ;

import javax.microedition.lcdui.game. * ;
import javax.microedition.lcdui. * ;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
public class WelcomeCanvas extends Canvas
... {
privateDisplaydisplay;
privateCrazyPuzzleMIDletmainMIDlet;
privateImageimg;
privateintdispWidth;
privateintdispHeight;
privateintimgState;
privatePlayerplayer;

//**构造函数
publicWelcomeCanvas(Displaydisp,CrazyPuzzleMIDletmidlet)
...{
this.display=disp;
this.mainMIDlet=midlet;
this.setFullScreenMode(true);
dispWidth
=this.getWidth();
dispHeight
=this.getHeight();
imgState
=1;

//播放开头音乐
try
...{
InputStreamin;
in
=getClass().getResourceAsStream("/res/welcome.mid");
player
=Manager.createPlayer(in,"audio/midi");
player.setLoopCount(
1);
player.start();
}

catch(IOExceptione)...{}
catch(MediaExceptione)...{}

}



//**角色创建函数,以整张图
privateImagecreatImage(StringfileName)
...{
Imageimg
=null;
try
...{
img
=Image.createImage(fileName);
}

catch(Exceptionexp)
...{
System.out.println(exp);
}

returnimg;
}


//**绘图
publicvoidpaint(Graphicsg)
...{

switch(imgState)
...{
case1:
img
=creatImage("/res/flash1.png");
g.setColor(
1,16,57);
break;
case2:
img
=creatImage("/res/flash2.png");
break;
case3:
img
=creatImage("/res/flash3.png");
g.setColor(
255,255,255);
break;
}


g.fillRect(
0,0,dispWidth,dispHeight);
g.drawImage(img,(dispWidth
-img.getWidth())/2,(dispHeight-img.getHeight())/2,0);
}



protectedvoidkeyPressed(intkeycode)
...{
//conti=false;
if(imgState!=3)
...{
imgState
++;
repaint();
}

else
...{
ListChoiceScreenlistChoiceScreen
=newListChoiceScreen(display,mainMIDlet);
display.setCurrent(listChoiceScreen);
//停止播放音乐
try
...{
if(player!=null)
...{
player.stop();
}

}

catch(Exceptionerro)
...{}

}


}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值