java實現飛機大戰小遊戲
本文實例為大傢分享瞭java實現飛機大戰遊戲的具體代碼,供大傢參考,具體內容如下
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 //創建面板類,繼承面板,實現移動監聽事件 public class MyPanel extends JPanel implements MouseMotionListener { //private ImageIcon bjImage = new ImageIcon("img/bj.png"); //英雄機圖片 private ImageIcon heroImage = new ImageIcon("img/頭.png"); //顯示logo //private ImageIcon logoImage = new ImageIcon("img/頭.png"); //英雄機的寬高 private int width = heroImage.getIconWidth(); private int height = heroImage.getIconHeight(); //英雄機的坐標 private int x=180; private int y=880; //創建一個敵機的集合,用於展示多個敵機 List<Enemy> enemys = new ArrayList<>(); //存儲子彈集合 List<Bullet> bullets = new ArrayList<>(); //存儲爆炸對象集合 List<Bomb> bombs = new ArrayList<>(); //存儲英雄機爆炸集合 List<HeroDead> heroDeads = new ArrayList<>(); private int number;//統計當前得分 private int m;//統計剩餘血條量 // MyPanel jp = new MyPanel() { // { // setBackground(Color.gray);// 設置面板背景顏色 // } public MyPanel() { //在構造方法中準備10個敵機 for(int i=0;i<10;i++){ enemys.add(new Enemy()); } } @Override public void paint(Graphics g) { //用於繪制圖片區域 super.paint(g); //在窗口左側展示打飛機得分 //設置字體: 參數1:字體傢族, 參數2:字體樣式:加粗 參數3:字體大小 g.setFont(new Font("宋體", Font.BOLD, 30)); g.drawString("當前得分:"+number, 5, 30); g.drawString("發量(萬根):"+(5-m), 5, 80); //g.drawImage(null, x, y, getBackground(), getFocusCycleRootAncestor()); //繪制英雄機圖片 參數1,圖片 參數2和3:坐標 g.drawImage(heroImage.getImage(), x, y, null); //g.drawImage(logoImage.getImage(), 19, 22, null); //在繪圖中顯示10輛敵機 for(int i=0;i<enemys.size();i++){ Enemy enemy = enemys.get(i); //獲取敵機對象 enemy.drawImage(g); //然後分別展示 } //展示子彈集合的圖片 for (int i = 0; i < bullets.size(); i++) { Bullet bullet = bullets.get(i); //取出子彈對象 bullet.drawImage(g); } //展示爆炸集合的圖片 for(int i=0;i<bombs.size();i++){ Bomb bomb = bombs.get(i); bomb.drawImage(g); } //展示英雄機的銷毀圖片 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("鼠標移動並拖拽觸發"); //鼠標拖拽時,需要將英雄機也帶著移動(隻需改變x軸和y軸) x = e.getX()-width/2; //英雄機的移動,隨著鼠標觸發移動的 y = e.getY()-height/2; //鼠標指定到英雄機中間位置 repaint(); //重新繪制圖片 } @Override public void mouseMoved(MouseEvent e) { //System.out.println("鼠標移動的觸發.."); //鼠標移動時,需要將英雄機也帶著移動(隻需改變x軸和y軸) x = e.getX()-width/2; //英雄機的移動,隨著鼠標觸發移動的 y = e.getY()-height/2; //鼠標指定到英雄機中間位置 repaint(); //重新繪制圖片 } //L-4. 創建子彈類Bullet,操作與敵機類類似 public void init() { //定義一個標記,循環瞭自定義的次數後,才去添加,這樣子彈數量會變少 int flag = 0; while(true) { //循環地跑,模擬依次移動的效果 flag++; // if(flag==50) { //控制子彈數量 bullets.add(new Bullet(x+width/2, y)); //就是從英雄級的x,y軸位置發射子彈的 flag=0; //又回到0,依次計算15次 //System.out.println("子彈數量:"+ bullets.size()); } //展示子彈,讓子彈飛起來 for(int i=0;i<bullets.size();i++) { Bullet bullet = bullets.get(i); //取出對象 if(i%2!=0)//設置子彈左右發射 bullet.move(); else bullet.move1();//移動子彈位置 //如果子彈移動到y軸為0,則移除掉 if(bullet.getY()<0){ bullets.remove(bullet); //移除子彈對象 } } //xx.3. //敵機循環移動.. for(int i=0;i<enemys.size();i++) { Enemy en = enemys.get(i); en.move(); //循環地移動每一架敵機y軸位置 //超出屏幕范圍後,需要移除,並重新添加一個對象 if(en.getY()>GameMain.HEIGHT) { //int count1=enemys.size(); enemys.remove(en); //移除對象 enemys.add(new Enemy()); //重新再創建對象 } //L 3-4 //在敵機的循環中,再繼續循環子彈; 需要判斷是否有子彈和敵機重疊瞭, //則移除敵機對象,重新添加,移除子彈;如果有則添加爆炸對象 for(int j=0;j<bullets.size();j++){ Bullet bu = bullets.get(j); if(isHit(en,bu)){ //碰撞的判斷 enemys.remove(en); //移除敵機,並重新new一個 enemys.add(new Enemy()); bullets.remove(bu); //移除子彈 //添加的爆炸位置和敵機位置一致 bombs.add(new Bomb(en.getX(), en.getY())); number += 10; //爆炸後,累加得分 } } //zz-6 //判斷英雄機與敵機的碰撞(英雄機的消亡) if(isHit(en)){ System.out.println("進入英雄機爆炸..."); //敵機的對象移除 enemys.remove(en); //英雄機爆炸圖片位置應該與英雄機重疊 heroDeads.add(new HeroDead(x, y)); for(m=0;m<=heroDeads.size();m++) { while(heroDeads.size()==5) { return; } } //遊戲結束,跳出死循環 } } //L-5 //將爆炸的所有對象,過一段時間則幹掉 for(int i=0;i<bombs.size();i++){ Bomb bomb = bombs.get(i); bomb.move(); //計算次數,統一循環多少次後,再幹掉 if(bomb.getCount()>6){ bombs.remove(bomb); //在循環的一定范圍後,可以移除爆炸瞭 } } //xx.2 //每次的移動都需要停頓一下 try { Thread.sleep(6); //單位:毫秒 睡眠6毫秒 } catch (InterruptedException e) { e.printStackTrace(); } repaint(); //重新繪制圖片 } } //zz-6 //英雄機與敵機的碰撞 private boolean isHit(Enemy en) { //英雄機的碰撞區域 Rectangle rect = new Rectangle(x, y, width, height); //碰撞點的位置,是在敵機的中心點 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) { //填充敵機的碰撞區域 Rectangle rect = new Rectangle(en.getX(), en.getY(), en.getWidth(), en.getHeight()); //將子彈的位置設置在中間 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; //設置靜態常量,作為狀態值使用 static final int HEIGHT = 660; public static void main(String[] args) { JFrame jFrame = new JFrame(); //實例化頂級窗口類 jFrame.setSize(WIDTH, HEIGHT); //設置寬高像素 jFrame.setTitle("小禿頭歷險記"); //設置標題 jFrame.setLocationRelativeTo(null); //設置居中效果 //JFrame.EXIT_ON_CLOSE: 狀態值 3 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關閉窗口的同時,把程序關閉 //在窗口中,加入面板 MyPanel pl = new MyPanel(); pl.setBackground(Color.pink); pl.setBorder(BorderFactory.createLineBorder(Color.cyan, 3)); jFrame.add(pl); //添加面板組件 jFrame.addMouseMotionListener(pl); //添加鼠標移動事件 jFrame.setVisible(true); //使窗口可視化 pl.init(); //在面板中循環將組件跑起來 } }
Enemy類
package P; import java.awt.Graphics; import java.util.Random; import javax.swing.ImageIcon; //創建敵機類 public class Enemy { //創建敵機圖片,寬高,坐標等屬性 private ImageIcon enemyImage = new ImageIcon("img/錯誤.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(){ //創建隨機類,在窗口寬高范圍內隨機 Random random = new Random(); //隨機出來的敵機位置可以顯示一半圖片 x = random.nextInt(GameMain.WIDTH-width/2); y = -random.nextInt(GameMain.HEIGHT-height/2); //初始敵機位置為負數 } public void drawImage(Graphics g) { //繪制敵機圖片 g.drawImage(enemyImage.getImage(), x, y, null); } public void move() { y +=2; //控制y軸速度 } }
Bullet類
package P; import java.awt.Graphics; import javax.swing.ImageIcon; //創建子彈類 public class Bullet { //創建屬性,子彈圖片,寬高,位置 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軸位置從外面傳入(等於英雄級的位置) 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; //創建爆炸的實體瞭 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; //記錄多少次數後爆炸 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++; } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- java實現雷霆戰機
- Java實現飛機大戰-II遊戲詳解
- java實現飛機大戰遊戲
- 解決java Graphics drawImage 無法顯示圖片的問題
- 一天時間用Java寫瞭個飛機大戰遊戲,朋友直呼高手