最新大学课程设计-飞机大战(精编版)_第1页
最新大学课程设计-飞机大战(精编版)_第2页
最新大学课程设计-飞机大战(精编版)_第3页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、大学课程设计- 飞机大战.精品资料湖北大学本科课程设计题目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.精品资料一项目介绍针对 java课程设计,我做了一个小游戏飞机大战,游戏代码包含到本学期所学的所有知识点。程序运行后,

2、进入到开始画面,鼠标单击开始游戏。敌机自上向下移动,随机出现,玩家机随鼠标移动并发射子弹,消灭敌机可以获得分数,随机出现小蜜蜂,消灭后可获得奖励。二概要设计2.1 资源需求此游戏需要导入图片:背景图片,开始界面,玩家飞机,敌机,小蜜蜂,子弹,暂停界面,结束界面。2.2 游戏流程显示标题界面单击鼠标游戏主界面鼠标移出单击鼠标玩家死亡暂停界面游戏结束.精品资料三 程序结构游戏界面:shootgame extends jpanel static 块:导入图片main ():创建窗口重写 paint ():画图action():鼠标事件timertask重写 run ():游戏运行的活动飞行物类:ab

3、stract flyingobject属性:x,y 坐标, image,图片长宽move(): 飞 行 物 移 动 outofbound(): 飞行物出界shootby(): 子弹击中飞行物敌机类:airplane extends flyingobject int speed:移动速度重写 move()重 写 outofbound () getscore():击中敌机后得分airplane ():初始化敌机.精品资料蜜蜂类:bee extends flyingobjectint xspeed,yspeed :移动速度int awardtype:奖励类型(双倍活力或加命)bee():初始化蜜蜂重

4、写 move()重写 outofbound ()gettype():获取奖励类型玩家飞机类:player extends flyingobjectint life , doublefire :生命,双倍火力player():初始化玩家重写 move():换图片,形成飞机的动态效果重写 outofbound ()shoot():生成子弹moveto():玩家机移动ishit ():玩家碰撞到飞行物setdoublefire():设置双倍火力adddoublefire ():奖励双倍火力addlife ():奖励生命deletelife ():减命getlife ():获取生命数.精品资料子弹类:

5、bullet extends flyingobject int speed:移动速度bullet ():初始化子弹重写 move()重写 outofbound ()四编码分析(一) shootgame 类此类继承 jpanel 类构建游戏窗口并控制游戏的运行类的成员变量:publicstaticfinalintwidth=400; /窗口宽publicstaticfinalintheight=600; /窗口高/图片属性publicstaticbufferedimageairplane;publicstaticbufferedimagebackground;publicstaticbuffer

6、edimagebee ;publicstaticbufferedimagebullet;publicstaticbufferedimagegameover ;publicstaticbufferedimageplayer0;publicstaticbufferedimageplayer1;publicstaticbufferedimagepause ;publicstaticbufferedimagestart;publicstaticfinalintdouble_fire=0; / 双倍火力的属性为0 publicstaticfinalintlife =1; /奖励生命的属性为1public

7、playerplayer=new player();/创建玩家对象privatebulletbullets=;/创建子弹对象(当前为空)privateflyingobjectflyings=;/创建飞行物对象(当前为空).精品资料publicstaticfinalintstart=0; /状态: 开始为 0 publicstaticfinalintrunning=1; /状态:运行为 1 publicstaticfinalintpause=2; /状态:暂停为 2publicstaticfinalintgame_ove=r3; /状态:游戏结束为3privateintstate=0; /当前状

8、态1. static 块静态块,在类加载时导入游戏所需的图片static tryairplane=imageio.read (shootgame. class.getresource("airplane.png");background=imageio.read (shootgame. class.getresource("backgr ound.png");bee =imageio.read (shootgame. class.getresource("bee.png");bullet=imageio.read (shootgame

9、. 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

10、. class.getresource("player0.png" );player1=imageio.read (shootgame. class.getresource("player1.p ng" );catch(exception e) e.printstacktrace();2. main ()在 main 方法中创建窗口publicstaticvoidmain(string args) jframe frame=new jframe("shootgame" ); shootgame game= new shootgame(

11、);.精品资料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(graphics g) g.drawimage(background, 0, 0,null);paintplayer(g);/画

12、玩家飞机paintflyings(g);/画飞行物paintbullets(g);/画子弹paintscore(g);/画分数paintstate(g);/画游戏状态/画每一个子弹private void paintbullets(graphics g) for ( int i=0;i< bullets . length ;i+) bullet b= bullets i;g.drawimage(b.image , b.x,b.y,null);/画飞行物(敌机,蜜蜂)privatevoidpaintflyings(graphics g) for(inti = 0; i <flying

13、s. length; i+) flyingobject flying=flyingsi;g.drawimage(flying.image ,flying.x,flying.y,null);/画玩家privatevoidpaintplayer(graphics g) g.drawimage(player. image ,player. x,player. y,null);.精品资料/画分数publicvoidpaintscore(graphics g) g.setcolor(color.red);/设置画笔颜色为红g.setfont(new font(font.sans_serif,font.b

14、old,20);/设置字体,加粗,字号g.drawstring("score:"+score , 10, 25);g.drawstring("life:"+player.getlife(), 10, 45);/画状态publicvoidpaintstate(graphics g) switch( state)casestart:g.drawimage(start, 0,0,null);break ; casepause:g.drawimage(pause , 0, 0,null);break ; casegame_ove:rg.drawimage(gam

15、eover , 0, 0,null);break ;4. action ()此方法处理鼠标响应事件:玩家机随鼠标移动,点击鼠标则游戏开始,鼠标移出则暂停游戏publicvoidaction()mouseadapter l=new mouseadapter()/鼠标移动事件publicvoidmousemoved(mouseevent e) if( state=running)intx=e.getx(); inty=e.gety();player.moveto(x,y);/鼠标点击事件:如果当前状态为start则开始游戏,如果当前状态为游戏结束则初始化所有对象,游戏重新开始publicvoidm

16、ouseclicked(mouseevent e) switch( state)casestart:.精品资料state=running; break ;casegame_ove:rflyings=new flyingobject0; player=new player();bullets=new bullet0; score =0;state=start;/鼠标移出,在当前状态为运行的情况下,改state为暂停publicvoidmouseexited(mouseevent e) if( state=running)state=pause;/鼠标移入,在当前状态为暂停的情况下,游戏继续运行p

17、ublicvoidmouseentered(mouseevent e) if( state=pause)state=running;this.addmouselistener(l);this.addmousemotionlistener(l);5. timertask.run() /游戏运行privatetimertimer;privateintinterval=10; /时间间隔, 10 毫秒intscore =0; /分数timer=new timer();/每隔 10 毫秒运行一次run 方法timer.schedule(new timertask() overridepublicvoi

18、drun() if( state=running)enteraction();/飞行物入场(敌机或蜜蜂)stepaction();/飞行物移动.精品资料shootaction();/射 击 ( 子 弹 入 场 ) bangaction();/碰撞outofboundsaction();/删除出界对象checkgameoveraction();/检查游戏结束repaint();,interval,interval);/子弹击中飞行物publicvoidbangaction() for( inti=0;i<bullets. length;i+) if(bang(bulletsi)bullet

19、 b=bulletsi;bulletsi=bullets bullets. length-1; bullets bullets. length-1=b;bullets=arrays.copyof ( bullets,bullets. length-1);/判断每一个子弹和飞行物是否碰撞publicbooleanbang(bullet b)intindex=-1;/被击中的飞行物下标for( inti=0;i<flyings. length;i+) flyingobject obj=flyingsi; if(obj.shootby(b)index=i; break ;if(index!=-

20、1)flyingobject obj=flyingsindex;/判断被击中的飞行物是什么类型,是敌机则得分,是蜜蜂则获得奖励if (obj instanceof airplane) airplane a=(airplane) obj; score +=a.getscore();if(objinstanceofbee) bee bee=(bee) obj;inttype=bee.gettype();.精品资料switch(type)casedouble_fire:player.adddoublefile();break ; caselife :player.addlife();break ;到

21、最后, 并删去1);flyingsindex=flyings flyings. length-1;flyings flyings. length-1=obj;/将击中的飞行物放flyings=arrays.copyof ( flyings,flyings. length- returntrue; else returnfalse;/删除出界飞行物publicvoidoutofboundsaction() flyingobject flyingalive=newflyingobjectflyings. length;intindex=0;/新数组的下标/将未出界的飞行物放入新的数组for(int

22、i = 0; i <flyings. length; i+) if(!flyingsi.outofbound()flyingaliveindex=flyingsi; index+;flyings=arrays.copyof (flyingalive, index);/缩小数组,将出界的飞机删除index=0;bullet bulletalive=new bulletbullets. length;/将未出界的子弹放入新的数组for(inti = 0; i <bullets. length; i+) if(!bulletsi.outofbound()bulletaliveindex=

23、bulletsi;index+;.精品资料bullets=arrays.copyof (bulletalive, index);/缩小数组,将出界的子弹删除/检查游戏是否结束publicvoidcheckgameoveraction() if(isgameover()state=game_ove;r/判断游戏结束publicbooleanisgameover()for(inti = 0; i <flyings. length; i+) intindex=-1;/被撞的飞行物下标if( player.ishit(flyingsi)player.deletelife(); player.se

24、tdoublefire(0); index=i;if(index!=-1)/将被撞的飞行物从数组中删除flyingobject obj=flyingsindex;flyingsindex=flyings flyings. length-1; flyings flyings. length-1=obj;flyings=arrays.copyof ( flyings,flyings. length-1);returnplayer.getlife()<=0;intshootindex=0; /射击频率/玩家发射子弹 (生成子弹)publicvoidshootaction() shootinde

25、x+; /300毫秒新建一组子弹if( shootindex%30=0) bullet bs=player.shoot();/将新建的子弹加入到子弹数组中.精品资料bullets=arrays.copyof ( bullets, bs. length+bullets. length);system. arraycopy(bs, 0,bullets,bullets. length- bs. length, bs.length);/子弹,玩家,飞行物走步publicvoidstepaction() for(inti = 0; i <flyings. length; i+) flyingsi.

26、move();for(inti = 0; i <bullets. length; i+) bulletsi.move();player.move();intflyenteredindex=0; /飞行物入场计数/飞行物入场(新建对象)publicvoidenteraction() flyenteredindex+; /400毫秒新建一个飞行物(敌机或蜜蜂)if( flyenteredindex%40=0) flyingobject obj=nextone (); flyings=arrays.copyof ( flyings,flyings. length+1);/扩容flyings f

27、lyings. length-1=obj;/工厂方法:生成对象publicstaticflyingobject nextone() random rand= new random();inttype=rand.nextint(20);/生成蜜蜂的概率为5% if(type=0)returnnew bee(); else returnnew airplane();.精品资料(二) flyingobject类成员变量:protectedintx; /x坐标protectedinty; /y坐标protectedintwidth; /图片宽protectedintheight; /图片长protec

28、tedbufferedimageimage ; /图片publicabstractvoidmove();/抽象方法:飞行物移动publicabstractbooleanoutofbound();/抽象方法:判断是否出界/飞行物是否被子弹击中publicbooleanshootby(bullet b) intx=b.x;inty=b.y;returnx> this. x&&x<this. x+width&&y>this. y&&y<this. y+height;(三) airplane类成员变量:privateintspee

29、d =2; /敌机移动速度publicairplane() x=( int) (math.random ()*shootgame.width); /随机生成敌机的 x 坐标y=- height;image =shootgame. airplane; width=image .getwidth(); height=image .getheight();.精品资料/敌机的移动方法(每次向下移动一个单位)publicvoidmove() /todoauto-generated method stub y+=speed ;/一个敌机的分数为5publicintgetscore() return5;/判

30、断敌机是否出界publicbooleanoutofbound() returnthis. y>shootgame. height;(四) bee 类成员变量:private int xspeed =1; / 蜜蜂在 x 轴方向的移动速度private int yspeed =2; / 蜜蜂在 y 轴方向的移动速度private int awardtype ; / 奖 励 类 型publicbee() image =shootgame. bee;x=( int) (math.random ()*shootgame.width); y=- height;width=image .getwid

31、th(); height=image .getheight();awardtype =( int) (math.random ()*2);/0或 1/获得奖励类型publicintgettype().精品资料returnawardtype ;/蜜蜂移动方法publicvoidmove() y+=yspeed ; x+=xspeed ;if( x>shootgame. width- width) xspeed =-1; elseif( x<0)xspeed =1;/判断蜜蜂是否出界publicbooleanoutofbound() returnthis. y>shootgame

32、. height;(五) player 类成员变量:privatebufferedimageimages ; /图片数组privateintindex ; /换图片计数privateintlife; /玩家生命privateintdoublefile; /双倍火力publicplayer() x=150;y=300;image =shootgame. player0; width=image .getwidth(); height=image .getheight(); images =newbufferedimageshootgame.player0,shootgame.player1; l

33、ife=3;doublefile=0;index=0;/生成子弹.精品资料20);publicbullet shoot()if( doublefile>0)bullet bs=new bullet2;bs0=new bullet(this. x+this. width/4,this. y-bs1=new bullet(this. x+3* this. width/4,this. y -20);returnbs; else bullet bs=new bullet1;bs0=new bullet(this. x+width/2,this. y); returnbs;/将玩家机移动到鼠标的位

34、置publicvoidmoveto( intx,inty) this. x=x- this. width/2;this. y=y- this. height/2;/奖励双倍活力publicvoidadddoublefile() doublefile+=40;/设置双倍火力的值publicvoidsetdoublefire(inta) doublefile=a;/获得生命的数值publicintgetlife()returnlife;/减命publicvoiddeletelife() life-;/奖励生命publicvoidaddlife() life+;.精品资料/重写 move 方法实现玩家飞机的动态效果publicvoidmove() index+;switch(index /10)%2)case 0:image =images 0;break ; case 1:image =images 1;break ;/重写 outofbound (),玩家飞机永不出界publicbooleanoutofbound() returnfalse;/玩家碰撞到飞行物publicbooleanishit(flyingobject f)if( this. x >= f.x &&am

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论