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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

飞机大战(Java)

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

飛機大戰 游戲規則:游戲可以產生小的敵機、大的敵機、小蜜蜂,這三類都是隨機概率出現的,游戲打開的時候,鼠標單擊,游戲開始, 自動發射子彈,英雄機跟隨鼠標移動,當鼠標移到窗口外時,游戲暫停,當鼠標又移回時,游戲繼續,子彈打中敵機和小蜜 蜂,當生命降到0時,消失,敵機 撞擊到英雄機, 英雄機生命值-1, 直到0時, 游戲結束, 小敵機: 分數+,大敵機: 分數+ 獎勵(生命值加成, 火力加成),小蜜蜂: 獎勵(生命值加成, 火力加成)。

源碼地址:https://gitee.com/lxlxlxl233/fly-battle

package com.chinasofti.shoot;import java.util.Random;//敵機 public class Airplane extends FlyingObject implements Enemy{private int speed=2;//走步/*構造方法*/public Airplane(){image=ShootGame.airplane;width=image.getWidth();height=image.getHeight();Random rand =new Random();x=rand.nextInt(ShootGame.WIDTH-this.width);y=-this.height;}@Overridepublic int getScore() {return 5;}@Overridepublic void step() {y+=speed;}@Overridepublic boolean outOfBounds() {return this.y>ShootGame.HEIGHT;}}

?

package com.chinasofti.shoot; //獎勵 public interface Award {public int LIFE=1;//命public int DOUBLE_FIRE=0;//火力值/*獲取獎勵 0:火力值,1:命*/public int getType(); } package com.chinasofti.shoot;import java.util.Random;//小蜜蜂 public class Bee extends FlyingObject implements Award {private int xspeed=1;//x軸速度private int yspeed=2;//y軸速度private int awardType;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);}@Overridepublic int getType() {return awardType;}@Overridepublic void step() {y+=yspeed;x+=xspeed;if(x>ShootGame.WIDTH-this.width){//最右邊xspeed=-1;}if(x<0){//最左邊xspeed=1;}}@Overridepublic boolean outOfBounds() {return this.y>ShootGame.HEIGHT;}} package com.chinasofti.shoot;import java.util.Random;//子彈 public class Bullet extends FlyingObject{private int speed=3;public Bullet(int x,int y) {image=ShootGame.bullet;width=image.getWidth();height=image.getHeight();Random rand =new Random();this.x=x;//隨著英雄機移動this.y=y;//隨著英雄機移動}@Overridepublic void step() {y-=speed;}@Overridepublic boolean outOfBounds() {return this.y<-this.height;}} package com.chinasofti.shoot; //敵人 public interface Enemy {/*得分*/public int getScore(); } package com.chinasofti.shoot;import java.awt.image.BufferedImage;//飛行物 public abstract class FlyingObject {protected BufferedImage image;//圖片protected int width;//長protected int height;//寬protected int x;//橫坐標xprotected int y;//縱坐標y/*飛行物走步*/public abstract void step();/*判斷飛行物是否越界*/public abstract boolean outOfBounds();/*敵人被子彈撞擊*/public boolean shootBy(Bullet bullet){int x1=this.x;//敵人xint x2=this.x+this.width;//敵人寬int y1=this.y;//敵人yint y2=this.y+this.height;//敵人高int x=bullet.x;//子彈xint y=bullet.y;//子彈yreturn x>x1&&x<x2&&y>y1&&y<y2;} } package com.chinasofti.shoot;import java.awt.image.BufferedImage;public class Hero extends FlyingObject {private int life;private int doubleFire;private BufferedImage[] images = {};private int index;//協助切換圖片public Hero() {image = ShootGame.hero0;width = image.getWidth();height = image.getHeight();x = 150;y = 400;life = 3;doubleFire = 0;//默認火力值images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};index = 0;}//圖片切換@Overridepublic void step() {//每100毫秒走一次image = images[index++/10%images.length];}@Overridepublic boolean outOfBounds() {return false;}/*英雄機發射子彈*/public Bullet[] shoot(){int xStep = this.width/4; //1/4英雄機寬度if(doubleFire > 0){//雙倍火力Bullet[] bullets = new Bullet[2];//兩發子彈bullets[0] = new Bullet(this.x+1*xStep,this.y - 20);bullets[1] = new Bullet(this.x+3*xStep,this.y - 20);doubleFire -= 2;//發射一次雙倍火力,活力值-2return bullets;}else {//單倍火力Bullet[] bullets = new Bullet[1];//一發子彈bullets[0] = new Bullet(this.x+2*xStep,this.y - 20);return bullets;}}/*英雄機隨著鼠標移動*/public void moveTo(int x,int y){this.x = x - this.width/2;this.y = y - this.height/2;}//加一條命public void addLife(){life++;}//獲取生命public int getLife(){return life;}/*加火力值*/public void addDoubleFire() {doubleFire += 40;}/*判斷英雄機與敵機碰撞*/public boolean hit(FlyingObject obj) {int x1 = obj.x - this.width/2;int x2 = obj.x + obj.width + this.width/2;int y1 = obj.y - this.height/2;int y2 = obj.y + obj.height +this.height/2;int x = this.x + this.width/2;int y = this.y + this.height/2;return x > x1 && x < x2&&y > y1 && y < y2;}//減少生命public void subtractLift() {life --;}public void setDoubleFire(int doubleFire) {this.doubleFire = doubleFire;} } package com.chinasofti.shoot;import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Arrays; import java.util.Random; import java.util.Timer; import java.util.TimerTask;//測試類 public class ShootGame extends JPanel {//窗口高public static final int WIDTH = 400;public static final int HEIGHT = 654;public static final int GAME_OVER=3;//游戲結束狀態public static final int START=0;//游戲開始狀態public static final int PAUSE=2;//游戲暫停狀態public static final int RUNING=1;//游戲結束狀態private int state=0;//當前狀態public static BufferedImage backgroud;//背景圖public static BufferedImage start;//啟動圖public static BufferedImage pause;//暫停圖public static BufferedImage gameover;//結束圖public static BufferedImage airplane;//敵機圖public static BufferedImage bee;//小蜜蜂圖public static BufferedImage bullet;//子彈圖public static BufferedImage hero0;//英雄0圖public static BufferedImage hero1;//英雄1圖private Hero hero = new Hero();private FlyingObject[] flyings = {};private Bullet[] bullets = {};/*ShootGame() {flyings = new FlyingObject[2];//敵機,小蜜蜂flyings[0] = new Airplane();flyings[1] = new Bee();bullets = new Bullet[1];bullets[0] = new Bullet(130, 110);}*//*生成敵人,小蜜蜂*/public static FlyingObject nextOne() {Random rand = new Random();int type = rand.nextInt(20);//0-19之間的隨機數//控制小蜜蜂數量if (type == 0) {return new Bee();} else {return new Airplane();}}//入場計數 int indxe=1++,使用index控制敵機數量,每400ms產生一架int flyEnteredIndex = 0;static {//靜態初始化資源try {backgroud = ImageIO.read(ShootGame.class.getResource("background.png"));start = ImageIO.read(ShootGame.class.getResource("start.png"));pause = ImageIO.read(ShootGame.class.getResource("pause.png"));gameover = ImageIO.read(ShootGame.class.getResource("gameover.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"));} catch (IOException e) {e.printStackTrace();}}/*敵人入場*/public void enterAction() {//將敵機放入flying數組flyEnteredIndex++;if (flyEnteredIndex % 40 == 0) {//每400毫秒產生一個敵人FlyingObject obj = nextOne();flyings = Arrays.copyOf(flyings, flyings.length + 1);//擴容flyings[flyings.length - 1] = obj;//將敵人賦值給flyings最后一個元素}}//飛行物走步public void stepAction() {hero.step();//敵人走步for (int i = 0; i < flyings.length; i++) {flyings[i].step();//敵人走一步}//子彈走步for (int i = 0; i < bullets.length; i++) {bullets[i].step();//子彈走一步}}/*英雄機發射子彈*/int shootindex = 0;public void shootAction() {shootindex++;if (shootindex % 30 == 0) {//每300ms走一次//創建子彈對象,添加到bullets數組中Bullet[] bs = hero.shoot();bullets=Arrays.copyOf(bullets,bullets.length+bs.length);//擴容彈夾System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);}}//飛行物越界刪除public void outOfBoundsAction() {int index=0;FlyingObject[] flyingLives=new FlyingObject[flyings.length];for(int i=0;i<flyings.length;i++){FlyingObject f = flyings[i];if(!f.outOfBounds()){flyingLives[index]=f;index++;}}flyings=Arrays.copyOf(flyingLives,index);//System.out.println(flyings.length);//子彈越界index=0;Bullet[] bulletLives=new Bullet[bullets.length];for(int i=0;i<bullets.length;i++){Bullet b=bullets[i];if (!b.outOfBounds()){bulletLives[index]=b;index++;}}bullets=Arrays.copyOf(bulletLives,index);}//檢查游戲結束public void checkGameOver(){if(isGameOver()){state=GAME_OVER;}} /*判斷游戲是否結束*/public boolean isGameOver(){for(int i=0;i<flyings.length;i++){FlyingObject f = flyings[i];if(hero.hit(f)){hero.subtractLift();//英雄機-life;//火力清零hero.setDoubleFire(0);//刪除被撞敵人FlyingObject temp=flyings[i];flyings[i]=flyings[flyings.length-1];flyings[flyings.length-1]=temp;//縮容flyings=Arrays.copyOf(flyings,flyings.length-1);}}return hero.getLife()<=0;}/*啟動執行action*/public void action() {//監聽器對象MouseAdapter l=new MouseAdapter() {@Override//重寫鼠標移動方法public void mouseMoved(MouseEvent e) {//監聽器對象if(state==RUNING){//只有運行 時可以控制int x=e.getX();int y=e.getY();hero.moveTo(x,y);}}//重寫鼠標點擊事件public void mouseClicked(MouseEvent e){switch (state){case START:state=RUNING;break;case GAME_OVER:state=START;break;}}public void mouseExited(MouseEvent e){if(state==RUNING){state=PAUSE;}}public void mouseEntered(MouseEvent e){if(state==PAUSE){state=RUNING;}}};this.addMouseListener(l);//處理鼠標操作事件this.addMouseMotionListener(l);//處理鼠標滑動事件Timer timer = new Timer();//定時器int intervel = 10;timer.schedule(new TimerTask() {@Overridepublic void run() {//每10ms運行一次if(state==RUNING) {//只有運行狀態能動enterAction(); //敵人入場stepAction();//飛行物shootAction();//子彈outOfBoundsAction();//刪除越界飛行物bangAction();//子彈碰撞checkGameOver();//檢查游戲結束}repaint();//重畫}}, intervel, intervel);}int score=0;//玩家得分/*所有子彈撞*/public void bangAction(){for (int i=0;i<bullets.length;i++){Bullet b=bullets[i];//獲取每一個子彈bang(b);//碰撞}}/*一顆子彈與敵人的碰撞*/public void bang(Bullet b){int index=-1;//被撞敵人下標,-1表示沒撞上for(int i=0;i<flyings.length;i++){FlyingObject f=flyings[i];//是否被撞if(f.shootBy(b)){index=i;//記錄下標break;}}if(index !=-1){FlyingObject obj=flyings[index];if(obj instanceof Enemy){Enemy e= (Enemy)obj;score+=e.getScore();}if(obj instanceof Award){Award a =(Award)obj;int type=a.getType();switch (type){case Award.DOUBLE_FIRE://雙倍火力hero.addDoubleFire();//英雄機增加火力break;case Award.LIFE:hero.addLife();//加生命break;}}//將被撞敵人刪除FlyingObject temp=flyings[index];flyings[index]=flyings[flyings.length-1];flyings[flyings.length-1]=temp;//縮容flyings=Arrays.copyOf(flyings,flyings.length-1);}}/*重寫paint g畫筆*/@Overridepublic void paint(Graphics g) {g.drawImage(backgroud, 0, 0, null);//畫背景paintHero(g);paintFlyingObjects(g);paintBullets(g);paintScore(g);pantState(g);}//畫狀態public void pantState(Graphics g){switch (state){case START:g.drawImage(start,0,0,null);break;case PAUSE:g.drawImage(pause,0,0,null);break;case GAME_OVER://清理現場hero=new Hero();flyings=new FlyingObject[0];bullets=new Bullet[0];g.drawImage(gameover,0,0,null);break;}}/*畫分 命*/public void paintScore(Graphics g){g.setColor(new Color(0xff));g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));g.drawString("SCORE:"+score,10,25);g.drawString("LIFE:"+hero.getLife(),10,45);}/*畫英雄機*/public void paintHero(Graphics g) {g.drawImage(hero.image, hero.x, hero.y, null);}/*畫敵機和小蜜蜂*/public void paintFlyingObjects(Graphics g) {for (int i = 0; i < flyings.length; i++) {FlyingObject f = flyings[i];//獲取每一個敵人g.drawImage(f.image, f.x, f.y, null);}}/*畫子彈*/public void paintBullets(Graphics g) {for (int i = 0; i < bullets.length; i++) {Bullet b = bullets[i];//獲取每一個子彈g.drawImage(b.image, b.x, b.y, null);}}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);//設置窗口可見 盡快調用paint()方法//啟動計時器game.action();} }

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。