日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java飞机大战

發(fā)布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java飞机大战 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我們在寫飛機(jī)大戰(zhàn)時,先思考一下飛機(jī)大戰(zhàn)中本身有哪些元素

1.本身元素有哪些

  • 第一點(diǎn)就是需要用到Jframe和Jpanel來畫出窗口和畫出圖片
  • 然后設(shè)計(jì)出六個類,分別是天空類、飛機(jī)類、子彈類、(小敵機(jī)、大敵機(jī)、獎勵機(jī))類,設(shè)計(jì)BackGround背景超類讓如上六大類來繼承,給BackGround設(shè)計(jì)兩種構(gòu)造方法,來進(jìn)行分別調(diào)用,圖片加載設(shè)計(jì)為Images類,還有游戲窗口的World類,初學(xué),博主較懶,所以把圖片也都放在了src里邊,同包中加載圖片
  • 在超類中寫出move方法,然后在六大類中引用重寫
  • 主要元素為,三種敵機(jī)在窗口上方隨機(jī)x軸刷新,我方戰(zhàn)機(jī)發(fā)射子彈,敵方大飛機(jī)發(fā)射子彈,獲取命數(shù),獲取戰(zhàn)力值解鎖更高級的子彈,檢測游戲開始、結(jié)束、暫停狀態(tài),天空移動,獲取得分,擊毀敵機(jī),敵機(jī)爆炸,得到獎勵等等
  • 2.里邊運(yùn)用到了什么?

    1)面向?qū)ο?學(xué)會繼承、封裝、抽象、實(shí)現(xiàn)接口 (重點(diǎn)為熟練掌握了重寫與重載);

    2)學(xué)會如何用BufferedImage來讀取圖片,以及怎樣才能讓這些圖片在窗口中顯示出來

    下邊為飛機(jī)大戰(zhàn)源代碼(附運(yùn)行效果)

    backGround 超類

    package flyChicken;import org.omg.CORBA.PUBLIC_MEMBER;import javax.swing.ImageIcon; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Random;// 背景板對象 public abstract class backGround {public static final int LIVE = 0;public static final int DEAD = 1;public static final int REMOVE = 2;protected int state = LIVE;protected int width; //寬protected int height;//高protected int x; //x坐標(biāo)protected int y; //y坐標(biāo)protected int speed;//移動速度/** 三種飛機(jī) */public backGround(int width,int height){this.width = width;this.height = height;Random rand = new Random();x = rand.nextInt(World.WIDTH-70+1);y = -height;speed = rand.nextInt(3)+1;}/* 寫給英雄機(jī),子彈 */public backGround(int width,int height,int x,int y,int speed){this.width = width;this.height = height;this.x = x;this.y = y;this.speed = speed;}/* 飛機(jī)移動 */public abstract void move();/* 獲取對象的圖片 */public abstract BufferedImage getImage();/* 判斷對象是否活著 */public boolean isLive(){return state==LIVE;}/* 判斷對象時候死了 */public boolean isDead(){return state==DEAD;}/** 判斷對象是否被刪除了 */public boolean isRemove(){return state==REMOVE;}/** 檢測越界 */public boolean isOutOfBounds(){return this.y >= World.HEIGHT;}/** 檢測碰撞 */public boolean isHit(backGround other){//假設(shè)this指敵機(jī) other指子彈int x1 = this.x- other.width;int x2 = this.x + this.width;int y1 = this.y - other.height;int y2 = this.y + this.height;int x = other.x;int y = other.y;return x>=x1 && x<=x2 && y>=y1 && y<=y2;}/** 檢測死亡 */public void goDead(){state = REMOVE;}}

    World 整個游戲世界

    package flyChicken;import org.omg.CORBA.PUBLIC_MEMBER;import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*;import java.util.Random; import java.util.Arrays; import java.util.Timer; import java.util.TimerTask; import java.awt.event.MouseEvent; import java.awt.event.MouseAdapter; import javax.swing.JButton;//整個游戲世界 public class World extends JPanel{public static final int WIDTH =400;public static final int HEIGHT = 700;public static final int START = 0; //啟動狀態(tài)public static final int RUNNING = 1; //運(yùn)行狀態(tài)public static final int PAUSE = 2; //暫停狀態(tài)public static final int GAME_OVER = 3; //游戲結(jié)束狀態(tài)private int state = START; //當(dāng)前狀態(tài)(默認(rèn)為啟動狀態(tài))private Sky sky = new Sky();private BattleFlyChicken flys = new BattleFlyChicken();private backGround[] enemys = {};private Bullet[] bullets= {};private EnemyBullet[] enemybuttles = {};/** 隨機(jī)生成敵機(jī) */public backGround nextbgd(){Random rand = new Random();int type = rand.nextInt(20);if (type<12){return new AirPlane();}else if (type<17){return new BigAirPlane();}else{return new Bee();}}/** 敵人入場 */private int bgdEnterIndex = 0;public void bgdEnterAction(){bgdEnterIndex++;if (bgdEnterIndex%20==0){backGround bgd = nextbgd();enemys = Arrays.copyOf(enemys,enemys.length+1);enemys[enemys.length-1] = bgd;}}private int enemybulletIndex = 0;/** 敵機(jī)子彈入場 */public void enemybuttlesEnterAction(){enemybulletIndex++; //10毫秒走一次if (enemybulletIndex%50==0){ //每1000毫秒走一次for (int i=0;i<enemys.length;i++){if (enemys[i] instanceof BigAirPlane){ //若敵機(jī)是大敵機(jī)BigAirPlane ms = (BigAirPlane)enemys[i]; //將敵機(jī)強(qiáng)制轉(zhuǎn)換為大敵機(jī)EnemyBullet obj = ms.shootenemybullet(); //獲取敵機(jī)子彈enemybuttles = Arrays.copyOf(enemybuttles,enemybuttles.length+1);//擴(kuò)容enemybuttles[enemybuttles.length-1] = obj; //將obj添加到最后一個元素上}}}}/** 飛行物移動 */private int moveEnterIndex = 0;public void moveAction(){switch (state){ //判斷是什么狀態(tài)case START:state = START;break;case RUNNING:state = RUNNING;sky.move();for (int i=0;i<enemys.length;i++){enemys[i].move();}for (int i=0;i<bullets.length;i++){bullets[i].move();}for (int i=0;i<enemybuttles.length;i++){enemybuttles[i].move();}break;case PAUSE:state = PAUSE;break;case GAME_OVER:state = GAME_OVER;break;}}/** 戰(zhàn)機(jī)子彈 */private int buttleEnterIndex = 0;public void buttleEnterAction(){switch (state){case START:state = START;break;case RUNNING:state = RUNNING;buttleEnterIndex++;if (buttleEnterIndex%10==0){Bullet[] bs = flys.shootbuttle();bullets = Arrays.copyOf(bullets,bullets.length+bs.length);//擴(kuò)容(bs有幾個就擴(kuò)大幾個容量)System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);}break;case PAUSE:state = PAUSE;break;case GAME_OVER:state = GAME_OVER;break;}}/** 刪除越界的子彈和敵人 */public void outOfBoundsAction(){for (int i=0;i<enemys.length;i++){if (enemys[i].isOutOfBounds() || enemys[i].isRemove()){enemys[i] = enemys[enemys.length-1];enemys = Arrays.copyOf(enemys,enemys.length-1);}}for (int i=0;i<bullets.length;i++){if (bullets[i].isOutOfBounds() || bullets[i].isRemove()){bullets[i] = bullets[bullets.length-1];bullets = Arrays.copyOf(bullets,bullets.length-1);}}for (int i=0;i<enemybuttles.length;i++){if (enemybuttles[i].isOutOfBounds() || enemybuttles[i].isRemove()){enemybuttles[i] = enemybuttles[enemybuttles.length-1];enemybuttles = Arrays.copyOf(enemybuttles,enemybuttles.length-1);}}}/** 子彈與敵人碰撞 */private int score = 0;public void buttleBangAction(){for (int i=0;i<bullets.length;i++){Bullet b = bullets[i];for (int j=0;j<enemys.length;j++){backGround g = enemys[j];if (b.isLive() && g.isLive() && b.isHit(g)){b.goDead();g.goDead();if (g instanceof EnemyScore){EnemyScore es = (EnemyScore)g;score += es.getScore();}if (g instanceof EnemyAward){EnemyAward ea = (EnemyAward) g;int type = ea.getAwardType();switch(type){case EnemyAward.FIRE:flys.addFire();break;case EnemyAward.LIFE:flys.addLife();break;}}}}}}public void enemybuttleBangAction(){for (int i = 0; i < enemybuttles.length; i++) {EnemyBullet eb = enemybuttles[i];if (eb.isLive() && flys.isLive() && eb.isHit(flys)){eb.goDead();flys.subtractLife();}}}/** 判斷游戲是否結(jié)束 */public void checkGameOverAction(){if (flys.getLife()<=0){ //若英雄機(jī)的命數(shù)<=0,表示游戲結(jié)束了state = GAME_OVER; //將當(dāng)前狀態(tài)修改為游戲結(jié)束狀態(tài)}}/** 啟動程序 */public void action(){System.out.println("-------------"+state);/** 鼠標(biāo)監(jiān)聽啟動程序的執(zhí)行 */MouseAdapter m = new MouseAdapter() {public void mouseMoved(MouseEvent e) {if (state==RUNNING){int x = e.getX(); //獲取鼠標(biāo)的x坐標(biāo)int y = e.getY(); //獲取鼠標(biāo)的y坐標(biāo)flys.moveTo(x,y); //英雄機(jī)隨著鼠標(biāo)移動}}/** 重寫mouseClicked()鼠標(biāo)點(diǎn)擊事件 */public void mouseClicked(MouseEvent e){switch (state){case START: //啟動狀態(tài)時state = RUNNING; //修改為運(yùn)行狀態(tài)break;case GAME_OVER: //游戲結(jié)束狀態(tài)時score = 0; //清理現(xiàn)場sky = new Sky();flys = new BattleFlyChicken();bullets = new Bullet[0];enemys = new backGround[0];enemybuttles = new EnemyBullet[0];state = START; //修改為啟動狀態(tài)break;}}/** 重寫mouseExited()鼠標(biāo)移出事件 */public void mouseExited(MouseEvent e){if (state==RUNNING){ //運(yùn)行狀態(tài)時state = PAUSE; //修改為暫停狀態(tài)}}/** 重寫mouseEntered()鼠標(biāo)移入事件 */public void mouseEntered(MouseEvent e){if(state==PAUSE){ //暫停狀態(tài)時state = RUNNING; //修改為運(yùn)行狀態(tài)}}};this.addMouseListener(m);this.addMouseMotionListener(m);Timer timer = new Timer();int interval = 10;timer.schedule(new TimerTask() {public void run() {bgdEnterAction();buttleEnterAction();enemybuttlesEnterAction();moveAction();outOfBoundsAction();buttleBangAction();enemybuttleBangAction();checkGameOverAction();repaint();}},interval,interval);}/** 重寫paint */public void paint(Graphics g){g.drawImage(sky.getImage(),sky.x,sky.y,null);g.drawImage(sky.getImage(), sky.x,sky.getY1(),null);g.drawImage(flys.getImage(), flys.x, flys.y,null);for (int i=0;i< enemys.length;i++){backGround f = enemys[i];g.drawImage(f.getImage(),f.x,f.y,null);}for (int i=0;i< bullets.length;i++){backGround b = bullets[i];g.drawImage(b.getImage(),b.x,b.y,null);}for (int i=0;i< enemybuttles.length;i++){backGround c = enemybuttles[i];g.drawImage(c.getImage(),c.x,c.y,null);}g.drawString("分?jǐn)?shù):"+score,10,25);g.drawString("生命:"+flys.getLife(),10,45);g.drawString("戰(zhàn)斗力:"+flys.getFire(),10,65);switch (state){ //根據(jù)當(dāng)前狀態(tài)來畫不同的狀態(tài)圖case START: //啟動狀態(tài)時畫啟動圖g.drawImage(Images.start,0,0,null);break;case PAUSE: //暫停狀態(tài)時畫暫停圖g.drawImage(Images.pause,0,0,null);break;case GAME_OVER: //游戲結(jié)束狀態(tài)時畫游戲結(jié)束圖g.drawImage(Images.gameover,0,0,null);break;}}public static void main(String[] args) {JFrame frame = new JFrame();World world = new World();frame.setTitle("雷霆戰(zhàn)機(jī)");frame.add(world);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(WIDTH+16,HEIGHT+39);frame.setLocationRelativeTo(null);frame.setVisible(true);world.action(); //啟動程序的執(zhí)行}}

    Images 圖片類

    package flyChicken;import javax.swing.ImageIcon; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; //圖片 public class Images {//導(dǎo)入圖片public static BufferedImage start;public static BufferedImage pause;public static BufferedImage gameover;public static BufferedImage sky;public static BufferedImage[] heros;public static BufferedImage bullet;public static BufferedImage enemybullet;public static BufferedImage[] bee;public static BufferedImage[] airplane;public static BufferedImage[] bigairplane;//初始化靜態(tài)圖片static {start = readImage("start.png");pause = readImage("pause.png");gameover = readImage("gameover.png");sky = readImage("background.png");bullet = readImage("bullet1.png");enemybullet = readImage("bullet2.png");heros = new BufferedImage[2]; //兩張圖片heros[0] = readImage("hero0.png");heros[1] = readImage("hero1.png");airplane = new BufferedImage[5]; //5張圖片bigairplane = new BufferedImage[5];bee = new BufferedImage[5];airplane[0] = readImage("airplane.png");bigairplane[0] = readImage("bigairplane.png");bee[0] = readImage("bee.png");for (int i=1;i< airplane.length;i++){airplane[i] = readImage("bom"+i+".png");bigairplane[i] = readImage("bom"+i+".png");bee[i] = readImage("bom"+i+".png");}}private static BufferedImage readImage(String fileName) {try{BufferedImage img = ImageIO.read(backGround.class.getResource(fileName));return img;}catch(Exception e){e.printStackTrace();throw new RuntimeException();}}}

    Sky 天空類

    package flyChicken;import java.awt.image.BufferedImage;public class Sky extends backGround{private int y1; //第2張圖片的y坐標(biāo)/** 構(gòu)造方法 */public Sky(){super(World.WIDTH,World.HEIGHT,0,0,3);y1 = -World.HEIGHT;}//移動 public void move(){y += speed;y1 += speed;if (y >= World.HEIGHT){y = -World.HEIGHT;}if (y1 >= World.HEIGHT){y1 = -World.HEIGHT;}}//獲取圖片public BufferedImage getImage(){return Images.sky;}//獲取第二張?zhí)炜請Dpublic int getY1(){return y1;}}

    EnemyBullet 敵機(jī)子彈類

    package flyChicken;import java.awt.image.BufferedImage;public class EnemyBullet extends backGround{public EnemyBullet(int x,int y){super(9,21,x,y,3);}//移動public void move(){y += speed;}//獲取圖片public BufferedImage getImage(){return Images.enemybullet;}//越界刪除public boolean isOutOfBounds(){return this.y>=World.HEIGHT; //深水炸彈的y>=窗口的高,即為越界了} }

    Bullet 英雄機(jī)子彈類

    package flyChicken;import javax.swing.*; import java.awt.image.BufferedImage;public class Bullet extends backGround{//構(gòu)造方法public Bullet(int x,int y){super(8,20,x,y,3);}//移動public void move(){y -= speed;}//獲取圖片public BufferedImage getImage(){return Images.bullet;}//越界刪除public boolean isOutOfBounds(){return this.y>=World.HEIGHT; //深水炸彈的y>=窗口的高,即為越界了}}

    BattleFlyChicken 英雄機(jī)類

    package flyChicken;import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;import javax.swing.*; import java.awt.*; import java.awt.event.InputEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage;public class BattleFlyChicken extends backGround{//定義私有的火力值和生命private int life;private int fire;public BattleFlyChicken(){super(97,139,150,550,20);life = 5;fire = 0;}public void move(){}//獲取圖片int index = 0;public BufferedImage getImage(){return Images.heros[index++%Images.heros.length];}//獲取多種子彈public Bullet[] shootbuttle(){int xa = this.x+width/2-25;int xb = this.x+width/2+15;int xc = this.x+width/2-5;int ya = this.y+40;int yb = this.y;if (fire>=50){Bullet[] bs = new Bullet[3];bs[0] = new Bullet(xa,ya);bs[1] = new Bullet(xc,yb);bs[2] = new Bullet(xb,ya);fire -= 1;return bs;}else if (fire>=20){Bullet[] bs = new Bullet[2];bs[0] = new Bullet(xa,ya);bs[1] = new Bullet(xb,ya);fire -= 2;return bs;}else{Bullet[] bs = new Bullet[1];bs[0] = new Bullet(xc,yb);return bs;}}//鼠標(biāo)事件public void moveTo(int x,int y){this.x = x-this.width/2; //英雄機(jī)的x=鼠標(biāo)的x-1/2英雄機(jī)的寬this.y = y-this.height/2; //英雄機(jī)的y=鼠標(biāo)的y-1/2英雄機(jī)的高}/** 英雄機(jī)加命 */public void addLife(){life++;}/** 獲取英雄機(jī)的命 */public int getLife(){return life;}//獲取英雄機(jī)火力值public int getFire(){return fire;}//添加火力值public void addFire(){fire += 20;}//碰撞子彈減命public void subtractLife(){life--;}}

    AirPlane 小敵機(jī)類

    package flyChicken;import javax.swing.*; import java.awt.image.BufferedImage;public class AirPlane extends backGround implements EnemyScore{public AirPlane(){super(48,50);}//移動public void move(){y += speed;}//獲取圖片int index = 1;public BufferedImage getImage(){if (isLive()){return Images.airplane[0];}else if(isDead()){BufferedImage img = Images.airplane[index++];if (index==Images.airplane.length){state = REMOVE;}return img;}return null;}//獲取得分public int getScore(){return 1;}}

    BigAirPlane 大敵機(jī)類

    package flyChicken;import javax.swing.*; import java.awt.image.BufferedImage;public class BigAirPlane extends backGround implements EnemyScore{//構(gòu)造方法public BigAirPlane(){super(66,89);}//移動public void move(){y += speed;}//敵機(jī)子彈public EnemyBullet shootenemybullet(){int x = this.x+width/2-5;int y = this.y;return new EnemyBullet(x,y);}//獲取圖片int index = 1;public BufferedImage getImage(){if (isLive()){return Images.bigairplane[0];}else if(isDead()){BufferedImage img = Images.bigairplane[index++];if (index==Images.bigairplane.length){state = REMOVE;}return img;}return null;}//得分public int getScore(){return 10;}}

    Bee 獎勵機(jī)類

    package flyChicken;import javax.swing.*; import java.awt.image.BufferedImage; import java.util.Random;public class Bee extends backGround implements EnemyAward{//獎勵機(jī)特有的獎勵private int awardType;//構(gòu)造方法public Bee(){super(60,51);Random rand = new Random();awardType = rand.nextInt(2);}//移動public void move(){y += speed;}//獲取圖片int index = 1;public BufferedImage getImage(){if (isLive()){return Images.bee[0];}else if(isDead()){BufferedImage img = Images.bee[index++];if (index==Images.bee.length){state = REMOVE;}return img;}return null;}//得到獎勵public int getAwardType(){return awardType;}}

    EnemyScore 得分接口

    package flyChicken;public interface EnemyScore {/** 得分 */public int getScore(); }

    EnemyAward 獎勵接口

    package flyChicken;public interface EnemyAward {public int FIRE = 0; //火力值public int LIFE = 1; //命/** 獲取獎勵類型(0或1) */public int getAwardType();}

    ?

    ?

    ?飛機(jī)大戰(zhàn)的分享就到此結(jié)束了,如果有幫助到你們的話,記得(三連)哦!!!

    總結(jié)

    以上是生活随笔為你收集整理的java飞机大战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。