79J2ME手机游戏设计案例源代码-SpaceWar

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

************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);}}*******GameMenuimportjava.io.IOException;importjava.util.Timer;importjava.util.TimerTask;importjavax.microedition.lcdui.Graphics;importjavax.microedition.lcdui.Image;importjavax.microedition.lcdui.game.GameCanvas;importjavax.microedition.lcdui.game.Sprite;/**Tochangethistemplate,chooseTools|Templates*andopenthetemplateintheeditor.*//****@authorAdministrator*/publicclassGameMenuextendsGameCanvas{privateGameMIDmid;//声明MIDletprivateintMenuIndex;//菜单项索引privatefinalbyteglt=Graphics.LEFT|Graphics.TOP;//锚点privateImagebackground,menu,menuarrow,icon;//菜单图像privateintmenuY=270;//菜单Y坐标privateintmenuArrowX=65;//箭头X坐标privateTimertimer;//声明定时器privateTimerTasktask;//定时器任务/*构造方法*/publicGameMenu(GameMIDmid){super(false);this.setFullScreenMode(true);//设置为全屏模式this.mid=mid;//设置MIDletMenuIndex=1;//初始化菜单项索引InitImage();//初始化图像menuAnima();//菜单动画GameMusic.playStoryMusic();//播放背景音乐}/*初始化图像*/privatevoidInitImage(){//加载图像资源try{background=Image.createImage(/background.png);menu=Image.createImage(/menu.png);menuarrow=Image.createImage(/menuarrow.png);icon=Image.createImage(/menuIcon.png);}catch(IOExceptionex){ex.printStackTrace();}}publicvoidrender(){Graphicsg=this.getGraphics();g.drawImage(background,0,0,glt);//绘制背景g.drawImage(icon,40,60,glt);//绘制菜单项g.drawRegion(menu,0,MenuIndex*18,72,18,Sprite.TRANS_NONE,83,menuY,glt);//绘制箭头g.drawImage(menuarrow,menuArrowX,menuY,glt);g.drawRegion(menuarrow,0,0,16,18,Sprite.TRANS_MIRROR,224-menuArrowX,menuY,glt);flushGraphics();//更新绘图}/*获取当前菜单项索引*/publicintgetMenuIndex(){returnMenuIndex;}/*设置当前菜单项索引*/publicvoidsetMenuIndex(intMenuIndex){this.MenuIndex=MenuIndex;}/*左移菜单项*/publicvoidmenuLeft(){MenuIndex--;if(MenuIndex0){MenuIndex=6;}}/*右移菜单项*/publicvoidmenuRight(){MenuIndex++;if(MenuIndex6){MenuIndex=0;}}/*按键事件处理,*/protectedvoidkeyPressed(intkeyCode){super.keyPressed(keyCode);intkeyState=this.getGameAction(keyCode);switch(keyState){caseLEFT://按下左键menuLeft();//菜单项左移break;caseRIGHT://按下右键menuRight();//菜单项右移break;caseFIRE://按下Fire键//根据当前选择的菜单索引值,进行相应处理switch(MenuIndex){case0://选择“继续游戏”的处理代码mid.loadGameWorld();//加载游戏画布release();//释放资源break;case1://选择“开始游戏”的处理代码mid.loadGameWorld();//加载游戏画布release();//释放资源break;case2://选择“设置”的处理代码break;case3://选择“排行榜”的处理代码break;case4://选择“帮助”的处理代码break;case5://选择“关于”的处理代码break;case6://选择“退出”的处理代码mid.exit();break;}break;}}/*菜单动画逻辑,主要用于循环绘制箭头图像*/privatevoidmenuAnima(){timer=newTimer();//创建定时器task=newTimerTask(){//创建定时器任务publicvoidrun(){//设置箭头X坐标if(menuArrowX55){menuArrowX--;}e

1 / 22
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功