Java编写飞机大战(超级详细的完整版)--附源码及图片
生活随笔
收集整理的這篇文章主要介紹了
Java编写飞机大战(超级详细的完整版)--附源码及图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、基本介紹
- 二、源碼分析
- 三、總結
一、基本介紹
首先,先想想要完成一個飛機大戰,需要完成那些步驟呢?
①是不是需要有個界面去顯示飛機和子彈之類的組件呢?
這個時候是不是需要去繪制一個界面類呢? 答案是必須的!!!
②既然畫完了界面,那界面的上的飛機、子彈、敵機啥的是不是也需要創建一個類去繪制呢?答案也是必須的!!!
在繪制子彈和敵機的時候,需要去考慮敵機遇到子彈爆炸的場景,以及每擊毀一架飛機,怎么算我的得分的情況,這些都是需要去考慮的。
通過飛機大戰能夠學到的知識點有:
①面向對象:其中包括了繼承、實現接口以及封裝;
②學會使用隊列ArrayList進行存儲;
③明白如何讀取圖片,然后將該圖片放在窗體上;
④學會使用多線程。
二、源碼分析
①先繪制飛機大戰的界面類GameMain類。
package FlyV4B;import javax.swing.ImageIcon; import javax.swing.JFrame;public class GameMain {//窗口的寬度和高度static int width = 600;static int height = 800;public static void main(String[] args) {//創建一個窗口的大小JFrame frame = new JFrame();//設置標題frame.setTitle("飛機大戰");//設置窗口的大小frame.setSize(width, height);//關閉窗口關閉后JVM終止運行frame.setDefaultCloseOperation(3);//設置窗口居中frame.setLocationRelativeTo(null);//創建JPanel容器GamePanel panel = new GamePanel();//把JPanel添加到窗口中frame.add(panel);//添加鼠標監聽器frame.addMouseMotionListener(panel);//設置可見frame.setVisible(true);//初始化容器panel.init();} }在界面類中添加鼠標移動監聽器,目的是鼠標放到界面上或者按壓界面時能夠使得飛機進行移動。
②英雄機類–GamePanel類
package FlyV4B;import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.List; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList;import javax.swing.ImageIcon; import javax.swing.JPanel;public class GamePanel extends JPanel implements MouseMotionListener{//讀取英雄機的圖片ImageIcon heroImage = new ImageIcon("image/hero.png");//定義一個集合來裝所有的敵機ArrayList<Enemy> enemys = new ArrayList();//定義一個隊列來裝所有的子彈ArrayList<Bullet> bullets = new ArrayList();//定義一個集合來裝爆炸的圖片ArrayList<Bomb> bombs = new ArrayList();//英雄機的坐標int heroX = 300;int heroY = 400;private int number=0;//分數public GamePanel() {//創建10個敵人for(int i=0; i<10; i++){enemys.add(new Enemy());}}//鼠標按下去拖動時調用public void mouseDragged(MouseEvent e) {System.out.println("鼠標按下去時拖動時調用");int x = e.getX();//獲取鼠標的xint y = e.getY();heroX = x-(heroImage.getIconWidth()/2); //把鼠標的x賦值給英雄機heroY = y-(heroImage.getIconHeight()/2);if(x+heroImage.getIconWidth() > GameMain.width) {heroX = GameMain.width - heroImage.getIconWidth() - 20; }if(y+heroImage.getIconHeight() > GameMain.height) {heroY = GameMain.height - heroImage.getIconHeight() - 20; }repaint();//x發生變化后需要重新繪制}//鼠標移動時調用public void mouseMoved(MouseEvent e) {System.out.println("鼠標放上去時調用");int x = e.getX();//獲取鼠標的x值int y = e.getY();//獲取鼠標的y值heroX = x-(heroImage.getIconWidth()/2); //把鼠標的x賦值給英雄機heroY = y-(heroImage.getIconHeight()/2);//把鼠標的y賦值給英雄機if(x+heroImage.getIconWidth() > GameMain.width) {heroX = GameMain.width - heroImage.getIconWidth() - 20; }if(y+heroImage.getIconHeight() > GameMain.height) {heroY = GameMain.height - heroImage.getIconHeight() - 20; }repaint();//x發生變化后需要重新繪制}//碰撞方法public boolean isHit(Enemy e, Bullet b) {//指定一個區域Rectangle rect = new Rectangle(e.getX(), e.getY(), e.getWidth(), e.getHeight());//表示(x,y)坐標空間中的位置的點Point p = new Point(b.getX()+b.getWidth()/2, b.getY()+b.getHeight());return rect.contains(p);}//重寫paint方法,做繪制圖片使用public void paint(Graphics g) {super.paint(g);g.setFont(new Font("", Color.RED.getRed(), 30));g.drawString("得分"+number, 20, 30);//1.繪制英雄機g.drawImage(heroImage.getImage(), heroX, heroY, null);//2.繪制敵機for(int i=0; i<enemys.size(); i++) {Enemy enemy = enemys.get(i);enemy.drawImage(g);//重新繪制}//3.繪制子彈for(int i=0; i<bullets.size(); i++) {Bullet bullet = bullets.get(i);bullet.drawImage(g);}//4.繪制爆炸圖片for(int i=0; i<bombs.size(); i++) {Bomb bomb = bombs.get(i);bomb.drawImage(g);}}/** init這個方法做初始化方法使用* 創建一些組件(英雄機,子彈,敵人)* */public void init() {int flag = 0;while(true) {flag++;//每循環20次創建一個子彈if(flag % 15 == 0) {//創建一些子彈Bullet bullet = new Bullet(heroX+heroImage.getIconWidth()/2, heroY);//把子彈添加到集合中bullets.add(bullet);}//讓敵機往下移動for(int i=0; i<enemys.size(); i++) {Enemy enemy = enemys.get(i);enemy.move();//改變敵機的y值//判斷敵機的y值是否大于整個窗口的高度if(enemy.getY() > GameMain.height){//刪除敵機enemys.remove(enemy);//再添加一個新的敵機enemys.add(new Enemy());}}//讓子彈飛起來for(int i=0; i<bullets.size(); i++){Bullet tempBullet = bullets.get(i);tempBullet.move();}System.out.println("子彈數量:"+bullets.size());//刪除越界的子彈for(int i = 0; i<bullets.size(); i++) {Bullet bullet = bullets.get(i);if(bullet.getY() < 0) {//y軸小于零說明越界了bullets.remove(bullet);}}//處理子彈碰撞到敵機的效果for(int i=0; i<enemys.size(); i++) {Enemy enemy = enemys.get(i);//敵機for(int j=0; j<bullets.size(); j++) {Bullet bullet = bullets.get(j);//得到子彈if(isHit(enemy, bullet)){//先刪除敵機enemys.remove(enemy);//先刪除擊中的敵機enemys.add(new Enemy());//再添加一個新的敵機bullets.remove(bullet);//刪除子彈//創建一個爆炸圖片的對象Bomb bomb = new Bomb(enemy.getX(), enemy.getY());bombs.add(bomb);//添加到集合中number+=10;//每次碰撞加10分}}}//刪除爆炸的圖片for(int i=0; i<bombs.size(); i++) {Bomb bomb = bombs.get(i);bomb.move();if(bomb.getCount()>5) {bombs.remove(bomb);}}try {Thread.sleep(5);} catch (Exception e) {e.printStackTrace();}repaint();} } }③敵機類–Enemy類
package FlyV4B;import java.awt.Graphics; import java.util.Random;import javax.swing.ImageIcon;/** 敵人對象* * */ public class Enemy {private int width;//敵人圖片的寬度private int height;//敵人圖片的高度//敵人的坐標private int x;private int y;//private ImageIcon enemyImageIcon = new ImageIcon("image/enemy.png");public Enemy() {this.width = enemyImageIcon.getIconWidth();this.height = enemyImageIcon.getIconHeight();//設置敵機的位置Random random = new Random();random.nextInt(10);this.x = random.nextInt(GameMain.width - (width/2));this.y = -random.nextInt(GameMain.height - (height/2));}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 void move() {this.y += 1; //速度}public void drawImage(Graphics g){g.drawImage(enemyImageIcon.getImage(), x, y, null);} }④子彈類–Bullet類
package FlyV4B;import java.awt.Graphics;import javax.swing.ImageIcon;/** 子彈對象* */public class Bullet {private int x;private int y;private int width;private int height;private ImageIcon bulletImageIcon = new ImageIcon("image/bullet.png");public Bullet(int x, int y) {this.x = x;this.y = y;this.width = bulletImageIcon.getIconWidth();this.height = bulletImageIcon.getIconHeight();}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 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 ImageIcon getBulletImageIcon() {return bulletImageIcon;}public void setBulletImageIcon(ImageIcon bulletImageIcon) {this.bulletImageIcon = bulletImageIcon;}public void move() {this.y -= 4;}public void drawImage(Graphics g) {g.drawImage(bulletImageIcon.getImage(), x, y, null);} }⑤爆炸圖效果類–Bomb類
package FlyV4B;import java.awt.Graphics;import javax.swing.ImageIcon;public class Bomb {private int x;private int y;private int width;private int height;private ImageIcon bombimg = new ImageIcon("image/bomb.png");private int count;//刪除的次數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;this.width = bombimg.getIconWidth();this.height = bombimg.getIconHeight();}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 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 ImageIcon getBombimg() {return bombimg;}public void setBombimg(ImageIcon bombimg) {this.bombimg = bombimg;}public void drawImage(Graphics g) {g.drawImage(bombimg.getImage(), x, y, null); }public void move() {count++;} }三、總結
通過上述的五個類,就能夠畫出接下來這樣的飛機大戰的效果,如下圖:
附件:飛機源碼+圖片(親測有效)密碼: f909
總結
以上是生活随笔為你收集整理的Java编写飞机大战(超级详细的完整版)--附源码及图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超易懂数据库范式
- 下一篇: LINUX下用脚本实现JDK+TOMCA