java项目飞机大战
生活随笔
收集整理的這篇文章主要介紹了
java项目飞机大战
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
項目說明
此項目總共代碼量為400行左右,在b站有教程(時長1h),適合于剛剛學完了java的同學做第一個項目。
程序運行效果圖
玩法說明
玩家鼠標左鍵開始游戲,空格鍵暫停,用鼠標控制我方飛機的移動,當出現的小飛機總數達到50時,敵方boss出現,擊敗敵方boss游戲勝利。
最終打包可運行文件下載
點擊下載
以下為項目實現部分------------------------------------------------
文件組織形式
源碼部分
bgObj.java
package com.sqm;import java.awt.*;public class bgObj extends GamObj {public bgObj(Image image, int x, int y, int speed){super(image, x, y, speed);}@Overridepublic void paintself(Graphics g){super.paintself(g);y += speed;if (y >= 0) y = -2000;//背景圖片的循環播放} }BossObj.java
package com.sqm;import java.awt.*;public class BossObj extends GamObj {int life = 10;//敵方boss血條public BossObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {super(image, x, y, width, height, speed, frame);}@Overridepublic void paintself(Graphics g) {super.paintself(g);//防止走出屏幕if(x >= 550 || x < -50){speed = -speed;}x += speed;for (ShellObj shellObj:GameUtils.shellObjList){if(this.getRec().intersects(shellObj.getRec())){shellObj.setX(-100);GameUtils.removeList.add(shellObj);life --;}if (life <= 0){GameWin.state = 4;}}//血條的白色背景g.setColor(Color.white);g.fillRect(20,40,100,10);//血條的繪制g.setColor(Color.red);g.fillRect(20,40,life * 100 / 10,10);}@Overridepublic Rectangle getRec() {return super.getRec();}}BulletObj.java
package com.sqm;import java.awt.*;public class BulletObj extends GamObj {public BulletObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {super(image, x, y, width, height, speed, frame);}@Overridepublic void paintself(Graphics g) {super.paintself(g);y += speed;//敵方子彈與我方子彈的碰撞檢測if(this.getRec().intersects(this.frame.plane.getRec())){GameWin.state = 3;}//判斷敵方子彈是否出界if(y > 600){GameUtils.bulletObjList.remove(this);GameUtils.removeList.add(this);}}@Overridepublic Rectangle getRec() {return super.getRec();} }EnemyObj.java
package com.sqm;import java.awt.*;public class EnemyObj extends GamObj {public EnemyObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {super(image, x, y, width, height, speed, frame);}@Overridepublic void paintself(Graphics g) {super.paintself(g);y += speed;//判斷敵機是否與我方飛機碰撞if(this.getRec().intersects(this.frame.plane.getRec())){GameWin.state = 3;}//判斷敵機和我方子彈是否碰撞for (ShellObj shell:GameUtils.shellObjList){//敵機被我方子彈擊落if(this.getRec().intersects(shell.getRec())){Explode explode = new Explode(x,y);GameUtils.explodeList.add(explode);GameUtils.removeList.add(explode);this.setX(-100);shell.setX(-100);GameUtils.removeList.add(this);GameUtils.removeList.add(shell);GameWin.score ++;}}//判斷敵方飛機是否出界if(y > 600){GameUtils.removeList.add(this);GameUtils.EnemyObjList.remove(this);}}@Overridepublic Rectangle getRec() {return super.getRec();} }Explode.java
package com.sqm;import java.awt.*;public class Explode extends GamObj {static Image[] pic = new Image[16];int explodeCount = 0;//讓圖片只顯示一次static{for (int i = 0; i < pic.length; i++){pic[i] = GameUtils.readImage("imgs/explode/e" + (i + 1) + ".gif");}}public Explode(int x, int y) {super(x, y);}@Overridepublic void paintself(Graphics g){if(explodeCount < 16){image = pic[explodeCount];explodeCount ++;super.paintself(g);}} }GameUtils.java
package com.sqm;import javax.imageio.ImageIO; import java.awt.*; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List;public class GameUtils {public static int WIDTH = 600;public static int HEIGHT = 600;//獲取圖片public static Image readImage(String path){Image image = null;try{//返回當前運行文件的目錄InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);image = ImageIO.read(resourceAsStream);resourceAsStream.close();}catch (IOException e){e.printStackTrace();}return image;}//繪制字符串的工具類public static void draw(Graphics gImage,String str, Color color, int size, int x, int y){gImage.setColor(color);gImage.setFont(new Font("仿宋",Font.BOLD,size));gImage.drawString(str,x,y);}//背景圖public static Image bgImg = readImage("imgs/bg.jpg");//boss圖public static Image bossImg = readImage("imgs/boss.png");//爆炸圖public static Image explodImg = readImage("imgs/explode/e6.gif");//我方飛機圖public static Image planeImg = readImage("imgs/plane.png");//我方子彈圖片public static Image shellImg = readImage("imgs/shell.png");//敵機圖片public static Image enemyImg = readImage("imgs/enemy.png");//敵方子彈圖片public static Image bulletImg = readImage("imgs/bullet.png");//我方子彈的集合public static List<ShellObj> shellObjList = new ArrayList<>();//我方游戲物體的集合public static List<GamObj> gamObjList = new ArrayList<>();//敵機的集合public static List<EnemyObj> EnemyObjList = new ArrayList<>();//要移除對象的集合public static List<GamObj> removeList = new ArrayList<>();//敵方子彈集合public static List<BulletObj> bulletObjList = new ArrayList<>();//爆炸集合public static List<Explode> explodeList = new ArrayList<>(); }GameWin.java
package com.sqm;import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;public class GameWin extends JFrame {//游戲狀態設置 0未開始 1游戲中 2暫停 3通關失敗 4通過成功public static int state = 0;//游戲得分public static int score = 0;//開掛public static boolean kaigua = false;//游戲重繪次數int count = 1;//敵機生成數量int enemyCount = 0;//雙緩沖罐防閃屏Image offScreenImage = null;//背景bgObj bg = new bgObj(GameUtils.bgImg,0,-2000,2);//我方飛機PlaneObj plane = new PlaneObj(GameUtils.planeImg,290,590,20,30,0,this);//boss飛機BossObj bossObj = null;public void lanch(){this.setVisible(true);this.setSize(GameUtils.WIDTH,GameUtils.HEIGHT);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);this.setTitle("飛機大戰");//鼠標點擊事件this.addMouseListener(new MouseAdapter(){@Overridepublic void mouseClicked(MouseEvent e){if (e.getButton() == 1)//按下鼠標左鍵并且游戲狀態為尚未開始時,開始游戲{if(state == 0){state = 1;repaint();}else if(state == 3){state = 1;GameUtils.gamObjList.clear();GameUtils.bulletObjList.clear();GameUtils.removeList.clear();GameUtils.EnemyObjList.clear();GameUtils.shellObjList.clear();GameUtils.explodeList.clear();bossObj = null;GameUtils.gamObjList.add(bg);GameUtils.gamObjList.add(plane);score = 0;repaint();}}}});//暫停功能this.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if(e.getKeyCode() == 32){switch (state){case 1:state = 2;break;case 2:state = 1;break;default:}}//按下k鍵開掛if(e.getKeyCode() == 75){if(kaigua)kaigua = false;else kaigua = true;}}});GameUtils.gamObjList.add(bg);GameUtils.gamObjList.add(plane);while(true){if(state == 1){createObj();repaint();}try{Thread.sleep(25);}catch (InterruptedException e){e.printStackTrace();}}}public void paint(Graphics g){if(offScreenImage == null){offScreenImage = createImage(GameUtils.WIDTH,GameUtils.HEIGHT);}//獲取緩沖圖片的畫筆對象Graphics gImage = offScreenImage.getGraphics();gImage.fillRect(0,0,GameUtils.WIDTH,GameUtils.HEIGHT);//游戲尚未開始if(state == 0){gImage.drawImage(GameUtils.bgImg,0,0,null);gImage.drawImage(GameUtils.bossImg,220,120,null);gImage.drawImage(GameUtils.explodImg,270,350,null);GameUtils.draw(gImage,"點擊開始游戲",Color.yellow,40,180,300);}//游戲已經開始if(state == 1){GameUtils.gamObjList.addAll(GameUtils.explodeList);for (int i = 0; i < GameUtils.gamObjList.size(); i++){GameUtils.gamObjList.get(i).paintself(gImage);}GameUtils.gamObjList.removeAll(GameUtils.removeList);}//游戲失敗if(state == 3){gImage.drawImage(GameUtils.explodImg,plane.getX()-35,plane.getY()-50,null);GameUtils.draw(gImage,"GAME OVER",Color.red,40,180,300);}//游戲通關if(state == 4){gImage.drawImage(GameUtils.explodImg,bossObj.getX()+30,bossObj.getY(),null);GameUtils.draw(gImage,"游戲通關",Color.green,50,190,300);}//計分面板GameUtils.draw(gImage,score + "分",Color.green,40,30,100);count ++;g.drawImage(offScreenImage,0,0,null);}void createObj(){if(count % 15 == 0 || kaigua){//我方子彈GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg,plane.getX()+3,plane.getY()-16,14,25,5,this));GameUtils.gamObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));}if(count % 15 == 0){//敵機GameUtils.EnemyObjList.add(new EnemyObj(GameUtils.enemyImg,(int)(Math.random()*12) * 50,0,49,36,5,this));GameUtils.gamObjList.add(GameUtils.EnemyObjList.get(GameUtils.EnemyObjList.size() - 1));enemyCount ++;}if(count % 15 == 0 && bossObj != null){//敵方子彈GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg, bossObj.getX() + 76, bossObj.getY() + 85, 15,25,5,this));GameUtils.gamObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size() - 1));}//小飛機出現100輛后出現bossif(enemyCount >= 50 && bossObj == null){bossObj = new BossObj(GameUtils.bossImg,250,0,155,100,5,this);GameUtils.gamObjList.add(bossObj);}}public static void main(String[] args){GameWin gameWin = new GameWin();gameWin.lanch();}}GameObj.java
package com.sqm;import java.awt.*;public class GamObj {Image image;int x;int y;int width;int height;int speed;GameWin frame;public Image getImage() {return image;}public int getX() {return x;}public int getY() {return y;}public int getWidth() {return width;}public int getHeight() {return height;}public int getSpeed() {return speed;}public GameWin getFrame() {return frame;}public GamObj setImage(Image image) {this.image = image;return this;}public GamObj setX(int x) {this.x = x;return this;}public GamObj setY(int y) {this.y = y;return this;}public GamObj setWidth(int width) {this.width = width;return this;}public GamObj setHeight(int height) {this.height = height;return this;}public GamObj setSpeed(int speed) {this.speed = speed;return this;}public GamObj setFrame(GameWin frame) {this.frame = frame;return this;}public GamObj(Image image, int x, int y, int width, int height, int speed, GameWin frame){this.image = image;this.x = x;this.y = y;this.width = width;this.height = height;this.speed = speed;this.frame = frame;}public GamObj(int x, int y) {this.x = x;this.y = y;}public GamObj(Image image, int x, int y, int speed){this.image = image;this.x = x;this.y = y;this.speed = speed;}public void paintself(Graphics g){g.drawImage(image,x,y,null);}public Rectangle getRec(){return new Rectangle(x,y,width,height);} }PlaneObj.java
package com.sqm;import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;public class PlaneObj extends GamObj {@Overridepublic Image getImage() {return super.getImage();}public PlaneObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {super(image, x, y, width, height, speed, frame);this.frame.addMouseMotionListener(new MouseAdapter() {@Overridepublic void mouseMoved(MouseEvent e) {PlaneObj.super.x = e.getX() - 11;PlaneObj.super.y = e.getY() - 16;}});}@Overridepublic void paintself(Graphics g) {//我方飛機和敵方boss的檢測if(this.frame.bossObj != null && this.getRec().intersects(this.frame.bossObj.getRec())){GameWin.state = 3;}super.paintself(g);}@Overridepublic Rectangle getRec() {return super.getRec();} }ShellObj.java
package com.sqm;import java.awt.*;public class ShellObj extends GamObj {@Overridepublic Image getImage() {return super.getImage();}public ShellObj(Image image, int x, int y, int width, int height, int speed, GameWin frame) {super(image, x, y, width, height, speed, frame);}@Overridepublic void paintself(Graphics g) {super.paintself(g);y -= speed;//我方子彈的越界消失if (y < 0){GameUtils.removeList.add(this);GameUtils.shellObjList.remove(this);}}@Overridepublic Rectangle getRec() {return super.getRec();} }總結
以上是生活随笔為你收集整理的java项目飞机大战的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 添加注解_你知道Java中的p
- 下一篇: 移动开发web第一天