您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > J2ME手机游戏设计案例源代码-SpaceWar_Level
*********GameMID/*@authorwizardyx*/importjavax.microedition.lcdui.Display;importjavax.microedition.midlet.*;/*游戏MIDlet*/publicclassGameMIDextendsMIDlet{privateDisplaydisplay;//声明DisplayprivateStartScreenstartscreen;//声明启动画面对象privateFlashScreenflashscreen;//声明闪屏画面对象privateGameMenugmenu;//声明菜单画面对象privateGameWorldgw;//声明游戏引擎框架privateGameMusicgm;//声明音效对象publicGameMID(){display=Display.getDisplay(this);//获取Displaygm=newGameMusic();loadFlashScreen();//加载闪屏画面}publicvoidstartApp(){}publicvoidpauseApp(){}publicvoiddestroyApp(booleanunconditional){gmenu=null;flashscreen=null;startscreen=null;gm=null;GameMusic.releases();System.gc();//释放资源}/*退出程序*/publicvoidexit(){try{destroyApp(false);}catch(Exceptione){}notifyDestroyed();}/*加载游戏启动画面*/publicvoidloadStartScreen(){flashscreen=null;startscreen=null;startscreen=newStartScreen(this);//创建启动画面display.setCurrent(startscreen);//设置启动画面为当前显示画面}/*加载闪屏画面*/publicvoidloadFlashScreen(){flashscreen=newFlashScreen(this);//创建闪屏display.setCurrent(flashscreen);//设置闪屏画面为当前显示画面}/*加载游戏菜单*/publicvoidloadGameMenu(intmenuIndex){flashscreen=null;startscreen=null;if(gmenu==null){gmenu=newGameMenu(this);//创建菜单}gmenu.setMenuIndex(menuIndex);//设置当前菜单项display.setCurrent(gmenu);//设置菜单画面为当前显示画面}/*加载游戏主界面*/publicvoidloadGameWorld(){gmenu=null;if(gw==null){gw=newGameWorld(this);//创建游戏引擎画布}display.setCurrent(gw);//设置游戏引擎画布为当前显示画面gw.start();}}**************FlashScreenimportjava.io.IOException;importjava.util.Timer;importjava.util.TimerTask;importjavax.microedition.lcdui.Canvas;importjavax.microedition.lcdui.Graphics;importjavax.microedition.lcdui.Image;/**Tochangethistemplate,chooseTools|Templates*andopenthetemplateintheeditor.*//****@authorWizardyx*//*闪屏画面*/publicclassFlashScreenextendsCanvas{GameMIDmid;Imageimg;privateTimertimer;//声明定时器privateTimerTasktask;//声明定时器任务/*构造方法*/publicFlashScreen(GameMIDmid){this.mid=mid;try{img=Image.createImage(/logo.png);}catch(IOExceptionex){ex.printStackTrace();}FlashTimer();//闪屏计时repaint();//绘制闪屏画面}//释放资源publicvoidrelease(){img=null;}/*按键事件处理,按任意键均加载游戏菜单画面*/protectedvoidkeyPressed(intkeyCode){super.keyPressed(keyCode);timer.cancel();release();mid.loadGameMenu(1);//加载游戏菜单}/*绘制闪屏画面*/protectedvoidpaint(Graphicsg){g.drawImage(img,0,0,Graphics.TOP|Graphics.LEFT);}/*闪屏画面计时*/privatevoidFlashTimer(){timer=newTimer();//创建定时器task=newTimerTask(){//创建定时任务privateinttimes=0;//计时变量publicvoidrun(){times++;//task任务计时,每次增加100ms(下面任务启动中定义)if(times10){//3000ms后,自动加载游戏启动界面release();mid.loadStartScreen();//加载游戏启动界面timer.cancel();}}};//启动task任务,在0ms后开始,每隔300ms运行一次timer.schedule(task,0,300);}}******GameDesign/**Tochangethistemplate,chooseTools|Templates*andopenthetemplateintheeditor.*/importjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;importjavax.microedition.lcdui.game.*;importjava.io.IOException;/***@authorAdministrator*/publicclassGameDesign{//editor-folddefaultstate=collapseddesc=GeneratedFields//GEN-BEGIN:|fields|0|privateImagebgTiles;privateTiledLayerbackground;privateTiledLayerMeteorite;///editor-fold//GEN-END:|fields|0|//editor-folddefaultstate=collapseddesc=GeneratedMethods//GEN-BEGIN:|methods|0|///editor-fold//GEN-END:|methods|0|publicImagegetBgTiles()throwsjava.io.IOException{//GEN-BEGIN:|1-getter|0|1-preInitif(bgTiles==null){//GEN-END:|1-getter|0|1-preInit//writepre-initusercodeherebgTiles=Image.createImage(/bgTiles.png);//GEN-BEGIN:|1-getter|1|1-postInit}//GEN-END:|1-getter|1|1-postInit//writepost-initusercodeherereturnthis.bgTiles;//GEN-BEGIN:|1-getter|2|}//GEN-END:|1-getter|2|publicTiledLayergetBackground()throwsjava.io.IOException{//GEN-BEGIN:|2-getter|0|2-preInitif(background==null){//GEN-END:|2-getter|0|2-preInit//writepre-initusercodeherebackground=newTiledLayer(8,11,getBgTiles(),32,32);//GEN-BEGIN:|2-getter|1|2-midInitint[][]tiles={{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1},{1,2,3,3,3,3,4,1}};//GEN-END:|2-getter|1|2-midInit//writemid-initusercodeherefor(introw=0;row11;row++){//GEN-BEGIN:|2-getter|2|2-postInitfor(intcol=0;col8;col++){background.setCell(col,row,tiles[row][col]);}}}//GEN-END:|2-getter|2|2-postInit//writepost-initusercodeherereturnbackground;//GEN-BEGIN:|2-getter|3|}//GEN-END:|2-getter|3|publicTiledLayergetMeteorite()throwsjava.io.IOException{//GEN-BEGIN:|3-getter|0|3-preInitif(Meteorite==null){//GEN-END:|3-getter|0|3-preInit//writepre-initusercodehereMeteorite=newTiledLayer(4,20,getBgTiles(),32,32);//GEN-BEGIN:|3-getter|1|3-midInitint[][]tiles={{21,0,0,0},{0,0,0,0},{22,0,0,0},{0,0,21,22},{0,0,0,0},{22,0,0,0},{0,21,0,0},{0,0,0,0},{0,0,0,0},{0,0,22,0},{0,0,0,0},{22,0,0,0},{0,0,0,22},{21,0,0,0},{0,0,0,0},{0,0,22,0},{0,0,0,0},{0,0,21,0},{0,22,0,0},{0,0,0,22}};//GEN-END:|3-getter|1|3-midInit//writemid-initusercodeherefor(introw=0;row20;row++){//GEN-BEGIN:|3-getter|2|3-postInitfor(intcol=0;col4;col++){Meteorite.setCell(col,row,tiles[row][col]);}}}//GEN-END:|3-getter|2|3-postInit//writepost-initusercodeh
本文标题:J2ME手机游戏设计案例源代码-SpaceWar_Level
链接地址:https://www.777doc.com/doc-6123765 .html