=0){rgBackground1.y=-scrHeight;}if(rgBackground2.y>=scrHeight){rgBackground2.y=0;}switch(GameState){caseGAMESTATE_MENU://画菜单DrawMenu(canvas);break;caseGAMESTATE_LEVELGUIDE:DrawLeverGuide(canvas);break;caseGAMESTATE_HELP:DrawHelp(canvas);break;caseGAMESTATE_SCORE:DrawScore(canvas);break;caseGAMESTATE_DETAIL:DrawDetail(canvas);break;caseGAMESTATE_GAME:DrawGame(canvas); default:break;}}publicvoidDrawMenu(Canvascanvas){DisplayImage(canvas,imgBackground,imgBackground.getWidth(),imgBackground.getHeight());ButtonStart.drawButton(canvas,null);ButtonHelp.drawButton(canvas,null);ButtonScore.drawButton(canvas,null);ButtonDetail.drawButton(canvas,null);ButtonReturn.drawButton(canvas,null);}publicvoidDrawLeverGuide(Canvascanvas){DisplayImage(canvas,imgBackground,imgBackground.getWidth(),imgBackground.getHeight());ButtonFirst.drawButton(canvas,null);ButtonSecond.drawButton(canvas,null);ButtonReturn.drawButton(canvas,null);}publicvoidDrawHelp(Canvascanvas){DisplayImage(canvas,imgHelpDisplay,imgHelpDisplay.getWidth(),imgHelpDisplay.getHeight());ButtonReturn.drawButton(canvas,null);}publicvoidDrawScore(Canvascanvas){//DisplayImage(canvas,imgScoreDisplay,imgScoreDisplay.getWidth(),imgScoreDisplay.getHeight());Paintpaint=newPaint();paint.setTextSize(22);//设置字体大小paint.setColor(Color.BLUE);//paint.setTypeface();//设置字体类型 canvas.drawText("用户名",50,50,paint);//paint.setTypeface();//设置字体类型Paintpaint1=newPaint();paint1.setTextSize(22);//设置字体大小paint1.setColor(Color.BLUE);canvas.drawText("分数",150,50,paint1);//canvas.drawText("o",33,33,11,scrWidth,null);ButtonReturn.drawButton(canvas,null);}publicvoidDrawDetail(Canvascanvas){DisplayImage(canvas,imgDetailDisplay,imgDetailDisplay.getWidth(),imgDetailDisplay.getHeight());ButtonReturn.drawButton(canvas,null);}publicvoidDrawGame(Canvascanvas){inti,j;if(rgSpriteCmd.x<0){//到达地图左边界rgSpriteCmd.IsLeftStop=true;rgSpriteCmd.x=0;}else{rgSpriteCmd.IsLeftStop=false;}if(rgSpriteCmd.x rgSpriteCmd.unWidth>scrWidth){//到达地图右边界rgSpriteCmd.IsRigtStop=true;rgSpriteCmd.x=scrWidth-rgSpriteCmd.unWidth;}else{rgSpriteCmd.IsRigtStop=false;}if(rgSpriteCmd.y<0){//到达地图上边界rgSpriteCmd.IsTopStop=true;rgSpriteCmd.y=0;}else{rgSpriteCmd.IsTopStop=false;}if(rgSpriteCmd.y rgSpriteCmd.unHeight>scrHeight){//到达地图下边界rgSpriteCmd.IsDownStop=true;rgSpriteCmd.y=scrHeight-rgSpriteCmd.unHeight;}else{rgSpriteCmd.IsDownStop=false;}////主机与敌机的碰撞判断及处理///////////////////////////////if((CheckHitEnemy(rgSpriteCmd,rgEnemy1Cmd)||CheckHitEnemy(rgSpriteCmd,rgEnemy2Cmd)||CheckHitEnemy(rgSpriteCmd,rgEnemy3Cmd))||CheckHitEnemy(rgSpriteCmd,rgEnemy4Cmd)||CheckHitEnemy(rgSpriteCmd,rgEnemy5Cmd)&&!rgSpriteCmd.IsHurt){rgSpriteCmd.CurrentState=MainState.enHURT;rgSpriteCmd.ProtectCount=18;rgSpriteCmd.IsHurt=true;rgBlood.unWidth-=19;if(rgBlood.unWidth<0){rgBlood.unWidth=96;}}/////主机子弹与敌机1的碰撞////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy1Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy1Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy1Cmd.x 15;rgBombCmds[j].y=rgEnemy1Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}////////主机子弹与敌机2的碰撞//////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy2Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy2Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy2Cmd.x 15;rgBombCmds[j].y=rgEnemy2Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////主机子弹与敌机3的碰撞/////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy3Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy3Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy3Cmd.x 15;rgBombCmds[j].y=rgEnemy3Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////主机子弹与敌机4的碰撞/////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy4Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy4Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy4Cmd.x 15;rgBombCmds[j].y=rgEnemy4Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////主机子弹与敌机5的碰撞/////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy5Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy5Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy5Cmd.x 15;rgBombCmds[j].y=rgEnemy5Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////炸弹的处理////////////////////////////////////for(i=0;i<5;i ){if(rgBombCmds[i].unLayer!=255){if(rgBombCmds[i].WalkCount<5){rgBombCmds[i].unSpriteIndex=rgBombCmds[i].WalkCount;}else{rgBombCmds[i].unLayer=255;}rgBombCmds[i].WalkCount ;}}////////////画背景/////////////////////////DrawSprites(canvas,imgBackground,rgBackground1);//第二个参数是要画的图片,第三个参数要画在屏幕上的位置等信息DrawSprites(canvas,imgBackground,rgBackground2);rgBackground1.y =10;rgBackground2.y =10;/////////////画血量/////////////////DrawSprites(canvas,imgBloodBg,rgBloodBg);DrawSprites(canvas,imgBlood,rgBlood);/////////////画分数//////////Paintpaint2=newPaint(); paint2.setTextSize(22);//设置字体大小paint2.setColor(Color.WHITE);canvas.drawText("分数:",370,50,paint2);intm=DeadEnemyCnt*76;Stringl=m"";Paintpaint3=newPaint();paint3.setTextSize(22);//设置字体大小paint3.setColor(Color.WHITE);canvas.drawText(l,420,50,paint3);///////过关判断//////////////////////////if(DeadEnemyCnt>30&&LeverNum==1){is_LeverUp=true;}////过关处理 主机、敌机、主机子弹全部消失 背景音乐停播 初始化恭喜过关以及画出来if(is_LeverUp){LeverCnt ;rgSpriteCmd.unLayer=255;rgEnemy1Cmd.unLayer=255;rgEnemy2Cmd.unLayer=255;rgEnemy3Cmd.unLayer=255;rgEnemy4Cmd.unLayer=255;for(i=0;i<5;i ){rgBulletCmd[i].unLayer=255;}mediaPlayer.pause();rgLerver.unLayer=0;rgLerver.x=(scrWidth-rgLerver.unWidth)/2;rgLerver.y=(scrHeight-rgLerver.unHeight)/2 MainTime%2*3;DrawSprites(canvas,imgPassLerver,rgLerver);//画出“恭喜过关”if(LeverCnt==40)//当计数器到40时进去下一关{LeverNum =1;TurnToGame();}}//////每帧各星星的位置并画出来////////////////////////////for(i=0;iscrHeight){if(MainTime%2==0){rgCmdStar[i].x=scrWidth/STAR_NUM*i 10;}else{rgCmdStar[i].x=scrWidth/STAR_NUM*(STAR_NUM-i) 10;}rgCmdStar[i].y=0;}else{rgCmdStar[i].y =5;}DrawSprites(canvas,imgStar,rgCmdStar[i]);}///////画“返回”和“声音”按钮//////////ButtonReturn.drawButton(canvas,null);ButtonSound.drawButton(canvas,null); /////子弹重新初始化///////////////////////for(i=0;i<20;i ){if(rgBulletCmd[i].unLayer==255&&MainTime%2==0&&!is_LeverUp){rgBulletCmd[i].unLayer=0;rgBulletCmd[i].x=rgSpriteCmd.x 10;rgBulletCmd[i].y=rgSpriteCmd.y-10;break;}}/////子弹的运动以及走出屏幕后的处理for(i=0;i<20;i ){if(rgBulletCmd[i].unLayer!=255){rgBulletCmd[i].y-=35;if(rgBulletCmd[i].y<-10){rgBulletCmd[i].unLayer=255;}}}///画子弹for(i=0;i<20;i ){DrawSprites(canvas,imgBullet,rgBulletCmd[i]);}///////敌人1初始化以及运动,画敌机1//////////////////if(rgEnemy1Cmd.unLayer==255&&!is_LeverUp){rgEnemy1Cmd.unLayer=0;rgEnemy1Cmd.x=(scrWidth-rgEnemy1Cmd.unWidth)/2;//屏幕x方向中间rgEnemy1Cmd.y=scrHeight/5;}else{rgEnemy1Cmd.y =14;if(rgEnemy1Cmd.y>scrHeight){rgEnemy1Cmd.unLayer=255;}}DrawSprites(canvas,imgEnemy1,rgEnemy1Cmd);//画敌机1;由imgEnemy1和rgEnemy1Cmd构成敌机1,imgEnemy1代表图条,rgEnemy1Cmd代表绘制位置等///////敌人2初始化以及运动,画敌机2//////////////////if(rgEnemy2Cmd.unLayer==255&&!is_LeverUp){rgEnemy2Cmd.unLayer=0;rgEnemy2Cmd.x=0;rgEnemy2Cmd.y=scrHeight/4;}else{rgEnemy2Cmd.x =5;rgEnemy2Cmd.y =10;if(rgEnemy2Cmd.y>scrHeight){rgEnemy2Cmd.unLayer=255;}}DrawSprites(canvas,imgEnemy2,rgEnemy2Cmd);//画敌机2;由imgEnemy2和rgEnemy2Cmd构成敌机2 ///////敌人3初始化以及运动,画敌机3//////////////////if(rgEnemy3Cmd.unLayer==255&&!is_LeverUp){rgEnemy3Cmd.unLayer=0;rgEnemy3Cmd.x=scrWidth-rgEnemy3Cmd.unWidth;//屏幕x方向最右边rgEnemy3Cmd.y=scrHeight/3;}else{if(rgEnemy3Cmd.IsRight){rgEnemy3Cmd.x =8;}else{rgEnemy3Cmd.x-=8;}if(rgEnemy3Cmd.x<0){rgEnemy3Cmd.IsRight=true;}if(rgEnemy3Cmd.x>scrWidth-rgEnemy3Cmd.unWidth){rgEnemy3Cmd.IsRight=false;}}DrawSprites(canvas,imgEnemy3,rgEnemy3Cmd);//画敌机3;由imgEnemy1和rgEnemy3Cmd构成敌机3,敌机1和敌机3使用相同的图片,不同的初始位置和移动//////敌人4初始化以及运动,画敌机4//////////////////if(rgEnemy4Cmd.unLayer==255&&!is_LeverUp){rgEnemy4Cmd.unLayer=0;rgEnemy4Cmd.x=scrWidth;//屏幕x方向最右边rgEnemy4Cmd.y=(scrHeight-rgEnemy4Cmd.unWidth)/2;}else{if(rgEnemy4Cmd.IsRight){rgEnemy4Cmd.y =5;rgEnemy4Cmd.x-=5;if(rgEnemy4Cmd.y>scrHeight||rgEnemy4Cmd.y<-scrHeight||rgEnemy4Cmd.x<-scrWidth){rgEnemy4Cmd.unLayer=255;}}else{rgEnemy4Cmd.y-=5;if(rgEnemy4Cmd.y>scrHeight||rgEnemy4Cmd.y<-scrHeight||rgEnemy4Cmd.x<-scrWidth){rgEnemy4Cmd.unLayer=255;}}if(rgEnemy4Cmd.y<(scrWidth-rgEnemy4Cmd.unWidth)/2){rgEnemy4Cmd.IsRight=true;}}DrawSprites(canvas,imgEnemy4,rgEnemy4Cmd);//画敌机4///////////////画5if(rgEnemy5Cmd.unLayer==255&&!is_LeverUp){rgEnemy5Cmd.unLayer=0;rgEnemy5Cmd.x=rgEnemy5Cmd.unWidth;//屏幕x方向中间rgEnemy5Cmd.y=(scrHeight-rgEnemy5Cmd.unHeight)/2;}else{rgEnemy5Cmd.x =5;if(rgEnemy5Cmd.x>scrWidth){rgEnemy5Cmd.unLayer=0;}}DrawSprites(canvas,imgEnemy5,rgEnemy5Cmd);//画敌机5/////画炸弹///////////////for(i=0;i<5;i ){DrawSprites(canvas,imgBomb,rgBombCmds[i]);}////画主机///////////////////DrawSprites(canvas,imgPlane,rgSpriteCmd);//主机各个状态下的处理 索引变化以及速度变化if(rgSpriteCmd.CurrentState==MainState.enSTAND){rgSpriteCmd.unSpriteIndex=MainTime/2%2;}elseif(rgSpriteCmd.CurrentState==MainState.enWALKRIGHT){rgSpriteCmd.WalkCount ;rgSpriteCmd.unSpriteIndex=3;if(rgSpriteCmd.WalkCount<5){rgSpriteCmd.x =8;}else{if(IsRightKeyRealess){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.CurrentState=MainState.enWALKRIGHT;}rgSpriteCmd.WalkCount=0;}}elseif(rgSpriteCmd.CurrentState==MainState.enWALKLEFT){rgSpriteCmd.WalkCount ;if(rgSpriteCmd.WalkCount<5){rgSpriteCmd.x-=8;rgSpriteCmd.unSpriteIndex=2;}else{if(IsLeftKeyRealess){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.CurrentState=MainState.enWALKLEFT;}rgSpriteCmd.WalkCount=0;}}elseif(rgSpriteCmd.CurrentState==MainState.enWALKUP){if(IsUpKeyRealess&&y_sensor>-1&&y_sensor<1){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.unSpriteIndex=0;rgSpriteCmd.y-=10;}}elseif(rgSpriteCmd.CurrentState==MainState.enWALKDOWN){if(IsDownKeyRealess&&y_sensor>-1&&y_sensor<1){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.unSpriteIndex=0;rgSpriteCmd.y =10;}}elseif(rgSpriteCmd.CurrentState==MainState.enHURT){rgSpriteCmd.unSpriteIndex=MainTime%2-1;rgSpriteCmd.ProtectCount--;if(rgSpriteCmd.ProtectCount==0){rgSpriteCmd.CurrentState=MainState.enSTAND;rgSpriteCmd.IsHurt=false;}}} ///////把图片画成全屏///////////////////////////////////////////public voidDisplayImage(Canvascanvas,Bitmapbitmap,intwidth,intheight){ if(bitmap!=null) { RectscrRect=newRect();//绘制区域在屏幕上的矩形范围 scrRect.left=0;//绘制区域左上角顶点在屏幕上的x坐标 scrRect.top=0;//绘制区域左上角顶点在屏幕上的y坐标 scrRect.right=scrWidth;//width;//绘制区域右下角顶点在屏幕上的x坐标 scrRect.bottom=scrHeight;//height;//绘制区域右下角顶点在屏幕上的y坐标 RectimgRect=newRect();//绘制区域在图片上的矩形范围 imgRect.left=0;//绘制区域左上角顶点距离图片左上角的x坐标 imgRect.top=0;//绘制区域左上角顶点距离图片左上角的y坐标 imgRect.right=0 width;//绘制区域右下角顶点距离图片左上角的x坐标 imgRect.bottom=0 height;//绘制区域右下角顶点距离图片左上角的y坐标 canvas.drawBitmap(bitmap,imgRect,scrRect,null);//绘制图片 } }/////////////////////////////////////////////////////////////////////////// ////////画精灵(相对于屏幕的相对位置)//////////////////////public voidDrawSprites(Canvascanvas,Bitmapbitmap,SpriteCmdspriteCmd){if(spriteCmd.unLayer!=255){DisplayImage(canvas,bitmap,//要画的图片spriteCmd.x,//图片左上角画在屏幕上的x位置spriteCmd.y,//图片左上角画在屏幕上的y位置spriteCmd.unWidth,//图片的宽度spriteCmd.unHeight,//图片的高度0,//要画的图片在整个图片中的x方向位置spriteCmd.unSpriteIndex*spriteCmd.unHeight);//要画的图片在整个图片中的y方向位置}}//画图片//参数依次为画布Canvas对象,位图对象,在屏幕上的x坐标,在屏幕上的y坐标,//绘制的宽度,绘制的高度,在图片上的x坐标,在图片上的y坐标public voidDisplayImage(Canvascanvas,Bitmapbitmap,intscrX,intscrY,intwidth,intheight,intimgX,intimgY){if(bitmap!=null){RectscrRect=newRect();//绘制区域在屏幕上的矩形范围scrRect.left=scrX;//绘制区域左上角顶点在屏幕上的x坐标scrRect.top=scrY;//绘制区域左上角顶点在屏幕上的y坐标scrRect.right=scrX width;//绘制区域右下角顶点在屏幕上的x坐标scrRect.bottom=scrY height;//绘制区域右下角顶点在屏幕上的y坐标RectimgRect=newRect();//绘制区域在图片上的矩形范围imgRect.left=imgX;//绘制区域左上角顶点距离图片左上角的x坐标imgRect.top=imgY;//绘制区域左上角顶点距离图片左上角的y坐标imgRect.right=imgX width;//绘制区域右下角顶点距离图片左上角的x坐标imgRect.bottom=imgY height;//绘制区域右下角顶点距离图片左上角的y坐标canvas.drawBitmap(bitmap,imgRect,scrRect,null);//绘制图片}}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////触屏事件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////publicbooleanonTouchEvent(MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_DOWN://当触摸到屏幕IsRightKeyRealess=false;IsLeftKeyRealess=false;IsUpKeyRealess=false;IsDownKeyRealess=false;switch(GameState){caseGAMESTATE_MENU:onTouchEventInMenu(event);//菜单界面的触屏事件处理break;caseGAMESTATE_LEVELGUIDE:onTouchEventLeverGuide(event);//选关界面的触屏事件处理break;caseGAMESTATE_HELP:onTouchEventHelp(event);//帮助界面的触屏事件处理break;caseGAMESTATE_SCORE:onTouchEventScore(event);//分数界面的触屏事件处理break;caseGAMESTATE_DETAIL:onTouchEventHelp(event);//信息界面的触屏事件处理break;caseGAMESTATE_GAME:onTouchEventInGame(event);//游戏界面的触屏事件处理break;default:break;}returntrue;caseMotionEvent.ACTION_UP://当手抬起,离开屏幕IsRightKeyRealess=true;IsLeftKeyRealess=true;IsUpKeyRealess=true;IsDownKeyRealess=true;returntrue;default:break;}returnsuper.onTouchEvent(event);}publicvoidonTouchEventInMenu(MotionEventevent)//菜单界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonStart.isClick(x,y)){TurnToLeverGuide();}elseif(ButtonHelp.isClick(x,y)){TurnToHelp();}elseif(ButtonScore.isClick(x,y)){TurnToScore();}elseif(ButtonDetail.isClick(x,y)){TurnToDetail();}elseif(ButtonReturn.isClick(x,y)){Is_exit=true;CloseGame();}}publicvoidonTouchEventLeverGuide(MotionEventevent)//选关界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonReturn.isClick(x,y)){TurnToMenu();}elseif(ButtonFirst.isClick(x,y)){LeverNum=1;TurnToGame();}elseif(ButtonSecond.isClick(x,y)){LeverNum=2;TurnToGame();}}publicvoidonTouchEventHelp(MotionEventevent)//帮助界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonReturn.isClick(x,y)){TurnToMenu();}}publicvoidonTouchEventScore(MotionEventevent)//帮助界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonReturn.isClick(x,y)){TurnToMenu();}}publicvoidonTouchEventInGame(MotionEventevent)//游戏界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();//////处理返回键///////////////////////////////if(ButtonReturn.isClick(x,y)){TurnToMenu();mediaPlayer.pause();}//////////////////////////////////////////////////////////声音处理///////////////////////////elseif(ButtonSound.isClick(x,y)){if(is_SoundOn){ButtonSound.setButtonPic(imgSoundOff);is_SoundOn=false;mediaPlayer.pause();}else{ButtonSound.setButtonPic(imgSoundOn);is_SoundOn=true; mediaPlayer.start(); mediaPlayer.setLooping(true);}}/////////////////////////////////////////////////////////////////根据触摸点的位置判断主角飞机的运动状态////////////////////if((x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)>(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)&&(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)>-(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)&&!rgSpriteCmd.IsRigtStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKRIGHT;//向右走}elseif((x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2<(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2))&&(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)<-(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)&&!rgSpriteCmd.IsLeftStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKLEFT;//向左走}elseif((y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)< (x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2) &&(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)<-(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2) &&!rgSpriteCmd.IsTopStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKUP;//向上走}elseif((y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)>=(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)&&(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)>=-(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)&&!rgSpriteCmd.IsDownStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKDOWN;//向下走}}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////publicBoolean CheckHitEnemy(SpriteCmdCmd,SpriteCmdSpCmd){if(Cmd.unLayer!=255&&SpCmd.unLayer!=255){if(Cmd.x Cmd.unWidth>SpCmd.x&&Cmd.xSpCmd.y&&Cmd.y0说明当前手机左翻x<0右翻y_sensor=event.values[1];//手机纵向翻滚//y>0说明当前手机下翻y<0上翻z_sensor=event.values[2];//屏幕的朝向//z>0手机屏幕朝上z<0手机屏幕朝下}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////退出游戏时释放所有资源///////////////////////////////////////////////////////////////////////////////////////publicvoidCloseGame(){FreeAppData();//释放应用所有资源((picture)this.getContext()).CloseActivity();//关闭应用}publicvoidFreeAppData(){//释放图片资源ReleaseImage(imgPlane);ReleaseImage(imgStar);ReleaseImage(imgStar);ReleaseImage(imgEnemy1);ReleaseImage(imgEnemy2);ReleaseImage(imgEnemy3);ReleaseImage(imgEnemy4);ReleaseImage(imgBomb);ReleaseImage(imgPassLerver);ReleaseImage(imgBloodBg);ReleaseImage(imgBlood);ReleaseImage(imgBackground);ReleaseImage(imgStart);ReleaseImage(imgHelp);ReleaseImage(imgScore);ReleaseImage(imgDetail);ReleaseImage(imgFirst);ReleaseImage(imgSecond);ReleaseImage(imgHelpDisplay);ReleaseImage(imgScoreDisplay);ReleaseImage(imgDetailDisplay);ReleaseImage(imgReturn);ReleaseImage(imgSoundOn);ReleaseImage(imgSoundOff);if(mediaPlayer!=null){mediaPlayer.stop();//停止播放声音mediaPlayer.release();//释放mediaPlayer资源mediaPlayer=null;//对象置空}//释放声音资源}publicstaticvoidFreeMedia(){if(mediaPlayer!=null){mediaPlayer.stop();//停止播放声音mediaPlayer.release();//释放mediaPlayer资源mediaPlayer=null;//对象置空}}publicstaticBitmapReleaseImage(Bitmapbitmap){if(bitmap!=null&&!bitmap.isRecycled()) {//bitmap.recycle();//用于回收该bitmap所占用的内存bitmap=null;//将bitmap置空 }returnnull;//返回空,将原图片对象置为空}}-IMDN开发者社群-imdn.cn">
=0){rgBackground1.y=-scrHeight;}if(rgBackground2.y>=scrHeight){rgBackground2.y=0;}switch(GameState){caseGAMESTATE_MENU://画菜单DrawMenu(canvas);break;caseGAMESTATE_LEVELGUIDE:DrawLeverGuide(canvas);break;caseGAMESTATE_HELP:DrawHelp(canvas);break;caseGAMESTATE_SCORE:DrawScore(canvas);break;caseGAMESTATE_DETAIL:DrawDetail(canvas);break;caseGAMESTATE_GAME:DrawGame(canvas); default:break;}}publicvoidDrawMenu(Canvascanvas){DisplayImage(canvas,imgBackground,imgBackground.getWidth(),imgBackground.getHeight());ButtonStart.drawButton(canvas,null);ButtonHelp.drawButton(canvas,null);ButtonScore.drawButton(canvas,null);ButtonDetail.drawButton(canvas,null);ButtonReturn.drawButton(canvas,null);}publicvoidDrawLeverGuide(Canvascanvas){DisplayImage(canvas,imgBackground,imgBackground.getWidth(),imgBackground.getHeight());ButtonFirst.drawButton(canvas,null);ButtonSecond.drawButton(canvas,null);ButtonReturn.drawButton(canvas,null);}publicvoidDrawHelp(Canvascanvas){DisplayImage(canvas,imgHelpDisplay,imgHelpDisplay.getWidth(),imgHelpDisplay.getHeight());ButtonReturn.drawButton(canvas,null);}publicvoidDrawScore(Canvascanvas){//DisplayImage(canvas,imgScoreDisplay,imgScoreDisplay.getWidth(),imgScoreDisplay.getHeight());Paintpaint=newPaint();paint.setTextSize(22);//设置字体大小paint.setColor(Color.BLUE);//paint.setTypeface();//设置字体类型 canvas.drawText("用户名",50,50,paint);//paint.setTypeface();//设置字体类型Paintpaint1=newPaint();paint1.setTextSize(22);//设置字体大小paint1.setColor(Color.BLUE);canvas.drawText("分数",150,50,paint1);//canvas.drawText("o",33,33,11,scrWidth,null);ButtonReturn.drawButton(canvas,null);}publicvoidDrawDetail(Canvascanvas){DisplayImage(canvas,imgDetailDisplay,imgDetailDisplay.getWidth(),imgDetailDisplay.getHeight());ButtonReturn.drawButton(canvas,null);}publicvoidDrawGame(Canvascanvas){inti,j;if(rgSpriteCmd.x<0){//到达地图左边界rgSpriteCmd.IsLeftStop=true;rgSpriteCmd.x=0;}else{rgSpriteCmd.IsLeftStop=false;}if(rgSpriteCmd.x rgSpriteCmd.unWidth>scrWidth){//到达地图右边界rgSpriteCmd.IsRigtStop=true;rgSpriteCmd.x=scrWidth-rgSpriteCmd.unWidth;}else{rgSpriteCmd.IsRigtStop=false;}if(rgSpriteCmd.y<0){//到达地图上边界rgSpriteCmd.IsTopStop=true;rgSpriteCmd.y=0;}else{rgSpriteCmd.IsTopStop=false;}if(rgSpriteCmd.y rgSpriteCmd.unHeight>scrHeight){//到达地图下边界rgSpriteCmd.IsDownStop=true;rgSpriteCmd.y=scrHeight-rgSpriteCmd.unHeight;}else{rgSpriteCmd.IsDownStop=false;}////主机与敌机的碰撞判断及处理///////////////////////////////if((CheckHitEnemy(rgSpriteCmd,rgEnemy1Cmd)||CheckHitEnemy(rgSpriteCmd,rgEnemy2Cmd)||CheckHitEnemy(rgSpriteCmd,rgEnemy3Cmd))||CheckHitEnemy(rgSpriteCmd,rgEnemy4Cmd)||CheckHitEnemy(rgSpriteCmd,rgEnemy5Cmd)&&!rgSpriteCmd.IsHurt){rgSpriteCmd.CurrentState=MainState.enHURT;rgSpriteCmd.ProtectCount=18;rgSpriteCmd.IsHurt=true;rgBlood.unWidth-=19;if(rgBlood.unWidth<0){rgBlood.unWidth=96;}}/////主机子弹与敌机1的碰撞////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy1Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy1Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy1Cmd.x 15;rgBombCmds[j].y=rgEnemy1Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}////////主机子弹与敌机2的碰撞//////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy2Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy2Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy2Cmd.x 15;rgBombCmds[j].y=rgEnemy2Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////主机子弹与敌机3的碰撞/////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy3Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy3Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy3Cmd.x 15;rgBombCmds[j].y=rgEnemy3Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////主机子弹与敌机4的碰撞/////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy4Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy4Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy4Cmd.x 15;rgBombCmds[j].y=rgEnemy4Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////主机子弹与敌机5的碰撞/////////////////////////////////for(i=0;i<20;i ){if(CheckHitEnemy(rgBulletCmd[i],rgEnemy5Cmd)){rgBulletCmd[i].unLayer=255;rgEnemy5Cmd.unLayer=255;for(j=0;j<5;j ){if(rgBombCmds[j].unLayer==255){rgBombCmds[j].unLayer=0;rgBombCmds[j].x=rgEnemy5Cmd.x 15;rgBombCmds[j].y=rgEnemy5Cmd.y 15;rgBombCmds[j].WalkCount=0;rgBombCmds[j].unSpriteIndex=0;break;}}DeadEnemyCnt ;}}/////炸弹的处理////////////////////////////////////for(i=0;i<5;i ){if(rgBombCmds[i].unLayer!=255){if(rgBombCmds[i].WalkCount<5){rgBombCmds[i].unSpriteIndex=rgBombCmds[i].WalkCount;}else{rgBombCmds[i].unLayer=255;}rgBombCmds[i].WalkCount ;}}////////////画背景/////////////////////////DrawSprites(canvas,imgBackground,rgBackground1);//第二个参数是要画的图片,第三个参数要画在屏幕上的位置等信息DrawSprites(canvas,imgBackground,rgBackground2);rgBackground1.y =10;rgBackground2.y =10;/////////////画血量/////////////////DrawSprites(canvas,imgBloodBg,rgBloodBg);DrawSprites(canvas,imgBlood,rgBlood);/////////////画分数//////////Paintpaint2=newPaint(); paint2.setTextSize(22);//设置字体大小paint2.setColor(Color.WHITE);canvas.drawText("分数:",370,50,paint2);intm=DeadEnemyCnt*76;Stringl=m"";Paintpaint3=newPaint();paint3.setTextSize(22);//设置字体大小paint3.setColor(Color.WHITE);canvas.drawText(l,420,50,paint3);///////过关判断//////////////////////////if(DeadEnemyCnt>30&&LeverNum==1){is_LeverUp=true;}////过关处理 主机、敌机、主机子弹全部消失 背景音乐停播 初始化恭喜过关以及画出来if(is_LeverUp){LeverCnt ;rgSpriteCmd.unLayer=255;rgEnemy1Cmd.unLayer=255;rgEnemy2Cmd.unLayer=255;rgEnemy3Cmd.unLayer=255;rgEnemy4Cmd.unLayer=255;for(i=0;i<5;i ){rgBulletCmd[i].unLayer=255;}mediaPlayer.pause();rgLerver.unLayer=0;rgLerver.x=(scrWidth-rgLerver.unWidth)/2;rgLerver.y=(scrHeight-rgLerver.unHeight)/2 MainTime%2*3;DrawSprites(canvas,imgPassLerver,rgLerver);//画出“恭喜过关”if(LeverCnt==40)//当计数器到40时进去下一关{LeverNum =1;TurnToGame();}}//////每帧各星星的位置并画出来////////////////////////////for(i=0;iscrHeight){if(MainTime%2==0){rgCmdStar[i].x=scrWidth/STAR_NUM*i 10;}else{rgCmdStar[i].x=scrWidth/STAR_NUM*(STAR_NUM-i) 10;}rgCmdStar[i].y=0;}else{rgCmdStar[i].y =5;}DrawSprites(canvas,imgStar,rgCmdStar[i]);}///////画“返回”和“声音”按钮//////////ButtonReturn.drawButton(canvas,null);ButtonSound.drawButton(canvas,null); /////子弹重新初始化///////////////////////for(i=0;i<20;i ){if(rgBulletCmd[i].unLayer==255&&MainTime%2==0&&!is_LeverUp){rgBulletCmd[i].unLayer=0;rgBulletCmd[i].x=rgSpriteCmd.x 10;rgBulletCmd[i].y=rgSpriteCmd.y-10;break;}}/////子弹的运动以及走出屏幕后的处理for(i=0;i<20;i ){if(rgBulletCmd[i].unLayer!=255){rgBulletCmd[i].y-=35;if(rgBulletCmd[i].y<-10){rgBulletCmd[i].unLayer=255;}}}///画子弹for(i=0;i<20;i ){DrawSprites(canvas,imgBullet,rgBulletCmd[i]);}///////敌人1初始化以及运动,画敌机1//////////////////if(rgEnemy1Cmd.unLayer==255&&!is_LeverUp){rgEnemy1Cmd.unLayer=0;rgEnemy1Cmd.x=(scrWidth-rgEnemy1Cmd.unWidth)/2;//屏幕x方向中间rgEnemy1Cmd.y=scrHeight/5;}else{rgEnemy1Cmd.y =14;if(rgEnemy1Cmd.y>scrHeight){rgEnemy1Cmd.unLayer=255;}}DrawSprites(canvas,imgEnemy1,rgEnemy1Cmd);//画敌机1;由imgEnemy1和rgEnemy1Cmd构成敌机1,imgEnemy1代表图条,rgEnemy1Cmd代表绘制位置等///////敌人2初始化以及运动,画敌机2//////////////////if(rgEnemy2Cmd.unLayer==255&&!is_LeverUp){rgEnemy2Cmd.unLayer=0;rgEnemy2Cmd.x=0;rgEnemy2Cmd.y=scrHeight/4;}else{rgEnemy2Cmd.x =5;rgEnemy2Cmd.y =10;if(rgEnemy2Cmd.y>scrHeight){rgEnemy2Cmd.unLayer=255;}}DrawSprites(canvas,imgEnemy2,rgEnemy2Cmd);//画敌机2;由imgEnemy2和rgEnemy2Cmd构成敌机2 ///////敌人3初始化以及运动,画敌机3//////////////////if(rgEnemy3Cmd.unLayer==255&&!is_LeverUp){rgEnemy3Cmd.unLayer=0;rgEnemy3Cmd.x=scrWidth-rgEnemy3Cmd.unWidth;//屏幕x方向最右边rgEnemy3Cmd.y=scrHeight/3;}else{if(rgEnemy3Cmd.IsRight){rgEnemy3Cmd.x =8;}else{rgEnemy3Cmd.x-=8;}if(rgEnemy3Cmd.x<0){rgEnemy3Cmd.IsRight=true;}if(rgEnemy3Cmd.x>scrWidth-rgEnemy3Cmd.unWidth){rgEnemy3Cmd.IsRight=false;}}DrawSprites(canvas,imgEnemy3,rgEnemy3Cmd);//画敌机3;由imgEnemy1和rgEnemy3Cmd构成敌机3,敌机1和敌机3使用相同的图片,不同的初始位置和移动//////敌人4初始化以及运动,画敌机4//////////////////if(rgEnemy4Cmd.unLayer==255&&!is_LeverUp){rgEnemy4Cmd.unLayer=0;rgEnemy4Cmd.x=scrWidth;//屏幕x方向最右边rgEnemy4Cmd.y=(scrHeight-rgEnemy4Cmd.unWidth)/2;}else{if(rgEnemy4Cmd.IsRight){rgEnemy4Cmd.y =5;rgEnemy4Cmd.x-=5;if(rgEnemy4Cmd.y>scrHeight||rgEnemy4Cmd.y<-scrHeight||rgEnemy4Cmd.x<-scrWidth){rgEnemy4Cmd.unLayer=255;}}else{rgEnemy4Cmd.y-=5;if(rgEnemy4Cmd.y>scrHeight||rgEnemy4Cmd.y<-scrHeight||rgEnemy4Cmd.x<-scrWidth){rgEnemy4Cmd.unLayer=255;}}if(rgEnemy4Cmd.y<(scrWidth-rgEnemy4Cmd.unWidth)/2){rgEnemy4Cmd.IsRight=true;}}DrawSprites(canvas,imgEnemy4,rgEnemy4Cmd);//画敌机4///////////////画5if(rgEnemy5Cmd.unLayer==255&&!is_LeverUp){rgEnemy5Cmd.unLayer=0;rgEnemy5Cmd.x=rgEnemy5Cmd.unWidth;//屏幕x方向中间rgEnemy5Cmd.y=(scrHeight-rgEnemy5Cmd.unHeight)/2;}else{rgEnemy5Cmd.x =5;if(rgEnemy5Cmd.x>scrWidth){rgEnemy5Cmd.unLayer=0;}}DrawSprites(canvas,imgEnemy5,rgEnemy5Cmd);//画敌机5/////画炸弹///////////////for(i=0;i<5;i ){DrawSprites(canvas,imgBomb,rgBombCmds[i]);}////画主机///////////////////DrawSprites(canvas,imgPlane,rgSpriteCmd);//主机各个状态下的处理 索引变化以及速度变化if(rgSpriteCmd.CurrentState==MainState.enSTAND){rgSpriteCmd.unSpriteIndex=MainTime/2%2;}elseif(rgSpriteCmd.CurrentState==MainState.enWALKRIGHT){rgSpriteCmd.WalkCount ;rgSpriteCmd.unSpriteIndex=3;if(rgSpriteCmd.WalkCount<5){rgSpriteCmd.x =8;}else{if(IsRightKeyRealess){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.CurrentState=MainState.enWALKRIGHT;}rgSpriteCmd.WalkCount=0;}}elseif(rgSpriteCmd.CurrentState==MainState.enWALKLEFT){rgSpriteCmd.WalkCount ;if(rgSpriteCmd.WalkCount<5){rgSpriteCmd.x-=8;rgSpriteCmd.unSpriteIndex=2;}else{if(IsLeftKeyRealess){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.CurrentState=MainState.enWALKLEFT;}rgSpriteCmd.WalkCount=0;}}elseif(rgSpriteCmd.CurrentState==MainState.enWALKUP){if(IsUpKeyRealess&&y_sensor>-1&&y_sensor<1){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.unSpriteIndex=0;rgSpriteCmd.y-=10;}}elseif(rgSpriteCmd.CurrentState==MainState.enWALKDOWN){if(IsDownKeyRealess&&y_sensor>-1&&y_sensor<1){rgSpriteCmd.CurrentState=MainState.enSTAND;}else{rgSpriteCmd.unSpriteIndex=0;rgSpriteCmd.y =10;}}elseif(rgSpriteCmd.CurrentState==MainState.enHURT){rgSpriteCmd.unSpriteIndex=MainTime%2-1;rgSpriteCmd.ProtectCount--;if(rgSpriteCmd.ProtectCount==0){rgSpriteCmd.CurrentState=MainState.enSTAND;rgSpriteCmd.IsHurt=false;}}} ///////把图片画成全屏///////////////////////////////////////////public voidDisplayImage(Canvascanvas,Bitmapbitmap,intwidth,intheight){ if(bitmap!=null) { RectscrRect=newRect();//绘制区域在屏幕上的矩形范围 scrRect.left=0;//绘制区域左上角顶点在屏幕上的x坐标 scrRect.top=0;//绘制区域左上角顶点在屏幕上的y坐标 scrRect.right=scrWidth;//width;//绘制区域右下角顶点在屏幕上的x坐标 scrRect.bottom=scrHeight;//height;//绘制区域右下角顶点在屏幕上的y坐标 RectimgRect=newRect();//绘制区域在图片上的矩形范围 imgRect.left=0;//绘制区域左上角顶点距离图片左上角的x坐标 imgRect.top=0;//绘制区域左上角顶点距离图片左上角的y坐标 imgRect.right=0 width;//绘制区域右下角顶点距离图片左上角的x坐标 imgRect.bottom=0 height;//绘制区域右下角顶点距离图片左上角的y坐标 canvas.drawBitmap(bitmap,imgRect,scrRect,null);//绘制图片 } }/////////////////////////////////////////////////////////////////////////// ////////画精灵(相对于屏幕的相对位置)//////////////////////public voidDrawSprites(Canvascanvas,Bitmapbitmap,SpriteCmdspriteCmd){if(spriteCmd.unLayer!=255){DisplayImage(canvas,bitmap,//要画的图片spriteCmd.x,//图片左上角画在屏幕上的x位置spriteCmd.y,//图片左上角画在屏幕上的y位置spriteCmd.unWidth,//图片的宽度spriteCmd.unHeight,//图片的高度0,//要画的图片在整个图片中的x方向位置spriteCmd.unSpriteIndex*spriteCmd.unHeight);//要画的图片在整个图片中的y方向位置}}//画图片//参数依次为画布Canvas对象,位图对象,在屏幕上的x坐标,在屏幕上的y坐标,//绘制的宽度,绘制的高度,在图片上的x坐标,在图片上的y坐标public voidDisplayImage(Canvascanvas,Bitmapbitmap,intscrX,intscrY,intwidth,intheight,intimgX,intimgY){if(bitmap!=null){RectscrRect=newRect();//绘制区域在屏幕上的矩形范围scrRect.left=scrX;//绘制区域左上角顶点在屏幕上的x坐标scrRect.top=scrY;//绘制区域左上角顶点在屏幕上的y坐标scrRect.right=scrX width;//绘制区域右下角顶点在屏幕上的x坐标scrRect.bottom=scrY height;//绘制区域右下角顶点在屏幕上的y坐标RectimgRect=newRect();//绘制区域在图片上的矩形范围imgRect.left=imgX;//绘制区域左上角顶点距离图片左上角的x坐标imgRect.top=imgY;//绘制区域左上角顶点距离图片左上角的y坐标imgRect.right=imgX width;//绘制区域右下角顶点距离图片左上角的x坐标imgRect.bottom=imgY height;//绘制区域右下角顶点距离图片左上角的y坐标canvas.drawBitmap(bitmap,imgRect,scrRect,null);//绘制图片}}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////触屏事件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////publicbooleanonTouchEvent(MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_DOWN://当触摸到屏幕IsRightKeyRealess=false;IsLeftKeyRealess=false;IsUpKeyRealess=false;IsDownKeyRealess=false;switch(GameState){caseGAMESTATE_MENU:onTouchEventInMenu(event);//菜单界面的触屏事件处理break;caseGAMESTATE_LEVELGUIDE:onTouchEventLeverGuide(event);//选关界面的触屏事件处理break;caseGAMESTATE_HELP:onTouchEventHelp(event);//帮助界面的触屏事件处理break;caseGAMESTATE_SCORE:onTouchEventScore(event);//分数界面的触屏事件处理break;caseGAMESTATE_DETAIL:onTouchEventHelp(event);//信息界面的触屏事件处理break;caseGAMESTATE_GAME:onTouchEventInGame(event);//游戏界面的触屏事件处理break;default:break;}returntrue;caseMotionEvent.ACTION_UP://当手抬起,离开屏幕IsRightKeyRealess=true;IsLeftKeyRealess=true;IsUpKeyRealess=true;IsDownKeyRealess=true;returntrue;default:break;}returnsuper.onTouchEvent(event);}publicvoidonTouchEventInMenu(MotionEventevent)//菜单界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonStart.isClick(x,y)){TurnToLeverGuide();}elseif(ButtonHelp.isClick(x,y)){TurnToHelp();}elseif(ButtonScore.isClick(x,y)){TurnToScore();}elseif(ButtonDetail.isClick(x,y)){TurnToDetail();}elseif(ButtonReturn.isClick(x,y)){Is_exit=true;CloseGame();}}publicvoidonTouchEventLeverGuide(MotionEventevent)//选关界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonReturn.isClick(x,y)){TurnToMenu();}elseif(ButtonFirst.isClick(x,y)){LeverNum=1;TurnToGame();}elseif(ButtonSecond.isClick(x,y)){LeverNum=2;TurnToGame();}}publicvoidonTouchEventHelp(MotionEventevent)//帮助界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonReturn.isClick(x,y)){TurnToMenu();}}publicvoidonTouchEventScore(MotionEventevent)//帮助界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();if(ButtonReturn.isClick(x,y)){TurnToMenu();}}publicvoidonTouchEventInGame(MotionEventevent)//游戏界面的触屏事件处理{floatx,y;x=event.getX();y=event.getY();//////处理返回键///////////////////////////////if(ButtonReturn.isClick(x,y)){TurnToMenu();mediaPlayer.pause();}//////////////////////////////////////////////////////////声音处理///////////////////////////elseif(ButtonSound.isClick(x,y)){if(is_SoundOn){ButtonSound.setButtonPic(imgSoundOff);is_SoundOn=false;mediaPlayer.pause();}else{ButtonSound.setButtonPic(imgSoundOn);is_SoundOn=true; mediaPlayer.start(); mediaPlayer.setLooping(true);}}/////////////////////////////////////////////////////////////////根据触摸点的位置判断主角飞机的运动状态////////////////////if((x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)>(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)&&(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)>-(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)&&!rgSpriteCmd.IsRigtStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKRIGHT;//向右走}elseif((x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2<(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2))&&(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)<-(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)&&!rgSpriteCmd.IsLeftStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKLEFT;//向左走}elseif((y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)< (x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2) &&(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)<-(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2) &&!rgSpriteCmd.IsTopStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKUP;//向上走}elseif((y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)>=(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)&&(y-rgSpriteCmd.y-rgSpriteCmd.unHeight/2)>=-(x-rgSpriteCmd.x-rgSpriteCmd.unWidth/2)&&!rgSpriteCmd.IsDownStop&&rgSpriteCmd.CurrentState!=MainState.enHURT&&!ButtonSound.isClick(x,y)){rgSpriteCmd.CurrentState=MainState.enWALKDOWN;//向下走}}///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////publicBoolean CheckHitEnemy(SpriteCmdCmd,SpriteCmdSpCmd){if(Cmd.unLayer!=255&&SpCmd.unLayer!=255){if(Cmd.x Cmd.unWidth>SpCmd.x&&Cmd.xSpCmd.y&&Cmd.y0说明当前手机左翻x<0右翻y_sensor=event.values[1];//手机纵向翻滚//y>0说明当前手机下翻y<0上翻z_sensor=event.values[2];//屏幕的朝向//z>0手机屏幕朝上z<0手机屏幕朝下}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////退出游戏时释放所有资源///////////////////////////////////////////////////////////////////////////////////////publicvoidCloseGame(){FreeAppData();//释放应用所有资源((picture)this.getContext()).CloseActivity();//关闭应用}publicvoidFreeAppData(){//释放图片资源ReleaseImage(imgPlane);ReleaseImage(imgStar);ReleaseImage(imgStar);ReleaseImage(imgEnemy1);ReleaseImage(imgEnemy2);ReleaseImage(imgEnemy3);ReleaseImage(imgEnemy4);ReleaseImage(imgBomb);ReleaseImage(imgPassLerver);ReleaseImage(imgBloodBg);ReleaseImage(imgBlood);ReleaseImage(imgBackground);ReleaseImage(imgStart);ReleaseImage(imgHelp);ReleaseImage(imgScore);ReleaseImage(imgDetail);ReleaseImage(imgFirst);ReleaseImage(imgSecond);ReleaseImage(imgHelpDisplay);ReleaseImage(imgScoreDisplay);ReleaseImage(imgDetailDisplay);ReleaseImage(imgReturn);ReleaseImage(imgSoundOn);ReleaseImage(imgSoundOff);if(mediaPlayer!=null){mediaPlayer.stop();//停止播放声音mediaPlayer.release();//释放mediaPlayer资源mediaPlayer=null;//对象置空}//释放声音资源}publicstaticvoidFreeMedia(){if(mediaPlayer!=null){mediaPlayer.stop();//停止播放声音mediaPlayer.release();//释放mediaPlayer资源mediaPlayer=null;//对象置空}}publicstaticBitmapReleaseImage(Bitmapbitmap){if(bitmap!=null&&!bitmap.isRecycled()) {//bitmap.recycle();//用于回收该bitmap所占用的内存bitmap=null;//将bitmap置空 }returnnull;//返回空,将原图片对象置为空}} - IMDN开发者社群-imdn.cn">
首页 »
Java »
android 打飞机游戏 源码下载
于 2015-07-02 发布
0 129
import com.pic.SpriteCmd.MainState;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.media.MediaPlayer;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class PictureView extends View implements Runnable,SensorEventListener{private Context context;private int scrWidth;//屏幕的宽度private int scrHeight;//屏幕的高度private int MainTime = 0; //主计时器private Bitmap imgBackground = null;//背景图片private Bitmap imgBackground2 = null;private ButtonUtil ButtonStart = null;//开始按钮private ButtonUtil ButtonHelp = null;//帮助按钮private ButtonUtil ButtonScore = null;private ButtonUtil ButtonDetail = null;private Bitmap imgStart = null;//开始按钮对应的图片private Bitmap imgHelp = null;//帮助按钮对应的图片private Bitmap imgScore = null;private Bitmap imgDetail = null;private ButtonUtil ButtonFirst = null;//选关界面第一关对应按钮private ButtonUtil ButtonSecond = null;//选关界面第二关对应按钮private Bitmap imgFirst = null;//第一关对应图片private Bitmap imgSecond = null;//第二关对应图片private Bitmap imgHelpDisplay = null;//帮助图片private Bitmap imgScoreDisplay = null;//高分图片private Bitmap imgDetailDisplay = null;private ButtonUtil ButtonReturn = null;//返回按钮private Bitmap imgReturn = null;//返回按钮对应图片private ButtonUtil ButtonSound = null;//声音开关按钮private Bitmap imgSoundOn = null;//开声音对应图片private Bitmap imgSoundOff = null;//关声音对应图片private Boolean is_SoundOn = true;//声音是否开private Boolean is_LeverUp = false;//是否过关private int DeadEnemyCnt = 0;//敌人死亡计数器 当死亡敌人个数达到某个数时 过关private int LeverCnt = 0;//过关界面的计数器private static MediaPlayer mediaPlayer = null;//声音播放对象public enum GameState//游戏状态{GAMESTATE_MENU, //菜单GAMESTATE_LEVELGUIDE, //关数提示,显示第一关,第二关 。。GAMESTATE_HELP, GAMESTATE_SCORE,GAMESTATE_DETAIL,GAMESTATE_GAME, //游戏}public GameState GameState; // 游戏状态public static final int STAR_NUM = 30;public SpriteCmd rgSpriteCmd = new SpriteCmd();////主角SpriteCmd类对象实例化public SpriteCmd rgCmdStar[] = new SpriteCmd[30];//星星对象public SpriteCmd rgBulletCmd[] = new SpriteCmd[20];//主角子弹对象public SpriteCmd rgEnemy1Cmd = new SpriteCmd();//敌人1的SpriteCmd类对象public SpriteCmd rgEnemy2Cmd = new SpriteCmd();//敌人2对象public SpriteCmd rgEnemy3Cmd = new SpriteCmd();//敌人3对象public SpriteCmd rgEnemy4Cmd = new SpriteCmd();public SpriteCmd rgEnemy5Cmd = new SpriteCmd();public SpriteCmd rgBombCmds[] = new SpriteCmd[5];//敌人死亡时候爆炸的对象private int LeverNum;//关数计数器public Bitmap imgPlane;//主角飞机对应图片public Bitmap imgStar;//星星对应图片public Bitmap imgBullet;//子弹图片public Bitmap imgEnemy1;//敌人1对应图片public Bitmap imgEnemy2;//敌人2对象图片public Bitmap imgEnemy3;public Bitmap imgEnemy4;public Bitmap imgEnemy5;public Bitmap imgBomb; //炸弹对应图片public Bitmap imgPassLerver;//过关图片public SpriteCmd rgGameover = new SpriteCmd();public Bitmap imgBloodBg;//血量框图片public Bitmap imgBlood;//血量图片public boolean IsRightKeyRealess = true;//右键是否释放public boolean IsLeftKeyRealess = true;//左键是否释放public boolean IsUpKeyRealess = true;//上键是否释放public boolean IsDownKeyRealess = true;//下键是否释放private SensorManager sm;//传感器管理器private Sensor sensor;//重力传感器对象public float x_sensor = 0;//重力传感器x方向的位置偏移public float y_sensor = 0;//重力传感器y方向的位置偏移public float z_sensor = 0;//重力传感器z方向的位置偏移public SpriteCmd rgBackground1 = new SpriteCmd();//背景对象1public SpriteCmd rgBackground2 = new SpriteCmd();//背景对象2public SpriteCmd rgLerver = new SpriteCmd();//“恭喜过关”对象public SpriteCmd rgBloodBg = new SpriteCmd();//“血量框”对象public SpriteCmd rgBlood = new SpriteCmd();//“血量”对象public boolean Is_exit = false;//是否退出游戏public PictureView(Context context){ super(context);//调用父类的构造方法 MainTime = 0; // 主计时清零 sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);// 得到一个重力传感器实例 sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME); new Thread(this).start(); //启动线程 通过start()方法找到run()方法 this.context = context; Is_exit = false; }public void TurnToMenu()//进入菜单{imgBackground =((BitmapDrawable)getResources().getDrawable(R.drawable.bg1)).getBitmap();//背景图片if (imgStart == null){imgStart = ((BitmapDrawable)getResources().getDrawable(R.drawable.start)).getBitmap();//“开始游戏”的图片}if (imgHelp == null){imgHelp = ((BitmapDrawable)getResources().getDrawable(R.drawable.help)).getBitmap();//“游戏帮助”的图片}if (imgScore == null){imgScore = ((BitmapDrawable)getResources().getDrawable(R.drawable.score)).getBitmap();//“高分”的图}if (imgDetail == null){imgDetail = ((BitmapDrawable)getResources().getDrawable(R.drawable.detail)).getBitmap();//“信息”的图}if (imgReturn == null){imgReturn = ((BitmapDrawable)getResources().getDrawable(R.drawable.back)).getBitmap();//“返回”的图片}ButtonStart = new ButtonUtil(imgStart,(scrWidth - imgStart.getWidth())/2,100);//把“开始游戏”的图片与按钮绑定ButtonHelp = new ButtonUtil(imgHelp,(scrWidth - imgHelp.getWidth())/2,200);ButtonScore = new ButtonUtil(imgScore,(scrWidth - imgScore.getWidth())/2,300);ButtonDetail = new ButtonUtil(imgDetail,(scrWidth - imgDetail.getWidth())/2,400);ButtonReturn = new ButtonUtil(imgReturn,scrWidth - imgReturn.getWidth() - 10,scrHeight - 50);//把“返回”的图片与按钮绑定GameState = GameState.GAMESTATE_MENU; //状态转换成菜单状态}public void TurnToLeverGuide()//进入选关{ReleaseImage(imgBackground);imgBackground =((BitmapDrawable)getResources().getDrawable(R.drawable.bg2)).getBitmap();//背景图片if (imgFirst == null){imgFirst = ((BitmapDrawable)getResources().getDrawable(R.drawable.firstlever)).getBitmap();//“第1关”的图片}if (imgSecond == null){imgSecond = ((BitmapDrawable)getResources().getDrawable(R.drawable.secondlever)).getBitmap();//“第2关”的图片}ButtonFirst = new ButtonUtil(imgFirst,(scrWidth - imgFirst.getWidth())/2,150);ButtonSecond = new ButtonUtil(imgSecond,(scrWidth - imgSecond.getWidth())/2,300);GameState = GameState.GAMESTATE_LEVELGUIDE;}public void TurnToHelp()//进入帮助{if (imgHelpDisplay == null){imgHelpDisplay = ((BitmapDrawable)getResources().getDrawable(R.drawable.helpdisplay)).getBitmap();}GameState = GameState.GAMESTATE_HELP;}public void TurnToScore()//进入高分榜{if (imgScoreDisplay == null){TextView a=new TextView(context);a.setText("a");//imgScoreDisplay = ((BitmapDrawable)getResources().getDrawable(R.drawable.scoredisplay)).getBitmap();}GameState = GameState.GAMESTATE_SCORE;}public void TurnToDetail()//进入信息{if (imgDetailDisplay == null){imgDetailDisplay = ((BitmapDrawable)getResources().getDrawable(R.drawable.detaildisplay)).getBitmap();}GameState = GameState.GAMESTATE_DETAIL;}public void TurnToGame()//进入游戏{ LoadResource();InitLever();//GameState = GameState.GAMESTATE_GAME;//////////为背景图定义了SpriteCmd类的两个对象,为背景图设定两个位置同时画出来//// rgBackground1.x = 0;rgBackground1.y = -scrHeight;rgBackground1.unLayer = 0;rgBackground1.unWidth = scrWidth; //背景图的宽度设为全屏rgBackground1.unHeight = scrHeight;//背景图的宽度设为全屏rgBackground2.x = 0;rgBackground2.y = 0;rgBackground2.unLayer = 0;rgBackground2.unWidth = scrWidth;//背景图的宽度设为全屏rgBackground2.unHeight = scrHeight; //背景图的宽度设为全屏///////////////////////////////////////////////////////////// imgSoundOn =((BitmapDrawable)getResources().getDrawable(R.drawable.bgsoundon)).getBitmap();imgSoundOff =((BitmapDrawable)getResources().getDrawable(R.drawable.bgsoundoff)).getBitmap();ButtonSound = new ButtonUtil(imgSoundOn,10,scrHeight - 20);rgLerver.unWidth = 200;rgLerver.unHeight = 40;rgLerver.unLayer = 255;rgGameover.unWidth = 200;rgGameover.unHeight = 40;rgGameover.unLayer = 255;rgBloodBg.unWidth = 100;rgBloodBg.unHeight = 32;rgBloodBg.unLayer = 0;rgBloodBg.x = 0;rgBloodBg.y = 0;rgBlood.unWidth = 96;rgBlood.unHeight = 14;rgBlood.unLayer = 0;rgBlood.x = rgBloodBg.x;rgBlood.y = rgBloodBg.y 10;mediaPlayer = MediaPlayer.create(context, R.raw.game); mediaPlayer.start(); mediaPlayer.setLooping(true);is_LeverUp = false;}void InitLever(){int i;switch (LeverNum) {case 1: //第一关rgSpriteCmd.unWidth = 36;rgSpriteCmd.unHeight = 34;rgSpriteCmd.x = (scrWidth - rgSpriteCmd.unWidth)/2;rgSpriteCmd.y = scrHeight - rgSpriteCmd.unHeight;rgSpriteCmd.unLayer = 0;rgSpriteCmd.unSpriteIndex = 0;rgSpriteCmd.CurrentState = MainState.enSTAND;rgSpriteCmd.WalkCount = 0;rgSpriteCmd.IsHurt = false;rgSpriteCmd.IsLeftStop = false;rgSpriteCmd.IsRight = false;rgSpriteCmd.IsTopStop = false;rgSpriteCmd.IsDownStop = false;//敌人初始化rgEnemy1Cmd.unWidth = 35;rgEnemy1Cmd.unHeight = 30;rgEnemy1Cmd.unLayer = 255;rgEnemy3Cmd.unWidth = 35;rgEnemy3Cmd.unHeight = 30;rgEnemy3Cmd.unLayer = 255;rgEnemy3Cmd.IsRight = false;rgEnemy4Cmd.unWidth = 35;rgEnemy4Cmd.unHeight = 30;rgEnemy4Cmd.unLayer = 255;rgEnemy4Cmd.IsRight = false;break;case 2: //第二关rgSpriteCmd.unWidth = 36;rgSpriteCmd.unHeight = 34;rgSpriteCmd.x = (scrWidth - rgSpriteCmd.unWidth)/4;rgSpriteCmd.y = scrHeight - rgSpriteCmd.unHeight;rgSpriteCmd.unLayer = 0;rgSpriteCmd.unSpriteIndex = 0;rgSpriteCmd.CurrentState = MainState.enSTAND;rgSpriteCmd.WalkCount = 0;rgSpriteCmd.IsHurt = false;rgSpriteCmd.IsLeftStop = false;rgSpriteCmd.IsRight = false;rgSpriteCmd.IsTopStop = false;rgSpriteCmd.IsDownStop = false;//敌人初始化rgEnemy1Cmd.unWidth = 35;rgEnemy1Cmd.unHeight = 30;rgEnemy1Cmd.unLayer = 255;rgEnemy2Cmd.unWidth = 35;rgEnemy2Cmd.unHeight = 30;rgEnemy2Cmd.unLayer = 255;rgEnemy3Cmd.unWidth = 35;rgEnemy3Cmd.unHeight = 30;rgEnemy3Cmd.unLayer = 255;rgEnemy3Cmd.IsRight = false;rgEnemy4Cmd.unWidth = 35;rgEnemy4Cmd.unHeight = 30;rgEnemy4Cmd.unLayer = 255;rgEnemy5Cmd.unWidth = 35;rgEnemy5Cmd.unHeight = 30;rgEnemy5Cmd.unLayer = 255;rgEnemy5Cmd.IsRight = false;break;default:break;}////星星的初始化///////////////////////////////for (i = 0; i < STAR_NUM; i = 1){rgCmdStar[i] = new SpriteCmd();//每颗星星都实例化SpriteCmd类对象}for (i = 0; i < STAR_NUM; i = 2) //偶数个星星的图片索引值为0{rgCmdStar[i].unSpriteIndex = 0;}for (i = 1; i < STAR_NUM; i = 2) //基数个星星的图片索引值为1{rgCmdStar[i].unSpriteIndex = 1;}for (i = 0; i < STAR_NUM; i ){rgCmdStar[i].unWidth = 3; //每颗星星图片的宽度和高度rgCmdStar[i].unHeight = 3;rgCmdStar[i].x = scrWidth/STAR_NUM*i 10;//每颗星星图片绘制在屏幕上的位置rgCmdStar[i].y = scrHeight/STAR_NUM*i;} //////////////每颗子弹实例化SpriteCmd类对象,初始化////////for(i=0;i scrHeight) {//到达地图下边界rgSpriteCmd.IsDownStop = true;rgSpriteCmd.y = scrHeight - rgSpriteCmd.unHeight;}else {rgSpriteCmd.IsDownStop = false;}////主机与敌机的碰撞判断及处理///////////////////////////////if ((CheckHitEnemy(rgSpriteCmd, rgEnemy1Cmd) || CheckHitEnemy(rgSpriteCmd, rgEnemy2Cmd)|| CheckHitEnemy(rgSpriteCmd, rgEnemy3Cmd)) || CheckHitEnemy(rgSpriteCmd, rgEnemy4Cmd)|| CheckHitEnemy(rgSpriteCmd, rgEnemy5Cmd)&& !rgSpriteCmd.IsHurt) {rgSpriteCmd.CurrentState = MainState.enHURT;rgSpriteCmd.ProtectCount = 18;rgSpriteCmd.IsHurt = true;rgBlood.unWidth -= 19;if (rgBlood.unWidth < 0) {rgBlood.unWidth = 96;}}/////主机子弹与敌机1的碰撞////////////////////////////////for(i=0;i -1 && y_sensor < 1) {rgSpriteCmd.CurrentState = MainState.enSTAND;}else {rgSpriteCmd.unSpriteIndex = 0;rgSpriteCmd.y = 10;}}else if (rgSpriteCmd.CurrentState == MainState.enHURT) {rgSpriteCmd.unSpriteIndex = MainTime%2 - 1; rgSpriteCmd.ProtectCount--;if (rgSpriteCmd.ProtectCount == 0) {rgSpriteCmd.CurrentState = MainState.enSTAND;rgSpriteCmd.IsHurt = false;}}} ///////把图片画成全屏///////////////////////////////////////////public void DisplayImage(Canvas canvas, Bitmap bitmap,int width, int height){ if(bitmap!=null) { Rect scrRect = new Rect(); //绘制区域在屏幕上的矩形范围 scrRect.left = 0; //绘制区域左上角顶点在屏幕上的x坐标 scrRect.top = 0; //绘制区域左上角顶点在屏幕上的y坐标 scrRect.right = scrWidth;//width; //绘制区域右下角顶点在屏幕上的x坐标 scrRect.bottom = scrHeight;//height; //绘制区域右下角顶点在屏幕上的y坐标 Rect imgRect = new Rect(); //绘制区域在图片上的矩形范围 imgRect.left = 0; //绘制区域左上角顶点距离图片左上角的x坐标 imgRect.top = 0; //绘制区域左上角顶点距离图片左上角的y坐标 imgRect.right = 0 width; //绘制区域右下角顶点距离图片左上角的x坐标 imgRect.bottom = 0 height; //绘制区域右下角顶点距离图片左上角的y坐标 canvas.drawBitmap(bitmap, imgRect, scrRect, null); //绘制图片 } }/////////////////////////////////////////////////////////////////////////// ////////画精灵(相对于屏幕的相对位置)//////////////////////public void DrawSprites(Canvas canvas, Bitmap bitmap, SpriteCmd spriteCmd){if (spriteCmd.unLayer != 255) {DisplayImage(canvas, bitmap, //要画的图片spriteCmd.x, //图片左上角画在屏幕上的x位置spriteCmd.y, //图片左上角画在屏幕上的y位置spriteCmd.unWidth, //图片的宽度spriteCmd.unHeight, //图片的高度0, //要画的图片在整个图片中的x方向位置spriteCmd.unSpriteIndex * spriteCmd.unHeight); //要画的图片在整个图片中的y方向位置}}//画图片//参数依次为画布Canvas对象,位图对象,在屏幕上的x坐标,在屏幕上的y坐标,//绘制的宽度,绘制的高度,在图片上的x坐标,在图片上的y坐标public void DisplayImage(Canvas canvas, Bitmap bitmap, int scrX, int scrY, int width, int height, int imgX, int imgY){if(bitmap!=null){Rect scrRect = new Rect(); //绘制区域在屏幕上的矩形范围scrRect.left = scrX; //绘制区域左上角顶点在屏幕上的x坐标scrRect.top = scrY; //绘制区域左上角顶点在屏幕上的y坐标scrRect.right = scrX width; //绘制区域右下角顶点在屏幕上的x坐标scrRect.bottom = scrY height; //绘制区域右下角顶点在屏幕上的y坐标Rect imgRect = new Rect(); //绘制区域在图片上的矩形范围imgRect.left = imgX; //绘制区域左上角顶点距离图片左上角的x坐标imgRect.top = imgY; //绘制区域左上角顶点距离图片左上角的y坐标imgRect.right = imgX width; //绘制区域右下角顶点距离图片左上角的x坐标imgRect.bottom = imgY height; //绘制区域右下角顶点距离图片左上角的y坐标canvas.drawBitmap(bitmap, imgRect, scrRect, null); //绘制图片}}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////触屏事件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public boolean onTouchEvent(MotionEvent event){switch (event.getAction()){case MotionEvent.ACTION_DOWN: // 当触摸到屏幕IsRightKeyRealess = false;IsLeftKeyRealess = false;IsUpKeyRealess = false;IsDownKeyRealess = false;switch (GameState){case GAMESTATE_MENU:onTouchEventInMenu(event); // 菜单界面的触屏事件处理break;case GAMESTATE_LEVELGUIDE:onTouchEventLeverGuide(event);// 选关界面的触屏事件处理break;case GAMESTATE_HELP:onTouchEventHelp(event);// 帮助界面的触屏事件处理break;case GAMESTATE_SCORE:onTouchEventScore(event);// 分数界面的触屏事件处理break;case GAMESTATE_DETAIL:onTouchEventHelp(event);// 信息界面的触屏事件处理break;case GAMESTATE_GAME:onTouchEventInGame(event);// 游戏界面的触屏事件处理break;default:break;}return true;case MotionEvent.ACTION_UP:// 当手抬起,离开屏幕IsRightKeyRealess = true;IsLeftKeyRealess = true;IsUpKeyRealess = true;IsDownKeyRealess = true;return true;default:break;} return super.onTouchEvent(event);}public void onTouchEventInMenu(MotionEvent event)// 菜单界面的触屏事件处理{float x, y;x = event.getX();y = event.getY();if (ButtonStart.isClick(x, y)) {TurnToLeverGuide();}else if (ButtonHelp.isClick(x, y)) {TurnToHelp();}else if (ButtonScore.isClick(x, y)) {TurnToScore();}else if (ButtonDetail.isClick(x, y)) {TurnToDetail();}else if (ButtonReturn.isClick(x, y)) {Is_exit = true;CloseGame();}}public void onTouchEventLeverGuide(MotionEvent event)// 选关界面的触屏事件处理{float x, y;x = event.getX();y = event.getY();if (ButtonReturn.isClick(x, y)) {TurnToMenu();}else if (ButtonFirst.isClick(x, y)) {LeverNum = 1;TurnToGame();}else if (ButtonSecond.isClick(x, y)) {LeverNum = 2;TurnToGame();}}public void onTouchEventHelp(MotionEvent event)// 帮助界面的触屏事件处理{float x, y;x = event.getX();y = event.getY();if (ButtonReturn.isClick(x, y)) {TurnToMenu();}}public void onTouchEventScore(MotionEvent event)// 帮助界面的触屏事件处理{float x, y;x = event.getX();y = event.getY();if (ButtonReturn.isClick(x, y)) {TurnToMenu();}}public void onTouchEventInGame(MotionEvent event)// 游戏界面的触屏事件处理{float x, y;x = event.getX();y = event.getY();//////处理返回键/////////////////////////////// if (ButtonReturn.isClick(x, y)) {TurnToMenu();mediaPlayer.pause();}/////////////////////////////////////////// ///////////////声音处理///////////////////////////else if(ButtonSound.isClick(x, y)){if (is_SoundOn) {ButtonSound.setButtonPic(imgSoundOff);is_SoundOn = false;mediaPlayer.pause();}else {ButtonSound.setButtonPic(imgSoundOn);is_SoundOn = true; mediaPlayer.start(); mediaPlayer.setLooping(true);}}/////////////////////////////////////////////////////////////////根据触摸点的位置判断主角飞机的运动状态////////////////////if ((x- rgSpriteCmd.x - rgSpriteCmd.unWidth/2) > (y - rgSpriteCmd.y - rgSpriteCmd.unHeight / 2)&& (x- rgSpriteCmd.x - rgSpriteCmd.unWidth/2) > -(y - rgSpriteCmd.y - rgSpriteCmd.unHeight / 2)&& !rgSpriteCmd.IsRigtStop && rgSpriteCmd.CurrentState != MainState.enHURT &&!ButtonSound.isClick(x, y)){rgSpriteCmd.CurrentState = MainState.enWALKRIGHT;//向右走}else if ((x - rgSpriteCmd.x - rgSpriteCmd.unWidth / 2 < (y - rgSpriteCmd.y - rgSpriteCmd.unHeight / 2)) && (x- rgSpriteCmd.x - rgSpriteCmd.unWidth/2) < -(y - rgSpriteCmd.y - rgSpriteCmd.unHeight / 2)&& !rgSpriteCmd.IsLeftStop && rgSpriteCmd.CurrentState != MainState.enHURT &&!ButtonSound.isClick(x, y)){rgSpriteCmd.CurrentState = MainState.enWALKLEFT;//向左走}else if ((y - rgSpriteCmd.y - rgSpriteCmd.unHeight / 2) = (x - rgSpriteCmd.x - rgSpriteCmd.unWidth / 2) && (y - rgSpriteCmd.y - rgSpriteCmd.unHeight / 2) >= -(x - rgSpriteCmd.x - rgSpriteCmd.unWidth / 2)&& !rgSpriteCmd.IsDownStop && rgSpriteCmd.CurrentState != MainState.enHURT &&!ButtonSound.isClick(x, y)){rgSpriteCmd.CurrentState = MainState.enWALKDOWN;//向下走}}/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public Boolean CheckHitEnemy(SpriteCmd Cmd,SpriteCmd SpCmd) {if (Cmd.unLayer!=255 && SpCmd.unLayer != 255) {if (Cmd.x Cmd.unWidth > SpCmd.x && Cmd.x < SpCmd.x SpCmd.unWidth && Cmd.y Cmd.unHeight > SpCmd.y && Cmd.y < SpCmd.y SpCmd.unHeight) {return true;}} return false;}/////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto-generated method stub}public void onSensorChanged(SensorEvent event) {// TODO Auto-generated method stubx_sensor = event.values[0]; // 手机横向翻滚// x>0 说明当前手机左翻 x0 说明当前手机下翻 y0 手机屏幕朝上 z
下载说明:请别用迅雷下载,失败请重下,重下不扣分!