您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“java如何實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
MyPanel類
package P; import java.awt.Font; import java.awt.Graphics; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.List; import javax.sql.RowSetInternal; import javax.swing.ImageIcon; import javax.swing.JPanel; //xx.2 //創(chuàng)建面板類,繼承面板,實(shí)現(xiàn)移動(dòng)監(jiān)聽(tīng)事件 public class MyPanel extends JPanel implements MouseMotionListener { //private ImageIcon bjImage = new ImageIcon("img/bj.png"); //英雄機(jī)圖片 private ImageIcon heroImage = new ImageIcon("img/頭.png"); //顯示logo //private ImageIcon logoImage = new ImageIcon("img/頭.png"); //英雄機(jī)的寬高 private int width = heroImage.getIconWidth(); private int height = heroImage.getIconHeight(); //英雄機(jī)的坐標(biāo) private int x=180; private int y=880; //創(chuàng)建一個(gè)敵機(jī)的集合,用于展示多個(gè)敵機(jī) List<Enemy> enemys = new ArrayList<>(); //存儲(chǔ)子彈集合 List<Bullet> bullets = new ArrayList<>(); //存儲(chǔ)爆炸對(duì)象集合 List<Bomb> bombs = new ArrayList<>(); //存儲(chǔ)英雄機(jī)爆炸集合 List<HeroDead> heroDeads = new ArrayList<>(); private int number;//統(tǒng)計(jì)當(dāng)前得分 private int m;//統(tǒng)計(jì)剩余血條量 // MyPanel jp = new MyPanel() { // { // setBackground(Color.gray);// 設(shè)置面板背景顏色 // } public MyPanel() { //在構(gòu)造方法中準(zhǔn)備10個(gè)敵機(jī) for(int i=0;i<10;i++){ enemys.add(new Enemy()); } } @Override public void paint(Graphics g) { //用于繪制圖片區(qū)域 super.paint(g); //在窗口左側(cè)展示打飛機(jī)得分 //設(shè)置字體: 參數(shù)1:字體家族, 參數(shù)2:字體樣式:加粗 參數(shù)3:字體大小 g.setFont(new Font("宋體", Font.BOLD, 30)); g.drawString("當(dāng)前得分:"+number, 5, 30); g.drawString("發(fā)量(萬(wàn)根):"+(5-m), 5, 80); //g.drawImage(null, x, y, getBackground(), getFocusCycleRootAncestor()); //繪制英雄機(jī)圖片 參數(shù)1,圖片 參數(shù)2和3:坐標(biāo) g.drawImage(heroImage.getImage(), x, y, null); //g.drawImage(logoImage.getImage(), 19, 22, null); //在繪圖中顯示10輛敵機(jī) for(int i=0;i<enemys.size();i++){ Enemy enemy = enemys.get(i); //獲取敵機(jī)對(duì)象 enemy.drawImage(g); //然后分別展示 } //展示子彈集合的圖片 for (int i = 0; i < bullets.size(); i++) { Bullet bullet = bullets.get(i); //取出子彈對(duì)象 bullet.drawImage(g); } //展示爆炸集合的圖片 for(int i=0;i<bombs.size();i++){ Bomb bomb = bombs.get(i); bomb.drawImage(g); } //展示英雄機(jī)的銷毀圖片 for(int i=0;i<heroDeads.size();i++){ HeroDead heroDead = heroDeads.get(i); heroDead.drawImage(g); } } @Override public void mouseDragged(MouseEvent e) { //System.out.println("鼠標(biāo)移動(dòng)并拖拽觸發(fā)"); //鼠標(biāo)拖拽時(shí),需要將英雄機(jī)也帶著移動(dòng)(只需改變x軸和y軸) x = e.getX()-width/2; //英雄機(jī)的移動(dòng),隨著鼠標(biāo)觸發(fā)移動(dòng)的 y = e.getY()-height/2; //鼠標(biāo)指定到英雄機(jī)中間位置 repaint(); //重新繪制圖片 } @Override public void mouseMoved(MouseEvent e) { //System.out.println("鼠標(biāo)移動(dòng)的觸發(fā).."); //鼠標(biāo)移動(dòng)時(shí),需要將英雄機(jī)也帶著移動(dòng)(只需改變x軸和y軸) x = e.getX()-width/2; //英雄機(jī)的移動(dòng),隨著鼠標(biāo)觸發(fā)移動(dòng)的 y = e.getY()-height/2; //鼠標(biāo)指定到英雄機(jī)中間位置 repaint(); //重新繪制圖片 } //L-4. 創(chuàng)建子彈類Bullet,操作與敵機(jī)類類似 public void init() { //定義一個(gè)標(biāo)記,循環(huán)了自定義的次數(shù)后,才去添加,這樣子彈數(shù)量會(huì)變少 int flag = 0; while(true) { //循環(huán)地跑,模擬依次移動(dòng)的效果 flag++; // if(flag==50) { //控制子彈數(shù)量 bullets.add(new Bullet(x+width/2, y)); //就是從英雄級(jí)的x,y軸位置發(fā)射子彈的 flag=0; //又回到0,依次計(jì)算15次 //System.out.println("子彈數(shù)量:"+ bullets.size()); } //展示子彈,讓子彈飛起來(lái) for(int i=0;i<bullets.size();i++) { Bullet bullet = bullets.get(i); //取出對(duì)象 if(i%2!=0)//設(shè)置子彈左右發(fā)射 bullet.move(); else bullet.move1();//移動(dòng)子彈位置 //如果子彈移動(dòng)到y(tǒng)軸為0,則移除掉 if(bullet.getY()<0){ bullets.remove(bullet); //移除子彈對(duì)象 } } //xx.3. //敵機(jī)循環(huán)移動(dòng).. for(int i=0;i<enemys.size();i++) { Enemy en = enemys.get(i); en.move(); //循環(huán)地移動(dòng)每一架敵機(jī)y軸位置 //超出屏幕范圍后,需要移除,并重新添加一個(gè)對(duì)象 if(en.getY()>GameMain.HEIGHT) { //int count1=enemys.size(); enemys.remove(en); //移除對(duì)象 enemys.add(new Enemy()); //重新再創(chuàng)建對(duì)象 } //L 3-4 //在敵機(jī)的循環(huán)中,再繼續(xù)循環(huán)子彈; 需要判斷是否有子彈和敵機(jī)重疊了, //則移除敵機(jī)對(duì)象,重新添加,移除子彈;如果有則添加爆炸對(duì)象 for(int j=0;j<bullets.size();j++){ Bullet bu = bullets.get(j); if(isHit(en,bu)){ //碰撞的判斷 enemys.remove(en); //移除敵機(jī),并重新new一個(gè) enemys.add(new Enemy()); bullets.remove(bu); //移除子彈 //添加的爆炸位置和敵機(jī)位置一致 bombs.add(new Bomb(en.getX(), en.getY())); number += 10; //爆炸后,累加得分 } } //zz-6 //判斷英雄機(jī)與敵機(jī)的碰撞(英雄機(jī)的消亡) if(isHit(en)){ System.out.println("進(jìn)入英雄機(jī)爆炸..."); //敵機(jī)的對(duì)象移除 enemys.remove(en); //英雄機(jī)爆炸圖片位置應(yīng)該與英雄機(jī)重疊 heroDeads.add(new HeroDead(x, y)); for(m=0;m<=heroDeads.size();m++) { while(heroDeads.size()==5) { return; } } //游戲結(jié)束,跳出死循環(huán) } } //L-5 //將爆炸的所有對(duì)象,過(guò)一段時(shí)間則干掉 for(int i=0;i<bombs.size();i++){ Bomb bomb = bombs.get(i); bomb.move(); //計(jì)算次數(shù),統(tǒng)一循環(huán)多少次后,再干掉 if(bomb.getCount()>6){ bombs.remove(bomb); //在循環(huán)的一定范圍后,可以移除爆炸了 } } //xx.2 //每次的移動(dòng)都需要停頓一下 try { Thread.sleep(6); //單位:毫秒 睡眠6毫秒 } catch (InterruptedException e) { e.printStackTrace(); } repaint(); //重新繪制圖片 } } //zz-6 //英雄機(jī)與敵機(jī)的碰撞 private boolean isHit(Enemy en) { //英雄機(jī)的碰撞區(qū)域 Rectangle rect = new Rectangle(x, y, width, height); //碰撞點(diǎn)的位置,是在敵機(jī)的中心點(diǎn) Point point = new Point(en.getX()+en.getWidth()/2, en.getY()+en.getHeight()/2); return rect.contains(point); } //L-5 private boolean isHit(Enemy en, Bullet bu) { //填充敵機(jī)的碰撞區(qū)域 Rectangle rect = new Rectangle(en.getX(), en.getY(), en.getWidth(), en.getHeight()); //將子彈的位置設(shè)置在中間 Point point = new Point(bu.getX()+bu.getWidth()/2, bu.getY()+bu.getHeight()/2); //如果位置有重疊,返回true return rect.contains(point); } }
HeroDead類
package P; import java.awt.Graphics; import javax.swing.ImageIcon; public class HeroDead { private ImageIcon heroImage = new ImageIcon("img/爆炸.gif"); private int width = heroImage.getIconWidth(); private int height = heroImage.getIconHeight(); private int x; private int y; public ImageIcon getHeroImage() { return heroImage; } public void setHeroImage(ImageIcon heroImage) { this.heroImage = heroImage; } 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 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 HeroDead(int x,int y){ this.x=x; this.y=y; } public void drawImage(Graphics g) { g.drawImage(heroImage.getImage(), x, y, null); } }
GameMain類
package P; import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JFrame; //游戲入口類 public class GameMain { static final int WIDTH = 860; //設(shè)置靜態(tài)常量,作為狀態(tài)值使用 static final int HEIGHT = 660; public static void main(String[] args) { JFrame jFrame = new JFrame(); //實(shí)例化頂級(jí)窗口類 jFrame.setSize(WIDTH, HEIGHT); //設(shè)置寬高像素 jFrame.setTitle("小禿頭歷險(xiǎn)記"); //設(shè)置標(biāo)題 jFrame.setLocationRelativeTo(null); //設(shè)置居中效果 //JFrame.EXIT_ON_CLOSE: 狀態(tài)值 3 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口的同時(shí),把程序關(guān)閉 //在窗口中,加入面板 MyPanel pl = new MyPanel(); pl.setBackground(Color.pink); pl.setBorder(BorderFactory.createLineBorder(Color.cyan, 3)); jFrame.add(pl); //添加面板組件 jFrame.addMouseMotionListener(pl); //添加鼠標(biāo)移動(dòng)事件 jFrame.setVisible(true); //使窗口可視化 pl.init(); //在面板中循環(huán)將組件跑起來(lái) } }
Enemy類
package P; import java.awt.Graphics; import java.util.Random; import javax.swing.ImageIcon; //創(chuàng)建敵機(jī)類 public class Enemy { //創(chuàng)建敵機(jī)圖片,寬高,坐標(biāo)等屬性 private ImageIcon enemyImage = new ImageIcon("img/錯(cuò)誤.png"); private int width = enemyImage.getIconWidth(); private int height = enemyImage.getIconHeight(); private int x; private int y; public ImageIcon getEnemyImage() { return enemyImage; } public void setEnemyImage(ImageIcon enemyImage) { this.enemyImage = enemyImage; } 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 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 Enemy(){ //創(chuàng)建隨機(jī)類,在窗口寬高范圍內(nèi)隨機(jī) Random random = new Random(); //隨機(jī)出來(lái)的敵機(jī)位置可以顯示一半圖片 x = random.nextInt(GameMain.WIDTH-width/2); y = -random.nextInt(GameMain.HEIGHT-height/2); //初始敵機(jī)位置為負(fù)數(shù) } public void drawImage(Graphics g) { //繪制敵機(jī)圖片 g.drawImage(enemyImage.getImage(), x, y, null); } public void move() { y +=2; //控制y軸速度 } }
Bullet類
package P; import java.awt.Graphics; import javax.swing.ImageIcon; //創(chuàng)建子彈類 public class Bullet { //創(chuàng)建屬性,子彈圖片,寬高,位置 private ImageIcon bulletImage = new ImageIcon("img/aaa.gif"); private int width = bulletImage.getIconWidth(); private int height = bulletImage.getIconHeight(); private int x; private int y; public ImageIcon getBulletImage() { return bulletImage; } public void setBulletImage(ImageIcon bulletImage) { this.bulletImage = bulletImage; } 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 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 Bullet(int x,int y) { //x,y軸位置從外面?zhèn)魅耄ǖ扔谟⑿奂?jí)的位置) this.x = x; this.y = y; } public void drawImage(Graphics g) { g.drawImage(bulletImage.getImage(), x, y, null); } public void move() { x-=1; y -= 2; //讓子彈y軸從下往上遞減 } public void move1() { x+=1; y -= 2; //讓子彈y軸從下往上遞減 } }
爆炸類bomb類
package P; import java.awt.Graphics; import javax.swing.ImageIcon; //創(chuàng)建爆炸的實(shí)體了 public class Bomb { //爆炸的圖片,寬高,位置屬性 private ImageIcon bombImage=new ImageIcon("img/zz.png"); private int width = bombImage.getIconWidth(); private int height = bombImage.getIconHeight(); private int x; private int y; private int count; //記錄多少次數(shù)后爆炸 public ImageIcon getBombImage() { return bombImage; } public void setBombImage(ImageIcon bombImage) { this.bombImage = bombImage; } 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 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 getCount() { return count; } public void setCount(int count) { this.count = count; } public Bomb(int x,int y){ this.x = x; this.y = y; } public void drawImage(Graphics g) { g.drawImage(bombImage.getImage(), x, y, null); } public void move() { count++; } }
“java如何實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。