Java實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲(敵機(jī)下落篇)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)游戲,敵機(jī)下落的具體代碼,供大家參考,具體內(nèi)容如下
在實(shí)現(xiàn)這個(gè)游戲之前,我們首先需要知道項(xiàng)目可能要用到哪些知識(shí)點(diǎn):
重繪,線程,雙緩沖,數(shù)據(jù)結(jié)構(gòu)的應(yīng)用
差不多是這大概有這些,如果不夠的話我們?cè)偌印?br />首先,我們應(yīng)該實(shí)現(xiàn)敵機(jī)下落,在這里大概思路和利用線程使小球下落差不多。不同的是,我在這里用到了三種敵機(jī),分別為小、大、BOSS機(jī)三種。然后給予這三種敵機(jī)不同的下落規(guī)則(即速度、出現(xiàn)的時(shí)間、是否伴隨子彈的發(fā)射等等)來(lái)給游戲適當(dāng)?shù)脑黾狱c(diǎn)難度。
以下是我的大概設(shè)計(jì)思路和下落規(guī)則,大家可以參考一下
1.飛機(jī)是在一開(kāi)始的時(shí)候就出現(xiàn)的,先出現(xiàn)什么飛機(jī)?速度?什么位置出現(xiàn)?頻率?
先出小飛機(jī),再出大飛機(jī),當(dāng)所有的大小飛機(jī)都消失以后出現(xiàn)BOSS機(jī).
小飛機(jī)的速度較快:8 大飛機(jī)的速度較慢:4 BOSS機(jī)的速度最慢:2
頻率:每隔1秒出現(xiàn)一架小飛機(jī),每出現(xiàn)4架小飛機(jī)后出現(xiàn)一架大飛機(jī),最后出現(xiàn)BOSS機(jī).
位置:在窗體的范圍內(nèi),考慮到png圖片的高度和寬度,所以出現(xiàn)的時(shí)候注意減去相應(yīng)的高度和寬度.
2.小飛機(jī),大飛機(jī),BOSS機(jī)需要使用不同的vector類來(lái)保存,取出的時(shí)候再使用一個(gè)vector類來(lái)保存(保存的時(shí)候應(yīng)該將三個(gè)vector類中原來(lái)的飛機(jī)移除).再將保存起來(lái)的飛機(jī)依次進(jìn)行畫(huà)出.當(dāng)出現(xiàn)4架小飛機(jī)的時(shí)候就出現(xiàn)一架大飛機(jī),當(dāng)小飛機(jī)和大飛機(jī)都出現(xiàn)完了以后,出現(xiàn)BOSS機(jī).注意:大飛機(jī)和BOSS機(jī)的時(shí)候伴隨著子彈的發(fā)出.
3.子彈:
子彈在大飛機(jī)和BOSS機(jī)出現(xiàn)的時(shí)候出現(xiàn).且速度優(yōu)于大飛機(jī)和BOSS機(jī).因?yàn)樾★w機(jī)沒(méi)有子彈,在這里我們利用byte標(biāo)記來(lái)區(qū)別于小飛機(jī)和其他兩種飛機(jī),另外也需要將子彈標(biāo)記,以防止子彈生成子彈。
4.使用兩個(gè)線程:
(1)一個(gè)線程用來(lái)實(shí)現(xiàn)將不同的vector向量取出的飛機(jī)(先將大小飛機(jī)取出以后,再取出BOSS機(jī),每取一次小飛機(jī)的時(shí)候?qū)ount記一次數(shù),當(dāng)count==4的時(shí)候,取出一架大飛機(jī).當(dāng)小飛機(jī)和大飛機(jī)全部取完以后再取出BOSS機(jī)),并保存在一個(gè)統(tǒng)一的vector向量中.
(2)另一個(gè)線程用來(lái)實(shí)現(xiàn)畫(huà)飛機(jī)和子彈并使其移動(dòng).
5.效果圖出現(xiàn)過(guò)于閃爍的情況可以利用雙緩沖技術(shù)來(lái)減少閃爍。
以下是代碼主類,主要生成窗體和敵機(jī),并將敵機(jī)保存在不同的vector向量中。
public class BallMain { ?? ?public Graphics g; ?? ?public Vector<Ball> vector, minVector, maxVector, bossVector; ?? ?public ImageIcon img; ?? ?public static void main(String[] args) { ?? ??? ?BallMain bm = new BallMain(); ?? ??? ?bm.init(); ?? ?} ?? ?public void init() { ?? ??? ?JFrame frame = new JFrame(); ?? ??? ?frame.setTitle("飛機(jī)大戰(zhàn)"); ?? ??? ?frame.setSize(800, 800); ?? ??? ?frame.setDefaultCloseOperation(3); ?? ??? ?frame.setLocationRelativeTo(null); ?? ??? ?frame.setBackground(Color.white); ?? ??? ?frame.setResizable(false); // 不允許改變窗體大小 ?? ??? ?frame.setVisible(true); ?? ??? ?this.g = frame.getGraphics(); ?? ??? ?minVector = new Vector<Ball>(); ?? ??? ?maxVector = new Vector<Ball>(); ?? ??? ?bossVector = new Vector<Ball>(); ?? ??? ?vector = new Vector<Ball>(); ?? ??? ?initPlane(frame); ?? ??? ?BallThread bt = new BallThread(vector, minVector, maxVector, ?? ??? ??? ??? ?bossVector, frame); ?? ??? ?bt.start(); ?? ??? ?BallGraThread bll = new BallGraThread(vector, frame, ?? ??? ??? ??? ?frame.getGraphics()); ?? ??? ?bll.start(); ?? ?} ?? ?public void initPlane(JFrame frame) { ?? ??? ?Random rand = new Random(); ?? ??? ?// 小飛機(jī)的生成 ?? ??? ?for (int i = 0; i < 8; i++) { ?? ??? ??? ?ImageIcon icon = new ImageIcon("plane/plane0.png"); ?? ??? ??? ?int r = icon.getIconWidth() / 2; ?? ??? ??? ?int x = rand.nextInt(frame.getWidth() - icon.getIconWidth()) + r; ?? ??? ??? ?int y = -icon.getIconHeight(); ?? ??? ??? ?int vy = 8; ?? ??? ??? ?Ball b = new Ball(icon.getImage(), x, y, r, vy, (byte) 1); ?? ??? ??? ?minVector.add(b); ?? ??? ?} ?? ??? ?// 大飛機(jī)的生成 ?? ??? ?for (int i = 0; i < 2; i++) { ?? ??? ??? ?ImageIcon icon = new ImageIcon("plane/plane1.png"); ?? ??? ??? ?int r = icon.getIconWidth() / 2; ?? ??? ??? ?int x = rand.nextInt(frame.getWidth() - icon.getIconWidth()) + r; ?? ??? ??? ?int y = -icon.getIconHeight(); ?? ??? ??? ?int vy = 4; ?? ??? ??? ?Ball b = new Ball(icon.getImage(), x, y, r, vy, (byte) 2); ?? ??? ??? ?maxVector.add(b); ?? ??? ?} ?? ??? ?// Boss機(jī)的生成 ?? ??? ?ImageIcon icon = new ImageIcon("plane/plane2.png"); ?? ??? ?int r = icon.getIconWidth() / 2; ?? ??? ?int x = rand.nextInt(frame.getWidth() - icon.getIconWidth()) + r; ?? ??? ?int y = -icon.getIconHeight(); ?? ??? ?int vy = 2; ?? ??? ?Ball b = new Ball(icon.getImage(), x, y, r, vy, (byte) 3); ?? ??? ?bossVector.add(b); ?? ?} }
以下為飛機(jī)的下落規(guī)則:
public BallThread(Vector<Ball> vector, Vector<Ball> minVector, ?? ??? ??? ?Vector<Ball> maxVector, Vector<Ball> bossVector, JFrame frame) { ?? ??? ?this.vector = vector; ?? ??? ?this.minVector = minVector; ?? ??? ?this.maxVector = maxVector; ?? ??? ?this.bossVector = bossVector; ?? ??? ?this.frame = frame; ?? ?} ?? ?public void run() { ?? ??? ?int count = 0; ?? ??? ?while (true) { ?? ??? ??? ?if (minVector.size() > 0) { ?? ??? ??? ??? ?vector.add(minVector.remove(0)); ?? ??? ??? ??? ?++count; ?? ??? ??? ??? ?if (count == 4) { ?? ??? ??? ??? ??? ?if (maxVector.size() > 0) { ?? ??? ??? ??? ??? ??? ?vector.add(maxVector.remove(0)); ?? ??? ??? ??? ??? ??? ?count = 0; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?if (vector.size() == 0) { ?? ??? ??? ??? ?vector.add(bossVector.remove(0)); ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?try { ?? ??? ??? ??? ?Thread.sleep(1000); ?? ??? ??? ?} catch (InterruptedException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
利用雙緩沖減少閃爍,畫(huà)出飛機(jī)并擦除,使其不斷更新位置:
public class BallGraThread extends Thread { ?? ?public Vector<Ball> vector; ?? ?public JFrame frame; ?? ?public Graphics g; ?? ?public BufferedImage image; ?? ?public Graphics ig; ?? ?public BallGraThread(Vector<Ball> vector, JFrame frame, Graphics g) { ?? ??? ?this.vector = vector; ?? ??? ?this.frame = frame; ?? ??? ?this.g = g; ?? ?} ?? ?public void run() { ?? ??? ?if (image == null) { ?? ??? ??? ? ?? ??? ??? ?//創(chuàng)建一個(gè)帶透明色的BufferedImage對(duì)象 ?? ??? ??? ?image = new BufferedImage(frame.getWidth(), frame.getHeight(), ?? ??? ??? ??? ??? ?BufferedImage.TYPE_INT_ARGB); ?? ??? ??? ?//獲取畫(huà)筆對(duì)象. ?? ??? ??? ?ig = image.getGraphics(); ?? ??? ?} ?? ??? ?while (true) { ?? ??? ??? ?//擦除. ?? ??? ??? ?ig.setColor(frame.getContentPane().getBackground()); ?? ??? ??? ?ig.fillRect(0, 0, frame.getWidth(), frame.getHeight()); ?? ??? ??? ?for (int i = 0; i < vector.size(); i++) { ?? ??? ??? ??? ?Ball ball = vector.get(i); ?? ??? ??? ??? ?ball.draw(ig); ?? ??? ??? ??? ?ball.move(vector, frame, ig); ?? ??? ??? ?} ?? ??? ??? ?g.drawImage(image, 0, 0, frame); ?? ??? ??? ?try { ?? ??? ??? ??? ?Thread.sleep(100); ?? ??? ??? ?} catch (InterruptedException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
子彈類,標(biāo)記子彈來(lái)區(qū)分三種敵機(jī),當(dāng)飛機(jī)和子彈飛出窗體外后將其移除與vector:
public class Ball { ?? ?public int x, y; ?? ?public int flag = 1; ?? ?public Color color; ?? ?public Image img; ?? ?public BulletThread ba; ?? ?int r; ?? ?int vy; ?? ?byte type; ?? ?Random random = new Random(); ?? ?public Ball(Image img, int x, int y, int r, int vy, byte type) { ?? ??? ?this.img = img; ?? ??? ?this.x = x; ?? ??? ?this.y = y; ?? ??? ?this.r = r; ?? ??? ?this.vy = vy; ?? ??? ?this.type = type; ?? ?} ?? ?public void move(Vector<Ball> vector, JFrame frame, Graphics g) { ?? ??? ?if (y >= 0 && flag == 1) { ?? ??? ??? ?// 如果是大飛機(jī)或者BOSS機(jī)的話,就發(fā)射子彈. ?? ??? ??? ?if (type == 2 || type == 3) { ?? ??? ??? ??? ?ba = new BulletThread(vector, this); ?? ??? ??? ??? ?ba.start(); ?? ??? ??? ??? ?flag = 2; ?? ??? ??? ??? ?System.out.println(ba); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (y >= frame.getHeight()) { ?? ??? ??? ?img = new ImageIcon("planne/bullet.png").getImage(); ?? ??? ??? ?g.drawImage(img, x, y, 2 * r, 2 * r, null); ?? ??? ??? ?System.out.println(ba); ?? ??? ??? ?if (type == 2 || type == 3) ?? ??? ??? ??? ?ba.stopFlag = false; ?? ??? ??? ?vector.remove(this); ?? ??? ?} ?? ??? ?y = y + vy; ?? ?} ?? ?public void draw(Graphics g) { ?? ??? ?// 需要將ImageIcon轉(zhuǎn)為Image類型的. ?? ??? ?g.drawImage(img, x, y, 2 * r, 2 * r, null); ?? ?} }
畫(huà)出子彈并添加于vector向量中,使其移動(dòng):
public class BulletThread extends Thread { ?? ?public Vector<Ball> vector; ?? ?public Ball ball;// 大飛機(jī)的數(shù)據(jù)來(lái)生成子彈 ?? ?public boolean stopFlag = true; ?? ?public boolean pauseFlag = true; ?? ?public BulletThread(Vector<Ball> vector, Ball ball) { ?? ??? ?this.vector = vector; ?? ??? ?this.ball = ball; ?? ?} ?? ?public void run() { ?? ??? ?while (stopFlag) { ?? ??? ??? ?if (pauseFlag) { ?? ??? ??? ??? ?int x = ball.x + ball.r; ?? ??? ??? ??? ?int y = ball.y + 2 * ball.r; ?? ??? ??? ??? ?int r = 20; ?? ??? ??? ??? ?Image img = new ImageIcon("plane/bullet.png").getImage(); ?? ??? ??? ??? ?int vy = ball.vy + 4; ?? ??? ??? ??? ?Ball bullet = new Ball(img, x, y, r, vy, (byte) 0); ?? ??? ??? ??? ?vector.add(bullet); ?? ??? ??? ?} ?? ??? ??? ?try { ?? ??? ??? ??? ?Thread.sleep(4000); ?? ??? ??? ?} catch (InterruptedException e) { ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
以上差不多就是敵機(jī)的下落,其他的一些功能尚未完善,比如可以給敵機(jī)添加不同的血量再來(lái)增加難度,大家可以根據(jù)自己的發(fā)揮來(lái)寫出屬于自己的飛機(jī)大戰(zhàn)游戲。
希望這篇文章對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲(控制主飛機(jī)篇)
- Java開(kāi)發(fā)實(shí)現(xiàn)飛機(jī)大戰(zhàn)
- 基于Java語(yǔ)言在窗體上實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲的完整步驟
- Java實(shí)現(xiàn)游戲飛機(jī)大戰(zhàn)-III的示例代碼
- Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-II游戲詳解
- Java實(shí)現(xiàn)經(jīng)典游戲飛機(jī)大戰(zhàn)-I的示例代碼
- 一天時(shí)間用Java寫了個(gè)飛機(jī)大戰(zhàn)游戲,朋友直呼高手
- java實(shí)戰(zhàn)之飛機(jī)大戰(zhàn)小游戲(源碼加注釋)
- java實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
- java實(shí)現(xiàn)簡(jiǎn)易飛機(jī)大戰(zhàn)
相關(guān)文章
IntelliJ IDEA中顯示和關(guān)閉工具欄與目錄欄的方法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中顯示和關(guān)閉工具欄與目錄欄的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10圖解Java中歸并排序算法的原理與實(shí)現(xiàn)
歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide and Conquer)的一個(gè)非常典型的應(yīng)用。本文將通過(guò)圖片詳解插入排序的原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08MybatisPlus使用注解的多對(duì)多級(jí)聯(lián)查詢方式
這篇文章主要介紹了MybatisPlus使用注解的多對(duì)多級(jí)聯(lián)查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringSecurity定義多個(gè)過(guò)濾器鏈的操作代碼
Spring?Security?是?Spring家族中的一個(gè)安全管理框架。相比與另外一個(gè)安全框架Shiro,它提供了更豐富的功能,社區(qū)資源也比Shiro豐富,今天通過(guò)本文給大家介紹SpringSecurity定義多個(gè)過(guò)濾器鏈的實(shí)例,感興趣的朋友跟隨小編一起看看吧2023-04-04