Java飞机大战项目
生活随笔
收集整理的這篇文章主要介紹了
Java飞机大战项目
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ?飛機大戰(zhàn)游戲是一款十分有趣的射擊類小游戲,流暢的畫面,高難度的挑戰(zhàn)。游戲中,玩家駕駛英雄機,在空中進行戰(zhàn)斗。點擊并移動自己的英雄機,發(fā)射炮彈,打掉敵飛機以及蜜蜂,來獲得分數(shù)和獎勵,打掉一架敵飛機贏得5分,打掉一只蜜蜂贏得1條命或是獲得20次雙倍火力,如果撞上敵飛機或小蜜蜂,將減少命、雙倍火力清零。每撞到一次蜜蜂或是敵飛機命減1,當命數(shù)為0時,則游戲結束。游戲界面如下圖所示
下面就是教大家怎么用java來寫出這個項目來。
數(shù)據(jù)建模:使用一個數(shù)據(jù)模型,描述對象的關系。使用繪圖坐標系作為參考模型,英雄機、敵飛機、蜜蜂、子彈都是矩形區(qū)域。
類的設計
完整代碼:
Airplane類的完整代碼如下所示:
package com.itkzhan.shoot;import java.util.Random;/** 敵機*/ public class Airplane extends FlyingObject implements Enemy{private int speed=2;//走步的步數(shù)public Airplane(){image=ShootGame.airplane;width=image.getWidth();height=image.getHeight();Random rand=new Random();x=rand.nextInt(ShootGame.WIDTH-this.width);y=-height;//y坐標}public int getScore() {return 5;}public void step() {y+=speed;}public boolean outBounds() {return this.y>ShootGame.HEIGHT;}}Award類的完整代碼如下所示:
package com.itkzhan.shoot; /** 獎勵*/ public interface Award {int DOUBLE_FIRE=0;//雙倍火力值int LIFE=1;//命int getType();//得獎勵類型(0或1) }Bee類的完整代碼如下所示:
package com.itkzhan.shoot;import java.util.Random;/** 蜜蜂*/ public class Bee extends FlyingObject implements Award{private int xSpeed=1;//x坐標走步步數(shù)private int ySpeed=2;//y坐標走步步數(shù)private int awardType;//獎勵類型/** Bee構造方法*/public Bee(){image=ShootGame.bee;//圖片width=image.getWidth();//圖片的寬height=image.getHeight();//圖片的高Random rand=new Random();x=rand.nextInt(ShootGame.WIDTH-this.width);y=-this.height;awardType=rand.nextInt(2);}public int getType() {//獎勵類型return awardType;}public void step() {x+=xSpeed;y+=ySpeed;if(this.x>=ShootGame.WIDTH-this.width){xSpeed=-1;}if(x<=0){xSpeed=1;}}public boolean outBounds() {return this.y>ShootGame.HEIGHT;}}Bullet類的完整代碼如下所示:
package com.itkzhan.shoot; /** 子彈*/ public class Bullet extends FlyingObject{private int speed=3;//走步的步數(shù)/** Bullet構造方法*/public Bullet(int x,int y){image=ShootGame.bullet;width=image.getWidth();height=image.getHeight();this.x=x;//x坐標this.y=y;//y坐標}public void step() {y-=speed;}public boolean outBounds() {return this.y<-height;} }Enemy類的完整代碼如下所示:
package com.itkzhan.shoot;public interface Enemy {int getScore(); }FlyingObject類的完整代碼如下所示:
package com.itkzhan.shoot;import java.awt.image.BufferedImage;/*** 飛行物* @author HP**/ public abstract class FlyingObject {protected int x;protected int y;protected int width;protected int height;protected BufferedImage image;public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public BufferedImage getImage() {return image;}public void setImage(BufferedImage image) {this.image = image;}public abstract void step();//敵人被子彈打public boolean shootBy(Bullet bullet){int x=bullet.x;//子彈的xint y=bullet.y;//子彈的yreturn x>this.x&&x<this.x+this.width&&y>this.y&&y<this.y+this.height;};public abstract boolean outBounds(); }Hero類的完整代碼如下所示:
package com.itkzhan.shoot;import java.awt.image.BufferedImage;/** 英雄機:是飛行物*/ public class Hero extends FlyingObject{private int life;//命private int doubleFire;//火力值private BufferedImage[] images;//圖片private int index;//圖片切換的頻率/** Hero構造方法*/public Hero(){image=ShootGame.hero0;//圖片width=image.getWidth();//寬height=image.getHeight();//高x=150;//固定150y=400;//固定的400life=3;//命值為3doubleFire=0;//火力值為0,單倍火力images=new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};index=0;}public void step() {index++;int a=index/10;//每100m b=0,1int b=a%2;image=images[b];image=images[b];}/** 發(fā)射子彈*/public Bullet[] shoot(){int xStep=this.width/4;//1/4英雄機的寬度int yStep=20;//子彈離飛機的距離if(doubleFire>0){//雙倍火力Bullet[] bullets=new Bullet[2];bullets[0]=new Bullet(this.x+xStep,this.y-yStep);bullets[1]=new Bullet(this.x+3*xStep,this.y-yStep);doubleFire-=-2;return bullets;}else{//單倍火力Bullet[] bullets=new Bullet[1];bullets[0]=new Bullet(this.x+2*xStep,this.y-yStep);return bullets;}}/** 英雄機隨著鼠標移動x:鼠標的x坐標 y鼠標的y坐標*/public void moveTo(int x,int y){this.x=x-this.width/2;this.y=y-this.height/2;}/**獲取命*/public int getLife(){return life;}/** 減命*/public void subtractLife(){life--;}public void addLife(){life++;}/** 增火力值*/public void addDoubleFire(){doubleFire+=40;}/** 設置火力值*/public void setDoubleFire(int doubleFire){this.doubleFire=doubleFire;}@Overridepublic boolean outBounds() {return false;//永不越界}public boolean hit(FlyingObject other){int x1=other.x-this.width/2;int x2=other.x+other.width+this.width/2;int y1=other.y-this.height/2;int y2=other.y+other.height/2;int hx=this.x+this.width/2;// int hy=this.y+this.height/2;return hx>x1 && hx<x2&&hy>y1 && hy<y2;}}ShootGame類的完整代碼如下所示:
package com.itkzhan.shoot;import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.util.Arrays; import java.util.Random; import java.util.Timer; import java.util.TimerTask;import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel;public class ShootGame extends JPanel{//面板public static final int WIDTH=400;public static final int HEIGHT=654;public static BufferedImage background;public static BufferedImage start;public static BufferedImage airplane;public static BufferedImage bee;public static BufferedImage bullet;public static BufferedImage hero0;public static BufferedImage hero1;public static BufferedImage pause;public static BufferedImage gameover;public Hero hero=new Hero();//英雄機對象private FlyingObject[] flyings={};//敵人數(shù)組(敵機+蜜蜂)private Bullet[] bullets={};//子彈數(shù)組public static final int START=0;//啟動狀態(tài)public static final int RUNNING=1;//運行public static final int PAUSE=2;//暫停public static final int GAME_OVER=3;//結束private int state=0; //存儲當前狀態(tài)static { // 靜態(tài)代碼塊,初始化圖片資源try {background = ImageIO.read(ShootGame.class.getResource("background.png"));start = ImageIO.read(ShootGame.class.getResource("start.png"));airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));bee = ImageIO.read(ShootGame.class.getResource("bee.png"));bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));pause = ImageIO.read(ShootGame.class.getResource("pause.png"));gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));} catch (Exception e) {e.printStackTrace();}}/** 隨機生成敵人對象(敵機、小蜜蜂)*/public static FlyingObject nextOne(){Random rand=new Random();int type=rand.nextInt(20);//生成[0,20)隨機數(shù)if(type==0){return new Bee();//若為0則返回蜜蜂對象}else{return new Airplane();//1到19則返回敵機對象}}private Timer timer;//聲明定時器對象private int intervel=10;//定時器的時間間隔(以毫秒為單位)int flyEnteredIndex=0;//敵人入場計數(shù)/** 敵人入場(敵機、小蜜蜂)*/public void enterAction(){//10毫秒走一次flyEnteredIndex++;if(flyEnteredIndex%40==0){//每400毫秒走一次FlyingObject o=nextOne();flyings=Arrays.copyOf(flyings, flyings.length+1);//擴容flyings[flyings.length-1]=o;//將對象加入到數(shù)組中}}/** 飛行物走一步*/public void stepAction(){hero.step();for(FlyingObject f:flyings){f.step();}for(Bullet b:bullets){b.step();}}int shootIndex=0;//子彈發(fā)射計數(shù)/** 英雄機發(fā)射子彈--子彈入場*/public void shootAction(){shootIndex++;if(shootIndex%30==0){Bullet[] bs=hero.shoot();bullets=Arrays.copyOf(bullets, bullets.length+bs.length);System.arraycopy(bs,0,bullets, bullets.length-bs.length,bs.length);}}int score=0;/**所有子彈與所有敵人的碰撞 */public void bangAction(){for(Bullet b:bullets){bang(b);//}}/** 一發(fā)子彈和所有敵人的碰撞*/public void bang(Bullet bullet){int index=-1;for(int i=0;i<flyings.length;i++){FlyingObject obj=flyings[i];if(obj.shootBy(bullet)){index=i;break;}}if(index!=-1){//被撞上了FlyingObject one=flyings[index];if(one instanceof Enemy){//敵機Enemy e=(Enemy)one;score+=e.getScore();}if(one instanceof Award){//獎勵Award a=(Award)one;int type=a.getType();switch (type) {case Award.DOUBLE_FIRE:hero.addDoubleFire();break;case Award.LIFE:hero.addLife();break;}}FlyingObject t=flyings[index];flyings[index]=flyings[flyings.length-1];flyings[flyings.length-1]=t;flyings=Arrays.copyOf(flyings, flyings.length-1);//縮容}}public void outOfBoundsAction(){//越界飛行物FlyingObject[] flyingLives=new FlyingObject[flyings.length];//活著的int index=0;for(int i=0;i<flyings.length;i++){FlyingObject obj=flyings[i];if(!obj.outBounds()){//不越界flyingLives[index]=obj;//存儲在flyingLivesindex++;}}flyings=Arrays.copyOf(flyingLives, index);index=0;Bullet[] bulletLives=new Bullet[bullets.length];for(int i=0;i<bullets.length;i++){Bullet b=bullets[i];if(!b.outBounds()){bulletLives[index]=b;index++;}}bullets=Arrays.copyOf(bulletLives, index);}/** 檢查游戲結束*/public void checkGameOverAction(){if(isGameOver()){//若游戲結束state=GAME_OVER; }}/** 判斷游戲是否結束*/public boolean isGameOver(){for(int i=0;i<flyings.length;i++){int index=-1;FlyingObject f=flyings[i]; if(hero.hit(f)){hero.subtractLife();hero.setDoubleFire(0);index=i;}if(index!=-1){//將被撞的敵人刪除FlyingObject t=flyings[index];flyings[index]=flyings[flyings.length-1];flyings[flyings.length-1]=t;flyings=Arrays.copyOf(flyings, flyings.length-1);}}return hero.getLife()<=0;}/** 主程序流程控制*/public void action(){//鼠標操作事件MouseAdapter l=new MouseAdapter() {/** 鼠標移動事件*/public void mouseMoved(MouseEvent e) {if(state==RUNNING){int x=e.getX();//獲取鼠標的x坐標int y=e.getY();//獲取鼠標的y坐標hero.moveTo(x, y);//英雄機隨鼠標移動}}public void mouseClicked(MouseEvent e) {//鼠標點擊事件switch (state) {//判斷當前狀態(tài)case START:state=RUNNING;break;case GAME_OVER:flyings = new FlyingObject[0]; // 清空飛行物bullets = new Bullet[0]; // 清空子彈hero = new Hero(); // 重新創(chuàng)建英雄機score = 0; // 清空成績state = START; // 狀態(tài)設置為啟動}}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);//處理鼠標滑動timer=new Timer();//創(chuàng)建定時器對象timer.schedule(new TimerTask(){public void run() {if(state==RUNNING){enterAction();//敵人入場stepAction();//飛行物走一步shootAction();//英雄機發(fā)子彈bangAction();//子彈和敵人的碰撞outOfBoundsAction();//刪除出界對象checkGameOverAction();//檢查游戲結束 }repaint();}}, intervel,intervel);//第一個intervel:程序啟動到第一次干事的間隔//第二個intervel:每次干事的間隔}//g:畫筆public void paint(Graphics g) {g.drawImage(background,0,0,null);//第四個參數(shù)不需要管,跟程序無關paintHero(g);paintFlyingObjects(g);paintBullets(g);paintScore(g);paintState(g);//畫狀態(tài)}//畫子彈private void paintBullets(Graphics g) {for(Bullet b:bullets){g.drawImage(b.image,b.x,b.y,null);}}//畫敵人private void paintFlyingObjects(Graphics g) {for(FlyingObject f:flyings){g.drawImage(f.image,f.x,f.y,null);}}//畫英雄機private void paintHero(Graphics g) {g.drawImage(hero.image,hero.x,hero.y,null);}//畫分和畫命private void paintScore(Graphics g){g.setColor(new Color(0xFF0000));//設置顏色--紅色g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));//字體,加粗,字號g.drawString("SCORE: "+score, 10, 25);g.drawString("LIFE: "+hero.getLife(), 10, 45);}//畫狀態(tài)public void paintState(Graphics g){switch (state) {//判斷狀態(tài)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;}}public static void main(String[] args) {JFrame frame=new JFrame("Fly");ShootGame game=new ShootGame();frame.add(game);//將面板添加到窗口中frame.setSize(WIDTH,HEIGHT);frame.setAlwaysOnTop(true);//總是居頂frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置默認關閉操作frame.setLocationRelativeTo(null);//設置位置相對與,默認相對位置就是中間點frame.setVisible(true);//1.設置窗口可見 2.盡快調(diào)用paint()方法game.action();//啟動執(zhí)行}}關注公眾號,回復"飛機大戰(zhàn)"即可獲取視頻及圖片
?
總結
以上是生活随笔為你收集整理的Java飞机大战项目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab晶闸管整流电路,采用Matl
- 下一篇: Tongweb连接oracle,Tong