




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上 湖北大學(xué)本科課程設(shè)計 題 目 Java課程設(shè)計飛機大戰(zhàn) 姓 名 學(xué) 號 專業(yè)年級 指導(dǎo)教師 職 稱 2015年 12月 18日專心-專注-專業(yè)-目錄-1 項目介紹- 12 概要設(shè)計 2.1資源需求- 1 2.2游戲流程- 13 類設(shè)計 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 總結(jié)- 18一項目介紹針對J
2、ava課程設(shè)計,我做了一個小游戲飛機大戰(zhàn),游戲代碼包含到本學(xué)期所學(xué)的所有知識點。程序運行后,進入到開始畫面,鼠標(biāo)單擊開始游戲。敵機自上向下移動,隨機出現(xiàn),玩家機隨鼠標(biāo)移動并發(fā)射子彈,消滅敵機可以獲得分數(shù),隨機出現(xiàn)小蜜蜂,消滅后可獲得獎勵。二概要設(shè)計2.1資源需求此游戲需要導(dǎo)入圖片:背景圖片,開始界面,玩家飛機,敵機,小蜜蜂,子彈,暫停界面,結(jié)束界面。顯示標(biāo)題界面 2.2游戲流程 單擊鼠標(biāo) 游戲主界面 暫停界面 鼠標(biāo)移出 單擊鼠標(biāo)游戲結(jié)束 玩家死亡 三程序結(jié)構(gòu) 游戲界面:ShootGame extends JPanel static塊:導(dǎo)入圖片 main():創(chuàng)建窗口 重寫paint():畫圖
3、action():鼠標(biāo)事件 TimerTask重寫run():游戲運行的活動飛行物類:abstract FlyingObject 屬性:x,y坐標(biāo),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():換圖片,形成飛機的動態(tài)效果 重寫outOfBound() shoot():生成子彈 moveTo():玩家機移動 isHit():玩家碰撞到飛行物 setDoubleFire():設(shè)置雙倍火力 addDoubleFire():獎勵雙倍火力 addLife():獎勵生命 delet
5、eLife():減命 getLife():獲取生命數(shù)子彈類: Bullet extends FlyingObject Int speed:移動速度 Bullet():初始化子彈 重寫move() 重寫outOfBound()四編碼分析(1) ShootGame類此類繼承JPanel類構(gòu)建游戲窗口并控制游戲的運行類的成員變量: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();/創(chuàng)建玩家對象private Bullet bullets=;/創(chuàng)建子彈對象(當(dāng)前為空)private FlyingObject flyings=;/創(chuàng)建飛行物對象(當(dāng)前為空)public static final int START=0;/狀態(tài):開始為0public static final int RUNNING=1;/狀態(tài):運行為1public static final int PAUSE=2;/狀態(tài):暫停為2pu
8、blic static final int GAME_OVER=3;/狀態(tài):游戲結(jié)束為3private int state=0;/當(dāng)前狀態(tài)1.static塊靜態(tài)塊,在類加載時導(dǎo)入游戲所需的圖片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方法中創(chuàng)建窗口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);/畫分數(shù)paintState(g);/畫游戲狀態(tài)/畫每一個子彈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);/畫分數(shù)public void paintScore(Graphics g)g.setColor(Color.RED);/設(shè)置畫筆顏色為紅g.setFont(new
14、 Font(Font.SANS_SERIF,Font.BOLD,20);/設(shè)置字體,加粗,字號g.drawString("Score:"+score, 10, 25);g.drawString("Life:"+player.getLife(), 10, 45);/畫狀態(tài)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()此方法處理鼠標(biāo)響應(yīng)事件:玩家機隨鼠標(biāo)移動,點擊鼠標(biāo)則游戲開始,鼠標(biāo)移出則暫停游戲public void action()MouseAdapter l=new MouseAdapter()/鼠標(biāo)移動事件public void mouseMoved(MouseEvent e)if(state=RUNNING)int x=e.getX();int y=e.getY();player.moveTo(x,y);/鼠標(biāo)點擊事件:如果當(dāng)前狀態(tài)為start則開始游戲,如果當(dāng)前狀態(tài)為游戲結(jié)
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;/鼠標(biāo)移出,在當(dāng)前狀態(tài)為運行的情況下,改state為暫停public void mouseExited(MouseEvent e) if(state=RUNNING)state=PAUSE;/鼠標(biāo)移入
17、,在當(dāng)前狀態(tài)為暫停的情況下,游戲繼續(xù)運行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;/分數(shù)timer=new Timer();/每隔10毫秒運行一次run方法timer.schedule(new TimerTask(
18、) Overridepublic void run() if(state=RUNNING)enterAction();/飛行物入場(敵機或蜜蜂)stepAction();/飛行物移動shootAction();/射擊(子彈入場)bangAction();/碰撞outOfBoundsAction();/刪除出界對象checkGameOverAction();/檢查游戲結(jié)束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;/被擊中的飛行物下標(biāo)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;/新數(shù)組的下標(biāo)/將未出界的飛行物放入新的數(shù)組for (int i = 0; i <
22、 flyings.length; i+) if(!flyingsi.outOfBound()flyingAliveindex=flyingsi;index+;flyings=Arrays.copyOf(flyingAlive, index);/縮小數(shù)組,將出界的飛機刪除index=0;Bullet bulletAlive=new Bulletbullets.length;/將未出界的子彈放入新的數(shù)組for (int i = 0; i < bullets.length; i+) if(!bulletsi.outOfBound()bulletAliveindex=bulletsi;index
23、+;bullets=Arrays.copyOf(bulletAlive, index);/縮小數(shù)組,將出界的子彈刪除/檢查游戲是否結(jié)束public void checkGameOverAction()if(isGameOver()state=GAME_OVER;/判斷游戲結(jié)束public boolean isGameOver()for (int i = 0; i <flyings.length; i+) int index=-1;/被撞的飛行物下標(biāo)if(player.isHit(flyingsi)player.deleteLife();player.setDoubleFire(0);in
24、dex=i;if(index!=-1)/將被撞的飛行物從數(shù)組中刪除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;/射擊頻率/玩家發(fā)射子彈(生成子彈)public void shootAction() shootIndex+;/300毫秒新建一組子彈 if(shoo
25、tIndex%30=0) Bullet bs=player.shoot();/將新建的子彈加入到子彈數(shù)組中 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;/飛行物入場計數(shù)/飛行物入場(新建對象)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坐標(biāo)protected int y;/y坐標(biāo)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坐標(biāo)y=-height;image=ShootGame.airplane;width=image.getWidth();height=image.getHeight();/敵機的移動方法(每次向下移動一個單位)public void move() / TODO Auto-generated method stuby+=speed;/一個敵機的分數(shù)為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;/圖片數(shù)組private int index;/換圖
32、片計數(shù)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;/將玩家機移動到鼠標(biāo)的位置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;/設(shè)置雙倍火力的值public void setDoubleFire(int a)doubleFile=a;/獲得生命的數(shù)值public int getLife()return life;/減命public void deleteLife()life-;/獎勵生命public void addLife()life+;/重寫move方法實現(xiàn)玩家飛機的動態(tài)效果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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣告管理案例評析(一)
- 2025年工業(yè)互聯(lián)網(wǎng)平臺TEE在智能工廠設(shè)備維護中的應(yīng)用分析報告
- 攪拌站業(yè)務(wù)人員管理制度
- picc管維護管理制度
- 山西村集體用工管理制度
- 三級子公司工資管理制度
- 學(xué)院圖書館采編管理制度
- 2025年福建省中考道德與法治真題(解析版)
- 星級管理動態(tài)管理制度
- 為規(guī)范公司印章管理制度
- 健康管理中心崗位職責(zé)與要求
- 中國肥胖及代謝疾病外科治療指南(2024版)解讀
- 2025年西師新版四年級英語下冊階段測試試卷
- 體育場所應(yīng)急預(yù)案
- 南開區(qū)2024-2025學(xué)年七年級上學(xué)期期末道德與法治試題
- 《電磁兼容性(EMC)培訓(xùn)》課件
- 孕期預(yù)防產(chǎn)后出血
- 痛風(fēng)性關(guān)節(jié)炎護理查房課件
- 國家開放大學(xué)本科《商務(wù)英語4》一平臺機考真題及答案(第五套)
- 2025陜西西安亮麗電力集團限責(zé)任公司招聘55人高頻重點提升(共500題)附帶答案詳解
- 采煤概論知到智慧樹章節(jié)測試課后答案2024年秋華北科技學(xué)院
評論
0/150
提交評論