大學課程設計-飛機大戰_第1頁
大學課程設計-飛機大戰_第2頁
大學課程設計-飛機大戰_第3頁
大學課程設計-飛機大戰_第4頁
大學課程設計-飛機大戰_第5頁
已閱讀5頁,還剩18頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優質文檔-傾情為你奉上 湖北大學本科課程設計 題 目 Java課程設計飛機大戰 姓 名 學 號 專業年級 指導教師 職 稱 2015年 12月 18日專心-專注-專業-目錄-1 項目介紹- 12 概要設計 2.1資源需求- 1 2.2游戲流程- 13 類設計 3.1游戲界面類- 2 3.2飛行物類- 2 3.3敵機類- 2 3.4蜜蜂類- 3 3.5玩家飛機類- 3 3.6子彈類- 44 編碼分析 4.1游戲界面類- 4 4.2飛行物類- 11 4.3敵機類- 12 4.4蜜蜂類- 13 4.5玩家飛機類- 13 4.6子彈類- 155 游戲測試畫面- 166 總結- 18一項目介紹針對J

2、ava課程設計,我做了一個小游戲飛機大戰,游戲代碼包含到本學期所學的所有知識點。程序運行后,進入到開始畫面,鼠標單擊開始游戲。敵機自上向下移動,隨機出現,玩家機隨鼠標移動并發射子彈,消滅敵機可以獲得分數,隨機出現小蜜蜂,消滅后可獲得獎勵。二概要設計2.1資源需求此游戲需要導入圖片:背景圖片,開始界面,玩家飛機,敵機,小蜜蜂,子彈,暫停界面,結束界面。顯示標題界面 2.2游戲流程 單擊鼠標 游戲主界面 暫停界面 鼠標移出 單擊鼠標游戲結束 玩家死亡 三程序結構 游戲界面:ShootGame extends JPanel static塊:導入圖片 main():創建窗口 重寫paint():畫圖

3、action():鼠標事件 TimerTask重寫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 aw

4、ardType:獎勵類型(雙倍活力或加命) Bee():初始化蜜蜂 重寫move() 重寫outOfBound() getType():獲取獎勵類型玩家飛機類:Player extends FlyingObject Int life,doubleFire:生命,雙倍火力 Player():初始化玩家 重寫move():換圖片,形成飛機的動態效果 重寫outOfBound() shoot():生成子彈 moveTo():玩家機移動 isHit():玩家碰撞到飛行物 setDoubleFire():設置雙倍火力 addDoubleFire():獎勵雙倍火力 addLife():獎勵生命 delet

5、eLife():減命 getLife():獲取生命數子彈類: Bullet extends FlyingObject Int speed:移動速度 Bullet():初始化子彈 重寫move() 重寫outOfBound()四編碼分析(1) ShootGame類此類繼承JPanel類構建游戲窗口并控制游戲的運行類的成員變量:public static final int WIDTH=400;/窗口寬public static final int HEIGHT=600;/窗口高/圖片屬性public static BufferedImage airplane;public static Buff

6、eredImage background;public 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

7、=0;/雙倍火力的屬性為0public static 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;/狀態:暫停為2pu

8、blic static final int GAME_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.getRe

9、source("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("s

10、tart.png");player0=ImageIO.read(ShootGame.class.getResource("player0.png");player1=ImageIO.read(ShootGame.class.getResource("player1.png"); catch (Exception e) e.printStackTrace();2. main()在main方法中創建窗口public static void main(String args) JFrame frame=new JFrame("ShootGa

11、me");ShootGame game=new ShootGame();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是畫筆)public void paint(Graphics g) g.drawImage(backgrou

12、nd, 0, 0, null);paintPlayer(g);/畫玩家飛機paintFlyings(g);/畫飛行物paintBullets(g);/畫子彈paintScore(g);/畫分數paintState(g);/畫游戲狀態/畫每一個子彈private void paintBullets(Graphics g) for(int i=0;i<bullets.length;i+)Bullet b=bulletsi;g.drawImage(b.image, b.x,b. y, null);/畫飛行物(敵機,蜜蜂)private void paintFlyings(Graphics g)

13、 for (int i = 0; i < flyings.length; i+) FlyingObject flying=flyingsi;g.drawImage(flying.image,flying. x,flying. y, null);/畫玩家private void paintPlayer(Graphics g) g.drawImage(player.image, player.x, player.y, null);/畫分數public void paintScore(Graphics g)g.setColor(Color.RED);/設置畫筆顏色為紅g.setFont(new

14、 Font(Font.SANS_SERIF,Font.BOLD,20);/設置字體,加粗,字號g.drawString("Score:"+score, 10, 25);g.drawString("Life:"+player.getLife(), 10, 45);/畫狀態public void paintState(Graphics g)switch(state)case START:g.drawImage(start, 0,0,null);break;case PAUSE:g.drawImage(pause, 0, 0, null);break;case

15、 GAME_OVER:g.drawImage(gameover, 0, 0, null);break;4. action()此方法處理鼠標響應事件:玩家機隨鼠標移動,點擊鼠標則游戲開始,鼠標移出則暫停游戲public void action()MouseAdapter l=new MouseAdapter()/鼠標移動事件public void mouseMoved(MouseEvent e)if(state=RUNNING)int x=e.getX();int y=e.getY();player.moveTo(x,y);/鼠標點擊事件:如果當前狀態為start則開始游戲,如果當前狀態為游戲結

16、束則初始化所有對象,游戲重新開始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=0;state=START;/鼠標移出,在當前狀態為運行的情況下,改state為暫停public void mouseExited(MouseEvent e) if(state=RUNNING)state=PAUSE;/鼠標移入

17、,在當前狀態為暫停的情況下,游戲繼續運行public void mouseEntered(MouseEvent e) if(state=PAUSE)state=RUNNING;this.addMouseListener(l);this.addMouseMotionListener(l);5. TimerTask.run()/游戲運行private Timer timer; private int interval=10;/時間間隔,10毫秒int score=0;/分數timer=new Timer();/每隔10毫秒運行一次run方法timer.schedule(new TimerTask(

18、) Overridepublic void run() if(state=RUNNING)enterAction();/飛行物入場(敵機或蜜蜂)stepAction();/飛行物移動shootAction();/射擊(子彈入場)bangAction();/碰撞outOfBoundsAction();/刪除出界對象checkGameOverAction();/檢查游戲結束repaint();, interval, interval);/子彈擊中飛行物public void bangAction() for(int i=0;i<bullets.length;i+)if(bang(bullet

19、si)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;/被擊中的飛行物下標for(int i=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(obj instanceof Bee)Bee bee=(Bee) obj;int type=bee.getType();switch(type)case DOUBLE_FIRE:player.addDoubleFile();break;case LIFE:player.addLife();break;flyingsind

21、ex=flyingsflyings.length-1;flyingsflyings.length-1=obj;/將擊中的飛行物放到最后,并刪去flyings=Arrays.copyOf(flyings, flyings.length-1);return true;elsereturn false;/刪除出界飛行物public void outOfBoundsAction()FlyingObject flyingAlive=new FlyingObjectflyings.length;int index=0;/新數組的下標/將未出界的飛行物放入新的數組for (int i = 0; i <

22、 flyings.length; i+) if(!flyingsi.outOfBound()flyingAliveindex=flyingsi;index+;flyings=Arrays.copyOf(flyingAlive, index);/縮小數組,將出界的飛機刪除index=0;Bullet bulletAlive=new Bulletbullets.length;/將未出界的子彈放入新的數組for (int i = 0; i < bullets.length; i+) if(!bulletsi.outOfBound()bulletAliveindex=bulletsi;index

23、+;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 index=-1;/被撞的飛行物下標if(player.isHit(flyingsi)player.deleteLife();player.setDoubleFire(0);in

24、dex=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 shootIndex=0;/射擊頻率/玩家發射子彈(生成子彈)public void shootAction() shootIndex+;/300毫秒新建一組子彈 if(shoo

25、tIndex%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 stepAction() for (int i = 0; i < flyings.length; i+) flyingsi.move();for (int i = 0; i <

26、;bullets.length; i+) bulletsi.move();player.move();int flyEnteredIndex=0;/飛行物入場計數/飛行物入場(新建對象)public void enterAction() flyEnteredIndex+;/400毫秒新建一個飛行物(敵機或蜜蜂)if(flyEnteredIndex%40=0)FlyingObject obj=nextOne();flyings=Arrays.copyOf(flyings, flyings.length+1);/擴容flyingsflyings.length-1=obj;/工廠方法:生成對象pub

27、lic static FlyingObject nextOne()Random rand=new Random();int type=rand.nextInt(20);/生成蜜蜂的概率為5%if(type=0)return new Bee();elsereturn new Airplane();(2) FlyingObject類成員變量:protected int x;/x坐標protected int y;/y坐標protected int width;/圖片寬protected int height;/圖片長protected BufferedImage image;/圖片public a

28、bstract void move();/抽象方法:飛行物移動public abstract boolean outOfBound();/抽象方法:判斷是否出界/飛行物是否被子彈擊中public boolean shootBy(Bullet b)int x=b.x;int y=b.y;return x>this.x&&x<this.x+width&&y>this.y&&y<this.y+height;(3) Airplane類成員變量:private int speed=2;/敵機移動速度public Airplane()

29、 x=(int) (Math.random()*ShootGame.WIDTH);/隨機生成敵機的x坐標y=-height;image=ShootGame.airplane;width=image.getWidth();height=image.getHeight();/敵機的移動方法(每次向下移動一個單位)public void move() / TODO Auto-generated method stuby+=speed;/一個敵機的分數為5public int getScore()return 5;/判斷敵機是否出界public boolean outOfBound() return

30、this.y>ShootGame.HEIGHT;(4) 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;width=image.getWidth();height=image.getHeight();awardType=(int) (Math.random()*2);

31、/0或1/獲得獎勵類型public int getType()return awardType;/蜜蜂移動方法public void move() y+=ySpeed;x+=xSpeed;if(x>ShootGame.WIDTH-width)xSpeed=-1;else if(x<0)xSpeed=1;/判斷蜜蜂是否出界public boolean outOfBound() return this.y>ShootGame.HEIGHT;(5) Player類成員變量:private BufferedImage images;/圖片數組private int index;/換圖

32、片計數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(doubleFile>0)Bullet bs=new

33、Bullet2;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 addD

34、oubleFile()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 &&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論