大学课程设计飞机大战_第1页
大学课程设计飞机大战_第2页
大学课程设计飞机大战_第3页
大学课程设计飞机大战_第4页
大学课程设计飞机大战_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

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

文档简介

1、2020年4月19日大学课程设计飞机大战文档仅供参考,不当之处,请联系改正。 湖北大学本科课程设计 题 目 Java课程设计飞机大战 姓 名 学 号 专业年级 指导教师 职 称 12月 18日目录项目介绍 1概要设计 2.1资源需求 1 2.2游戏流程 1类设计 3.1游戏界面类 2 3.2飞行物类 2 3.3敌机类 2 3.4蜜蜂类 3 3.5玩家飞机类 3 3.6子弹类 4编码分析 4.1游戏界面类 4 4.2飞行物类 11 4.3敌机类 12 4.4蜜蜂类 13 4.5玩家飞机类 13 4.6子弹类 15游戏测试画面 16总结 18一项目介绍针对Java课程设计,我做了一个小游戏飞机大战

2、,游戏代码包含到本学期所学的所有知识点。程序运行后,进入到开始画面,鼠标单击开始游戏。敌机自上向下移动,随机出现,玩家机随鼠标移动并发射子弹,消灭敌机能够获得分数,随机出现小蜜蜂,消灭后可获得奖励。二概要设计2.1资源需求此游戏需要导入图片:背景图片,开始界面,玩家飞机,敌机,小蜜蜂,子弹,暂停界面,结束界面。显示标题界面 2.2游戏流程 单击鼠标 游戏主界面 暂停界面 鼠标移出 单击鼠标游戏结束 玩家死亡 三程序结构 游戏界面:ShootGame extends JPanel static块:导入图片 main():创立窗口 重写paint():画图 action():鼠标事件 TimerT

3、ask重写run():游戏运行的活动飞行物类:abstract FlyingObject 属性:x,y坐标,image,图片长宽 move():飞行物移动 outOfbound():飞行物出界 shootBy():子弹击中飞行物敌机类:Airplane extends FlyingObject Int speed:移动速度 重写move() 重写outOfBound() getScore():击中敌机后得分 Airplane():初始化敌机蜜蜂类:Bee extends FlyingObject Int xSpeed,ySpeed :移动速度Int awardType:奖励类型(双倍活力或加命

4、) Bee():初始化蜜蜂 重写move() 重写outOfBound() getType():获取奖励类型玩家飞机类:Player extends FlyingObject Int life,doubleFire:生命,双倍火力 Player():初始化玩家 重写move():换图片,形成飞机的动态效果 重写outOfBound() shoot():生成子弹 moveTo():玩家机移动 isHit():玩家碰撞到飞行物 setDoubleFire():设置双倍火力 addDoubleFire():奖励双倍火力 addLife():奖励生命 deleteLife():减命 getLife()

5、:获取生命数子弹类: Bullet extends FlyingObject Int speed:移动速度 Bullet():初始化子弹 重写move() 重写outOfBound()四编码分析ShootGame类此类继承JPanel类构建游戏窗口并控制游戏的运行类的成员变量:public static final int WIDTH=400;/窗口宽public static final int HEIGHT=600;/窗口高/图片属性public static BufferedImage airplane;public static BufferedImage background;pub

6、lic static BufferedImage bee;public static BufferedImage bullet;public static BufferedImage gameover;public static BufferedImage player0;public static BufferedImage player1;public static BufferedImage pause;public static BufferedImage start;public static final int DOUBLE_FIRE=0;/双倍火力的属性为0public stat

7、ic final int LIFE=1;/奖励生命的属性为1public Player player=new Player();/创立玩家对象private Bullet bullets=;/创立子弹对象(当前为空)private FlyingObject flyings=;/创立飞行物对象(当前为空)public static final int START=0;/状态:开始为0public static final int RUNNING=1;/状态:运行为1public static final int PAUSE=2;/状态:暂停为2public static final int GA

8、ME_OVER=3;/状态:游戏结束为3private int state=0;/当前状态1.static块静态块,在类加载时导入游戏所需的图片statictry 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.cl

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

10、.class.getResource(player1.png); catch (Exception e) e.printStackTrace();main()在main方法中创立窗口public static void main(String args) JFrame frame=new JFrame(ShootGame);ShootGame game=new ShootGame();frame.add(game);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocation(400, 100);frame.set

11、AlwaysOnTop(true);frame.setVisible(true);frame.setSize(WIDTH, HEIGHT);game.action();paint()/画图(g是画笔)public void paint(Graphics g) g.drawImage(background, 0, 0, null);paintPlayer(g);/画玩家飞机paintFlyings(g);/画飞行物paintBullets(g);/画子弹paintScore(g);/画分数paintState(g);/画游戏状态/画每一个子弹private void paintBullets(G

12、raphics g) for(int i=0;ibullets.length;i+)Bullet b=bulletsi;g.drawImage(b.image, b.x,b. y, null);/画飞行物(敌机,蜜蜂)private void paintFlyings(Graphics g) for (int i = 0; i flyings.length; i+) FlyingObject flying=flyingsi;g.drawImage(flying.image,flying. x,flying. y, null);/画玩家private void paintPlayer(Graph

13、ics g) g.drawImage(player.image, player.x, player.y, null);/画分数public void paintScore(Graphics g)g.setColor(Color.RED);/设置画笔颜色为红g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20);/设置字体,加粗,字号g.drawString(Score:+score, 10, 25);g.drawString(Life:+player.getLife(), 10, 45);/画状态public void paintState(Graph

14、ics g)switch(state)case START:g.drawImage(start, 0,0,null);break;case PAUSE:g.drawImage(pause, 0, 0, null);break;case GAME_OVER:g.drawImage(gameover, 0, 0, null);break;action()此方法处理鼠标响应事件:玩家机随鼠标移动,点击鼠标则游戏开始,鼠标移出则暂停游戏public void action()MouseAdapter l=new MouseAdapter()/鼠标移动事件public void mouseMoved(M

15、ouseEvent e)if(state=RUNNING)int x=e.getX();int y=e.getY();player.moveTo(x,y);/鼠标点击事件:如果当前状态为start则开始游戏,如果当前状态为游戏结束则初始化所有对象,游戏重新开始public void mouseClicked(MouseEvent e) switch(state)case START:state=RUNNING;break;case GAME_OVER:flyings=new FlyingObject0;player=new Player();bullets=new Bullet0;score=

16、0;state=START;/鼠标移出,在当前状态为运行的情况下,改state为暂停public void mouseExited(MouseEvent e) if(state=RUNNING)state=PAUSE;/鼠标移入,在当前状态为暂停的情况下,游戏继续运行public void mouseEntered(MouseEvent e) if(state=PAUSE)state=RUNNING;this.addMouseListener(l);this.addMouseMotionListener(l);TimerTask.run()/游戏运行private Timer timer; p

17、rivate int interval=10;/时间间隔,10毫秒int score=0;/分数timer=new Timer();/每隔10毫秒运行一次run方法timer.schedule(new TimerTask() Overridepublic void run() if(state=RUNNING)enterAction();/飞行物入场(敌机或蜜蜂)stepAction();/飞行物移动shootAction();/射击(子弹入场)bangAction();/碰撞outOfBoundsAction();/删除出界对象checkGameOverAction();/检查游戏结束rep

18、aint();, interval, interval);/子弹击中飞行物public void bangAction() for(int i=0;ibullets.length;i+)if(bang(bulletsi)Bullet b=bulletsi;bulletsi=bulletsbullets.length-1;bulletsbullets.length-1=b;bullets=Arrays.copyOf(bullets, bullets.length-1);/判断每一个子弹和飞行物是否碰撞public boolean bang(Bullet b)int index=-1;/被击中的飞

19、行物下标for(int i=0;iflyings.length;i+)FlyingObject obj=flyingsi;if(obj.shootBy(b)index=i;break;if(index!=-1)FlyingObject obj=flyingsindex;/判断被击中的飞行物是什么类型,是敌机则得分,是蜜蜂则获得奖励if(obj instanceof Airplane)Airplane a=(Airplane) obj;score+=a.getScore();if(obj instanceof Bee)Bee bee=(Bee) obj;int type=bee.getType(

20、);switch(type)case DOUBLE_FIRE:player.addDoubleFile();break;case LIFE:player.addLife();break;flyingsindex=flyingsflyings.length-1;flyingsflyings.length-1=obj;/将击中的飞行物放到最后,并删去flyings=Arrays.copyOf(flyings, flyings.length-1);return true;elsereturn false;/删除出界飞行物public void outOfBoundsAction()FlyingObj

21、ect flyingAlive=new FlyingObjectflyings.length;int index=0;/新数组的下标/将未出界的飞行物放入新的数组for (int i = 0; i flyings.length; i+) if(!flyingsi.outOfBound()flyingAliveindex=flyingsi;index+;flyings=Arrays.copyOf(flyingAlive, index);/缩小数组,将出界的飞机删除index=0;Bullet bulletAlive=new Bulletbullets.length;/将未出界的子弹放入新的数组f

22、or (int i = 0; i bullets.length; i+) if(!bulletsi.outOfBound()bulletAliveindex=bulletsi;index+;bullets=Arrays.copyOf(bulletAlive, index);/缩小数组,将出界的子弹删除/检查游戏是否结束public void checkGameOverAction()if(isGameOver()state=GAME_OVER;/判断游戏结束public boolean isGameOver()for (int i = 0; i flyings.length; i+) int

23、index=-1;/被撞的飞行物下标if(player.isHit(flyingsi)player.deleteLife();player.setDoubleFire(0);index=i;if(index!=-1)/将被撞的飞行物从数组中删除FlyingObject obj=flyingsindex;flyingsindex=flyingsflyings.length-1;flyingsflyings.length-1=obj;flyings=Arrays.copyOf(flyings, flyings.length-1);return player.getLife()=0;int shoo

24、tIndex=0;/射击频率/玩家发射子弹(生成子弹)public void shootAction() shootIndex+;/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); /子弹,玩家,飞行物走步public void stepActi

25、on() for (int i = 0; i flyings.length; i+) flyingsi.move();for (int i = 0; i this.x&xthis.y&yShootGame.HEIGHT;Bee类成员变量:private int xSpeed=1;/蜜蜂在x轴方向的移动速度private int ySpeed=2;/蜜蜂在y轴方向的移动速度private int awardType;/奖励类型public Bee() image=ShootGame.bee;x=(int) (Math.random()*ShootGame.WIDTH);y=-height;wid

26、th=image.getWidth();height=image.getHeight();awardType=(int) (Math.random()*2);/0或1/获得奖励类型public int getType()return awardType;/蜜蜂移动方法public void move() y+=ySpeed;x+=xSpeed;if(xShootGame.WIDTH-width)xSpeed=-1;else if(xShootGame.HEIGHT;Player类成员变量:private BufferedImage images;/图片数组private int index;/

27、换图片计数private int life;/玩家生命private int doubleFile;/双倍火力public Player() x=150;y=300;image=ShootGame.player0;width=image.getWidth();height=image.getHeight();images=new BufferedImageShootGame.player0,ShootGame.player1;life=3;doubleFile=0;index=0;/生成子弹public Bullet shoot()if(doubleFile0)Bullet bs=new Bu

28、llet2;bs0=new Bullet(this.x+this.width/4,this.y-20);bs1=new Bullet(this.x+3*this.width/4, this.y-20);return bs;elseBullet bs=new Bullet1;bs0=new Bullet(this.x+width/2,this.y);return bs;/将玩家机移动到鼠标的位置public void moveTo(int x,int y) this.x=x-this.width/2;this.y=y-this.height/2;/奖励双倍活力public void addDou

29、bleFile()doubleFile+=40;/设置双倍火力的值public void setDoubleFire(int a)doubleFile=a;/获得生命的数值public int getLife()return life;/减命public void deleteLife()life-;/奖励生命public void addLife()life+;/重写move方法实现玩家飞机的动态效果public void move() index+;switch(index/10)%2)case 0:image=images0;break;case 1:image=images1;break;/重写outOfBound(),玩家飞机永不出界public boolean outOfBound() return false;/玩家碰撞到飞行物public boolean isHit(FlyingObject f)if (this.x = f.x & this.x = f.x + f.width) return false; else if (this.x= f

温馨提示

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

评论

0/150

提交评论