湖北大学本科课程设计题目Java课程设计——飞机大战姓名学号专业年级指导教师职称2015年12月18日----目录----一.项目介绍--------------------------------1二.概要设计2.1资源需求------------------------------12.2游戏流程------------------------------1三.类设计3.1游戏界面类----------------------------23.2飞行物类------------------------------23.3敌机类--------------------------------23.4蜜蜂类--------------------------------33.5玩家飞机类-----------------------------33.6子弹类--------------------------------4四.编码分析4.1游戏界面类----------------------------44.2飞行物类------------------------------114.3敌机类--------------------------------124.4蜜蜂类--------------------------------134.5玩家飞机类-----------------------------134.6子弹类--------------------------------15五.游戏测试画面-----------------------------16六.总结------------------------------------18-1-游戏主界面一.项目介绍针对Java课程设计,我做了一个小游戏——飞机大战,游戏代码包含到本学期所学的所有知识点。程序运行后,进入到开始画面,鼠标单击开始游戏。敌机自上向下移动,随机出现,玩家机随鼠标移动并发射子弹,消灭敌机可以获得分数,随机出现小蜜蜂,消灭后可获得奖励。二.概要设计2.1资源需求此游戏需要导入图片:背景图片,开始界面,玩家飞机,敌机,小蜜蜂,子弹,暂停界面,结束界面。2.2游戏流程单击鼠标鼠标移出单击鼠标玩家死亡显示标题界面暂停界面游戏结束-2-三.程序结构游戏界面:ShootGameextendsJPanelstatic块:导入图片main():创建窗口重写paint():画图action():鼠标事件TimerTask重写run():游戏运行的活动飞行物类:abstractFlyingObject属性:x,y坐标,image,图片长宽move():飞行物移动outOfbound():飞行物出界shootBy():子弹击中飞行物敌机类:AirplaneextendsFlyingObjectIntspeed:移动速度重写move()重写outOfBound()getScore():击中敌机后得分Airplane():初始化敌机-3-蜜蜂类:BeeextendsFlyingObjectIntxSpeed,ySpeed:移动速度IntawardType:奖励类型(双倍活力或加命)Bee():初始化蜜蜂重写move()重写outOfBound()getType():获取奖励类型玩家飞机类:PlayerextendsFlyingObjectIntlife,doubleFire:生命,双倍火力Player():初始化玩家重写move():换图片,形成飞机的动态效果重写outOfBound()shoot():生成子弹moveTo():玩家机移动isHit():玩家碰撞到飞行物setDoubleFire():设置双倍火力addDoubleFire():奖励双倍火力addLife():奖励生命deleteLife():减命getLife():获取生命数-4-子弹类:BulletextendsFlyingObjectIntspeed:移动速度Bullet():初始化子弹重写move()重写outOfBound()四.编码分析(一)ShootGame类此类继承JPanel类构建游戏窗口并控制游戏的运行类的成员变量:publicstaticfinalintWIDTH=400;//窗口宽publicstaticfinalintHEIGHT=600;//窗口高//图片属性publicstaticBufferedImageairplane;publicstaticBufferedImagebackground;publicstaticBufferedImagebee;publicstaticBufferedImagebullet;publicstaticBufferedImagegameover;publicstaticBufferedImageplayer0;publicstaticBufferedImageplayer1;publicstaticBufferedImagepause;publicstaticBufferedImagestart;publicstaticfinalintDOUBLE_FIRE=0;//双倍火力的属性为0publicstaticfinalintLIFE=1;//奖励生命的属性为1publicPlayerplayer=newPlayer();//创建玩家对象privateBullet[]bullets={};//创建子弹对象(当前为空)privateFlyingObject[]flyings={};//创建飞行物对象(当前为空)publicstaticfinalintSTART=0;//状态:开始为0publicstaticfinalintRUNNING=1;//状态:运行为1publicstaticfinalintPAUSE=2;//状态:暂停为2publicstaticfinalintGAME_OVER=3;//状态:游戏结束为3privateintstate=0;//当前状态1.static块-5-静态块,在类加载时导入游戏所需的图片static{try{airplane=ImageIO.read(ShootGame.class.getResource(airplane.png));background=ImageIO.read(ShootGame.class.getResource(background.png));bee=ImageIO.read(ShootGame.class.getResource(bee.png));bullet=ImageIO.read(ShootGame.class.getResource(bullet.png));gameover=ImageIO.read(ShootGame.class.getResource(gameover.png));pause=ImageIO.read(ShootGame.class.getResource(pause.png));start=ImageIO.read(ShootGame.class.getResource(start.png));player0=ImageIO.read(ShootGame.class.getResource(player0.png));player1=ImageIO.read(ShootGame.class.getResource(player1.png));}catch(Exceptione){e.printStackTrace();}}2.main()在main方法中创建窗口publicstaticvoidmain(String[]args){JFrameframe=newJFrame(ShootGame);ShootGamegame=newShootGame();frame.add(game);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocation(400,100);frame.setAlwaysOnTop(true);frame.setVisible(true);frame.setSize(WIDTH,HEIGHT);game.action();}3.paint()//画图(g是画笔)publicvoidpaint(Graphicsg){-6-g.drawImage(background,0,0,null);paintPlayer(g);//画玩家飞机paintFlyings(g);//画飞行物paintBullets(g);//画子弹paintScore(g);//画分数paintState(g);//画游戏状态}//画每一个子弹privatevoidpaintBullets(Graphicsg){for(inti=0;ibullets.length;i++){Bulletb=bullets[i];g.drawImage(b.image,b.x,b.y,null);}}//画飞行物(敌机,蜜蜂)privatevoidpaintFlyings(Graphicsg){for(inti=0;iflyings.length;i++){FlyingObjectflying=flyings[i];g.drawImage(flying.image,flying.x,flying.y,null);}}//画玩家privatevoidpaintPlayer(Graphicsg){g.drawImage(player.image,player.x,player.y,null);}//画分数publicvoidpaintScore(Graphicsg){g.setColor(Color.RED);//设置画笔颜色为红g.setFont(newFont(Font.SANS_SERIF,Font.BOLD,20));//设置字体,加粗,字号g.drawString(Score:+score,10,25);g.drawString(Life:+player.getLife(),10,45);}//画状态publicvoidpaintState(Graphicsg){switch(state){caseSTART:g.drawImage(start,0,0,null);break;casePAUSE:g.drawImage(pause,0,0,null);break;caseGAME_OVER:g.drawImage(gameover,0,0,null);break;}}-7-4.action()此方法处理鼠标响应事件:玩家机随鼠标移动,点击鼠标则游戏开始,鼠标移出则暂停游戏publicvoidaction(){MouseAdapterl=newMouseAdapter(){//鼠标移动事件publicvoidmouseMoved(MouseEvente){if(state==RUNNING){intx=e.getX();inty=e.getY();player.moveTo(x,y);}}//鼠标点击事件:如果当前状态为start则开始游戏,如果当前状态为游戏结束则初始化所有对象,游戏重新开始publicvoidmouseClicked(MouseEvente){switch(state){caseSTART:state=RUNNING;break;caseGAME_OVER:flyings=newFlyingObject[0];player=newPlayer();bullets=newBullet[0];score=0;state=START;}}//鼠标移出,在当前状态为运行的情况下,改state为暂停publicvoidmouseExited(MouseEvente){if(state==RUNNING){state=PAUSE;}}//鼠标移入,在当前状态为