欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例詳解

 更新時(shí)間:2021年03月22日 11:12:22   作者:復(fù)雜先森*  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)飛機(jī)大戰(zhàn)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

飛機(jī)大戰(zhàn)是一個(gè)非常經(jīng)典的案例,因?yàn)樗硕喾N新手需要掌握的概念,是一個(gè)非常契合面向?qū)ο笏枷氲娜腴T(mén)練習(xí)案例

程序分析:

在此游戲中共有六個(gè)對(duì)象:

小敵機(jī)Airplane,大敵機(jī)BigAirplane,小蜜蜂Bee,天空Sky,英雄機(jī)Hero,子彈Bullet

其次我們還需要三個(gè)類:

超類Flyer,圖片類Images,測(cè)試類World

還需:

英雄機(jī)2張,小敵機(jī),大敵機(jī),小蜜蜂,子彈,天空各1張,爆炸圖4張,游戲開(kāi)始,暫停,游戲結(jié)束各1張,共14張圖片放入與圖片類Images同包中

超類Flyer:

此類是用來(lái)封裝所有對(duì)象共有的行為及屬性的
不管是寫(xiě)什么程序,都建議遵循兩點(diǎn):數(shù)據(jù)私有化,行為公開(kāi)化

import java.util.Random;
import java.awt.image.BufferedImage;

public abstract class Flyer {
 //所有對(duì)象都有三種狀態(tài):活著的,死了的,及刪除的
 //這里之所以選擇用常量表示狀態(tài)是因?yàn)槭紫葼顟B(tài)是一個(gè)不需要去修改的值
 //其次狀態(tài)需要反復(fù)使用所以結(jié)合這兩個(gè)特點(diǎn),我選擇了使用常量表示
 //state是用來(lái)表示當(dāng)前狀態(tài)的,每個(gè)對(duì)象都有一個(gè)實(shí)時(shí)的狀態(tài),此狀態(tài)是會(huì)改變的,且初始狀態(tài)都是活著的
 public static final int LIVE = 0;//活著的
 public static final int DEAD = 1;//死了的
 public static final int REMOVE = 2;//刪除的
 protected int state = LIVE;//當(dāng)前狀態(tài)(默認(rèn)狀態(tài)為活著的)

 每個(gè)對(duì)象都是一張圖片,既然是圖片那么就一定有寬高,其次因?yàn)槊總€(gè)對(duì)象都是會(huì)隨時(shí)移動(dòng)的 即為都有x,y坐標(biāo)
 protected int width;//寬
 protected int height;//高
 protected int x;//左右移動(dòng)(x坐標(biāo))
 protected int y;//上下移動(dòng)(y坐標(biāo))

 /**
 * 飛行物移動(dòng)(抽象)
 * 每個(gè)飛行物都是會(huì)移動(dòng)的,但是移動(dòng)方式不同
 * 所以這里就將共有的行為抽到了超類中
 * 但是設(shè)置成了抽象方法,實(shí)現(xiàn)了多態(tài)的效果
 */
 public abstract void step();

 /**
 * 獲取圖片(抽象)
 * 所有對(duì)象都是圖片但圖片不相同所以抽象化了
 */
 public abstract BufferedImage getImage();

 /**
 * 判斷對(duì)象是否是活著的
 */
 public boolean isLive(){
 return state == LIVE;
 }
 /**
 * 判斷對(duì)象是否是死了的
 */
 public boolean isDead(){
 return state == DEAD;
 }
 /**
 * 判斷對(duì)象是否刪除了
 */
 public boolean isRemove(){
 return state == REMOVE;
 }

 /**
 * 判斷對(duì)象(大敵機(jī),小敵機(jī),小蜜蜂)是否越界
 * 當(dāng)敵人越界我們就需要?jiǎng)h除它否則程序越執(zhí)行越卡,會(huì)出現(xiàn)內(nèi)存泄露的問(wèn)題,此方法就是為后續(xù)刪除越界對(duì)象做鋪墊的
 * @return
 */
 public boolean isOutOfBounds(){
 return y >= World.HEIGHT;
 }
 /**
 * 給小/大敵機(jī),小蜜蜂提供的
 * 因?yàn)槿N飛行物的寬,高不同所以不能寫(xiě)死。
 * 若三種飛行物的寬,高相同,那么就可以將寬,高寫(xiě)死
 */
 public Flyer(int width,int height){
 Random rand = new Random();
 this.width = width;
 this.height = height;
 x = rand.nextInt(World.WIDTH-width);//x:0到負(fù)的width長(zhǎng)度的之間的隨機(jī)數(shù)
 y = -height;//y:負(fù)的height高度
 }

 /**
 * 給天空,子彈,英雄機(jī)提供的
 * 因?yàn)橛⑿蹤C(jī),子彈,天空的寬,高,x,y都是不同的,所以數(shù)據(jù)不能寫(xiě)死,需要傳參
 */
 public Flyer(int width,int height,int x,int y){
 this.width = width;
 this.height = height;
 this.x = x;
 this.y = y;
 }

 /**
 *檢測(cè)碰撞
 * this:敵人(小敵機(jī)/小蜜蜂/大敵機(jī))
 * other:子彈/英雄機(jī)
 *@return
 */
 public boolean isHit(Flyer other){
 int x1 = this.x - other.width;//x1:敵人的x-英雄機(jī)/子彈的寬
 int x2 = this.x + this.width;//x2:敵人的x加上敵人的寬
 int y1 = this.y - other.height;//y1:敵人的y-英雄機(jī)/子彈的高
 int y2 = this.y + this.height;//y2:敵人的y加上敵人的高
 int x = other.x;//x:英雄機(jī)/子彈的x
 int y = other.y;//y:英雄機(jī)/子彈的y
 /*
 x在x1與x2之間 并且 y在y1與y2之間,即為撞上了
 */
 return x>x1 && x<=x2 && y>=y1 && y<=y2;
 }

 /**
 * 飛行物死亡
 */
 public void goDead(){
 state = DEAD;//將當(dāng)前狀態(tài)修改為死了的
 }
}

圖片工具類Images:

此類用來(lái)獲取每個(gè)對(duì)象對(duì)應(yīng)的圖片

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
/**
 * 圖片工具類
 */
public class Images {
// 公開(kāi)的 靜態(tài)的 圖片數(shù)據(jù)類型 變量名
 /**
 * 對(duì)象圖片
 */
 public static BufferedImage sky;//天空
 public static BufferedImage bullet;//子彈
 public static BufferedImage[] heros;//英雄機(jī)
 public static BufferedImage[] airs;//小敵機(jī)
 public static BufferedImage[] bairs;//大敵機(jī)
 public static BufferedImage[] bees;//小蜜蜂

 /**
 * 狀態(tài)圖片
 */
 public static BufferedImage start;//啟動(dòng)狀態(tài)圖
 public static BufferedImage pause;//暫停狀態(tài)圖
 public static BufferedImage gameover;//游戲結(jié)束狀態(tài)圖

 static {//初始化靜態(tài)圖片
 sky = readImage("background01.png");//天空
 bullet = readImage("bullet.png");//子彈
 heros = new BufferedImage[2];//英雄機(jī)圖片數(shù)組
 heros[0] = readImage("hero0.png");//英雄機(jī)圖片1
 heros[1] = readImage("hero1.png");//英雄機(jī)圖片2
 airs = new BufferedImage[5];//小敵機(jī)圖片數(shù)組
 bairs = new BufferedImage[5];//大敵機(jī)圖片數(shù)組
 bees = new BufferedImage[5];//小蜜蜂圖片數(shù)組
 airs[0] = readImage("airplane.png");//小敵機(jī)圖片讀取
 bairs[0] = readImage("bigairplane.png");//大敵機(jī)圖片讀取
 bees[0] = readImage("bee01.png");//小蜜蜂圖片讀取

 /**爆炸圖迭代讀取*/
 for (int i=1;i<5;i++){//遍歷/迭代賦值
 airs[i] = readImage("bom"+i+".png");//小敵機(jī)圖片數(shù)組其余元素賦值爆炸圖
 bairs[i] = readImage("bom"+i+".png");//大敵機(jī)圖片數(shù)組其余元素賦值爆炸圖
 bees[i] = readImage("bom"+i+".png");//小蜜蜂圖片數(shù)組其余元素賦值爆炸圖
 }
 start = readImage("start.png");//啟動(dòng)狀態(tài)圖
 pause = readImage("pause.png");//暫停狀態(tài)圖
 gameover = readImage("gameover.png");//游戲結(jié)束狀態(tài)圖
 }

 /**
 * 讀取圖片
 * 此處的fileName:圖片文件名
 *
 * try.....catch:異常的一種處理方法
 */
 public static BufferedImage readImage(String fileName){
 try{
 BufferedImage img = ImageIO.read(Flyer.class.getResource(fileName)); //讀取與Flyer在同一個(gè)包中的圖片
 return img;
 }catch(Exception e){
 e.printStackTrace();
 throw new RuntimeException();
 }
 }
}

世界窗口類/測(cè)試類 World:

此類用來(lái)集合所有類進(jìn)行排序及具體的操作,和程序的最終運(yùn)行

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.nio.Buffer;
//定時(shí)器
import java.util.Timer;
//定時(shí)器任務(wù)
import java.util.TimerTask;
//打開(kāi)隨機(jī)類
import java.util.Random;
//擴(kuò)容類
import java.util.Arrays;
/**
 * 世界測(cè)試類(整個(gè)游戲窗口)
 */
public class World extends JPanel{
 public static final int WIDTH = 400;//窗口寬
 public static final int HEIGHT = 700;//窗口高

 public static final int START = 0;//啟動(dòng)狀態(tài)
 public static final int RUNNING = 1;//運(yùn)行狀態(tài)
 public static final int PAUSE = 2;//暫停狀態(tài)
 public static final int GAME_OVER = 3;//游戲結(jié)束狀態(tài)
 private int state = START;//當(dāng)前狀態(tài)默認(rèn)是啟動(dòng)狀態(tài)

 /**
 * 聲明每個(gè)類具體的對(duì)象
 * 如下為:窗口中所看到的對(duì)象
 */
 private Sky s = new Sky();//天空對(duì)象
 private Hero h = new Hero();//英雄機(jī)對(duì)象
 private Flyer[] enemies ={};//敵人對(duì)象,分別是大敵機(jī),小敵機(jī),小蜜蜂所以寫(xiě)成了數(shù)組
 private Bullet[] bt ={};//子彈也是有很多的所以寫(xiě)成了數(shù)組

 /**
 * 生成敵人對(duì)象(小敵機(jī),大敵機(jī),小蜜蜂)
 */
 public Flyer nextOne(){
 Random rand = new Random();
 int type = rand.nextInt(20);//0-19之間的隨機(jī)數(shù)
 if (type < 5){//當(dāng)隨機(jī)數(shù)小于5
 return new Bee();//返回小蜜蜂
 }else if (type < 13){//當(dāng)隨機(jī)數(shù)小于13
 return new Airplane();//返回小敵機(jī)
 }else{//大于十三則
 return new BigAirplane();//返回大敵機(jī)
 }
 }

 private int enterIndex = 0;
 /**
 * 敵人(大敵機(jī),小敵機(jī),小蜜蜂)入場(chǎng)
 */
 public void enterAction() {//每10毫秒走一次
 enterIndex++;
 if (enterIndex%40 == 0 ){//四百毫秒走一次
 Flyer fl = nextOne();//獲取敵人對(duì)象
 enemies = Arrays.copyOf(enemies,enemies.length+1);//擴(kuò)容(每產(chǎn)生一個(gè)敵人數(shù)組就擴(kuò)容1)
 enemies[enemies.length-1] = fl;//將生成的敵人fl放置enemies數(shù)組的末尾
 }
 }

 int shootIndex = 0;
 /**
 * 子彈入場(chǎng)
 */
 public void shootAction(){//10毫秒走一次
 shootIndex++;
 if (shootIndex%30 == 0){//每300毫秒走一次
 Bullet[] bs = h.shoot();//獲取子彈數(shù)組對(duì)象
 bt = Arrays.copyOf(bt,bt.length+bs.length);//擴(kuò)容子彈數(shù)組(每入場(chǎng)一個(gè)子彈就加一個(gè)元素)
 System.arraycopy(bs,0,bt,bt.length-bs.length,bs.length);//數(shù)組的追加
 }
 }

 /**
 * 讓除去英雄機(jī)外的所有對(duì)象(小敵機(jī),大敵機(jī),小蜜蜂,子彈,天空)移動(dòng)
 */
 public void setpAction() {//每10毫秒走一次
 s.step();//天空移動(dòng)
 for (int i=0;i<enemies.length;i++) {//遍歷所有敵人
 enemies[i].step();//敵人移動(dòng)
 }
 for (int i=0;i<bt.length;i++){//遍歷所有子彈
 bt[i].step();//子彈移動(dòng)
 }
 }

 /**
 * 重寫(xiě)outOfBoundsAction(方法)
 */
 public void outOfBoundsAction() {//每10毫秒走一次
 for (int i=0;i<enemies.length;i++){//遍歷所有敵人
 if (enemies[i].isOutOfBounds() || enemies[i].isRemove()){
 enemies[i] = enemies[enemies.length-1];//最后一個(gè)敵人和越界敵人替換
 enemies = Arrays.copyOf(enemies,enemies.length-1);//縮容
 }
 }
 for (int i=0;i<bt.length;i++){//迭代所有子彈
 if (bt[i].isOutOfBounds() || bt[i].isRemove()){
 bt[i] = bt[bt.length-1];//用最后一個(gè)子彈替換出界的子彈
 bt = Arrays.copyOf(bt,bt.length-1);//縮容
 }
 }
 }
 private int score = 0;//玩家的得分
 /**
 * 子彈與敵人的碰撞
 */
 public void bulletBangAction() {//每10毫秒走一次
 for (int i=0;i<bt.length;i++){//遍歷所有子彈
 Bullet b = bt[i];//獲取每一個(gè)子彈
 for (int j=0;j<enemies.length;j++){//迭代每一個(gè)敵人
 Flyer f = enemies[j];//獲取每一個(gè)敵人
 if (b.isLive() && f.isLive() && f.isHit(b)){//若子彈活著的,敵人活著的,并且兩個(gè)對(duì)象相撞
 b.goDead();//子彈當(dāng)前狀態(tài)修改為死亡
 f.goDead();//敵人當(dāng)前狀態(tài)修改為死亡
 if (f instanceof EnemyScore) {//判斷死亡的敵人類型能否強(qiáng)轉(zhuǎn)為得分接口類型
 EnemyScore es = (EnemyScore) f;//將死亡敵人向下造型
 score += es.getScore();//調(diào)用具體的敵人對(duì)象的得分接口的getScore()加分方法
 }
 if (f instanceof EnemyAward){//判斷死亡的敵人類型能否強(qiáng)轉(zhuǎn)為獎(jiǎng)勵(lì)值接口類型
 EnemyAward ea = (EnemyAward) f;//將死亡敵人強(qiáng)轉(zhuǎn)為獎(jiǎng)勵(lì)值接口類型
 int type = ea.getAwardType();//將具體的獎(jiǎng)勵(lì)值賦值給type
 switch (type){
 case EnemyAward.FIRE://火力值
 h.addFier();//返回增加火力值
 break;
 case EnemyAward.LIFE://生命值
 h.addLife();//返回增加生命值
 break;
 }
 }
 }
 }
 }
 }

 /**
 * 英雄機(jī)與敵人的碰撞
 */
 private void heroBangAction() {//每10毫秒走一次
 for (int i=0;i<enemies.length;i++){//迭代所有敵人
 Flyer f = enemies[i];//獲取每個(gè)敵人
 if (f.isLive() && h.isLive() && f.isHit(h)){//判斷碰撞
 f.goDead();//敵人死亡
 h.subtractLife();//英雄機(jī)減生命值
 h.clearFier();//英雄機(jī)清空火力值
 }
 }
 }

 /**
 * 檢測(cè)游戲結(jié)束
 */
 private void checkGameOverAction() {//每10毫秒走一次
 if (h.getLife() <= 0) {//若英雄機(jī)生命值為0或小于0
 state = GAME_OVER;//將狀態(tài)修改為GAME_OVER游戲結(jié)束狀態(tài)
 }
 }
 /**
 * 啟動(dòng)程序的執(zhí)行
 */
 public void action() {//測(cè)試代碼
 MouseAdapter m = new MouseAdapter() {
 /**
 * 重寫(xiě)mouseMoved()鼠標(biāo)移動(dòng)事件
 * @param e
 */
 @Override
 public void mouseMoved(MouseEvent e) {
 if (state == RUNNING){//僅在運(yùn)行狀態(tài)下執(zhí)行
 int x = e.getX();//獲取鼠標(biāo)的x坐標(biāo)
 int y = e.getY();//獲取鼠標(biāo)的y坐標(biāo)
 h.moveTo(x,y);//接收鼠標(biāo)具體坐標(biāo)
 }
 }

 /**
 * 重寫(xiě)mouseClicked() 鼠標(biāo)點(diǎn)擊事件
 * @param e
 */
 public void mouseClicked(MouseEvent e){
 switch (state){//根據(jù)當(dāng)前狀態(tài)做不同的處理
 case START://啟動(dòng)狀態(tài)時(shí)
 state = RUNNING;//鼠標(biāo)點(diǎn)擊后改成運(yùn)行狀態(tài)
 break;
 case GAME_OVER://游戲結(jié)束狀態(tài)時(shí)
 /**
 * 清理戰(zhàn)場(chǎng)(將所有數(shù)據(jù)初始化)
 */
 score = 0;//總分歸零
 s = new Sky();//天空初始化所有屬性
 h = new Hero();//英雄機(jī)初始化所有屬性
 enemies = new Flyer[0];//敵人初始化所有屬性
 bt = new Bullet[0];//子彈初始化所有屬性
 
 state = START;//鼠標(biāo)點(diǎn)擊后修改為啟動(dòng)狀態(tài)
 break;
 }
 }

 /**
 * 鼠標(biāo)移出窗口事件
 * @param e
 */
 public void mouseExited(MouseEvent e){
 if (state == RUNNING){//若狀態(tài)為運(yùn)行
 state = PAUSE;//則將當(dāng)前狀態(tài)修改為暫停
 }
 }

 /**
 * 鼠標(biāo)的進(jìn)入窗口事件
 * @param e
 */
 public void mouseEntered(MouseEvent e){
 if (state == PAUSE){//若當(dāng)前狀態(tài)為暫停
 state = RUNNING;//則將當(dāng)前狀態(tài)修改為運(yùn)行
 }
 }
 };
 this.addMouseListener(m);
 this.addMouseMotionListener(m);
 Timer timer = new Timer();//定時(shí)器對(duì)象
 int interval = 10;//定時(shí)的間隔(此間隔是以毫秒為單位)
 timer.schedule(new TimerTask() {
 @Override
 public void run() {//定時(shí)干的事(每10毫秒自動(dòng)執(zhí)行此方法當(dāng)中的所有方法)
 if (state == RUNNING){//只在運(yùn)行狀態(tài)下執(zhí)行
 enterAction();//敵人(大敵機(jī),小敵機(jī),小蜜蜂)入場(chǎng)
 shootAction();//子彈入場(chǎng)
 setpAction();//飛行物移動(dòng)
 outOfBoundsAction();//刪除越界的敵人
 bulletBangAction();//子彈與敵人的碰撞
 heroBangAction();//英雄機(jī)與敵人的碰撞
 checkGameOverAction();//檢測(cè)游戲結(jié)束
 }
 repaint();//重新調(diào)用paint()方法(重畫(huà))
 }
 }, interval, interval);//定時(shí)計(jì)劃表
 }

 /**
 * 重寫(xiě)paint方法,在窗口中畫(huà)圖片
 * @param g:畫(huà)筆
 */
 public void paint(Graphics g){//每10毫秒走一次
 g.drawImage(s.getImage(), s.x, s.y, null);//畫(huà)天空
 g.drawImage(s.getImage(), s.x, s.getY1(), null);//畫(huà)第二張?zhí)炜?
 g.drawImage(h.getImage(),h.x,h.y,null);//畫(huà)英雄機(jī)
 for (int i=0;i<enemies.length;i++){//遍歷所有敵人
 Flyer f = enemies[i];//獲取每一個(gè)敵人
 g.drawImage(f.getImage(),f.x,f.y,null);//畫(huà)敵人
 }
 for (int i = 0; i<bt.length; i++){//遍歷所有子彈
 Bullet b = bt[i];//獲取所有子彈
 g.drawImage(b.getImage(),b.x,b.y,null);//畫(huà)子彈
 }
 g.drawString("SCORE:"+score,10,25);//在窗口右上角畫(huà)分?jǐn)?shù)
 g.drawString("HP:"+h.getLife(),10,45);//在窗口右上角畫(huà)出英雄機(jī)的生命值
 switch (state){//畫(huà)狀態(tài)圖
 case START:
 g.drawImage(Images.start,0,0,null);//啟動(dòng)狀態(tài)圖
 break;
 case PAUSE:
 g.drawImage(Images.pause,0,0,null);//暫停圖
 break;
 case GAME_OVER:
 g.drawImage(Images.gameover,0,0,null);//游戲結(jié)束圖
 break;
 }
 }

 /**
 * 主執(zhí)行方法
 * @param args
 */
 public static void main(String[] args) {
 JFrame frame = new JFrame();
 World world = new World();
 frame.add(world);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setSize(WIDTH,HEIGHT);
 frame.setLocationRelativeTo(null);
 frame.setVisible(true);//1)設(shè)置窗口可見(jiàn) 2)盡快調(diào)用paint()方法

 world.action();//啟動(dòng)程序的執(zhí)行
 }
}
/**
 * 1.問(wèn):為什么要將引用設(shè)計(jì)再main方法的外面?
 * 答:因?yàn)槿魧⒁迷O(shè)計(jì)在main中,則引用只能在main中使用,其他方法都不能訪問(wèn),
 * 為了能在其他方法中也能訪問(wèn)這些引用,所以將引用設(shè)計(jì)在main外
 *
 * 2.問(wèn):為什么要單獨(dú)創(chuàng)建action方法來(lái)測(cè)試?
 * 答:因?yàn)閙ain方法時(shí)static的,在main方法中是無(wú)法訪問(wèn)引用的,
 * 所以需要單獨(dú)創(chuàng)建非static的方法來(lái)測(cè)試
 *
 * 3.問(wèn):為什么在main中要先創(chuàng)建world對(duì)象,然后再調(diào)用action()方法?
 * 答:因?yàn)閙ain方法是static的,再main中是無(wú)法調(diào)用action()方法的
 * 所以要先創(chuàng)建world對(duì)象,然后再調(diào)用action()方法
 */

小敵機(jī)類Airplane:

此類存儲(chǔ)小敵機(jī)特有的屬性及行為:
移動(dòng)速度,分值,及圖片的切換
繼承超類,且實(shí)現(xiàn)得分接口

package cn.tedu.shoot;

import java.awt.image.BufferedImage;

/**
 * 小敵機(jī)
 */
public class Airplane extends Flyer implements EnemyScore{
 // 移動(dòng)速度
 private int speed;

 public Airplane(){
 super(66,89);
 speed = 2;//小敵機(jī)的下落速度
 }
 /**重寫(xiě)step方法(移動(dòng))*/
 public void step(){
 y += speed;//y+表示向下
 }
 int index = 1;
 /**
 * 重寫(xiě)getImage()獲取對(duì)象圖片
 * @return
 */
 public BufferedImage getImage() {
 if (isLive()){//若活著 則返回airs[0]圖片
 return Images.airs[0];
 }else if (isDead()){//若死了 則返回airs[1~4]圖片
 BufferedImage img = Images.airs[index++];//獲取爆破圖
 if (index == Images.airs.length){//若index到了5 則表示到了最后一張
 state = REMOVE;//將當(dāng)前狀態(tài)修改為REMOVE刪除的
 }
 return img;//返回爆炸圖
 /*
 index = 1
 10M isLive返回true 則 return返回airs[0]圖片
 20M isLive返回false 則 執(zhí)行isDead返回true img = airs[1] index = 2 返回airs[1]圖片
 30M isLive返回false 則 執(zhí)行isDead返回true img = airs[2] index = 3 返回airs[2]圖片
 40M isLive返回false 則 執(zhí)行isDead返回true img = airs[3] index = 4 返回airs[3]圖片
 50M isLive返回false 則 執(zhí)行isDead返回true img = airs[4] index = 5 state修改為REMOVE 返回airs[4]圖片
 60M isLive返回false 則 執(zhí)行isDead返回false return返回null空值(不返回圖片)
 */
 }
 return null;
 }

 /**
 * 重寫(xiě)getScore()方法
 * @return:分值
 */
 public int getScore(){
 return 1;
 }
}

大敵機(jī)類BigAirplane:

大敵機(jī)與小敵機(jī)幾乎無(wú)差別
同樣要繼承超類,且實(shí)現(xiàn)得分接口

package cn.tedu.shoot;

import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 大敵機(jī)
 */
public class BigAirplane extends Flyer implements EnemyScore{
// 移動(dòng)速度
 private int speed;

 public BigAirplane(){//初始化默認(rèn)屬性
 super(203,211);//圖片寬,高
 speed = 2;//移動(dòng)速度
 }

 /**重寫(xiě)step方法(移動(dòng))*/
 public void step(){
 y += speed;//y+表示直線向下移動(dòng)
 }

 int index = 1;
 @Override
 public BufferedImage getImage() {
 if (isLive()){//若活著 則返回airs[0]圖片
 return Images.bairs[0];
 }else if (isDead()){//若死了 則返回airs[1~4]圖片
 BufferedImage img = Images.bairs[index++];//獲取爆破圖
 if (index == Images.bairs.length){//若index到了5 則表示到了最后一張
 state = REMOVE;//將當(dāng)前狀態(tài)修改為REMOVE刪除的
 }
 return img;
 }
 return null;
 }

 /**
 * 重寫(xiě)getScore()方法
 * @return:分值
 */
 public int getScore(){
 return 3;
 }
}

小蜜蜂類Bee:

此類雖也可以算作敵人類,但是與小/大敵機(jī)有所不同,它是實(shí)現(xiàn)獎(jiǎng)勵(lì)值接口

package cn.tedu.shoot;

import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 小蜜蜂
 */
public class Bee extends Flyer implements EnemyAward{
 // x坐標(biāo)移動(dòng)速度,y坐標(biāo)移動(dòng)速度,
 private int xSpeed;//x坐標(biāo)移動(dòng)速度
 private int ySpeed;//y坐標(biāo)移動(dòng)速度
 private int awardType;//獎(jiǎng)勵(lì)類型

 public Bee(){//初始化屬性
 super(48,50);//圖片寬,高
 Random rand = new Random();
 awardType = rand.nextInt(2);//隨機(jī)獎(jiǎng)勵(lì)值類型0~2之間(不包括2)0表示火力值,1表示生命值
 xSpeed = 1;//平行移動(dòng)
 ySpeed = 2;//垂直移動(dòng)
 }
 /**重寫(xiě)step方法(移動(dòng))*/
 public void step() {
 y += ySpeed;//y+:向下移動(dòng)
 x += xSpeed;//x+:隨機(jī)向左或是向右移動(dòng)
 if (x <= 0 || x >= World.WIDTH - width) {
 xSpeed *= -1;//到達(dá)邊界后反方向移動(dòng)(正負(fù)為負(fù),負(fù)負(fù)為正)
 }
 }

 int index = 1;
 public BufferedImage getImage() {
 if (isLive()){//若活著 則返回airs[0]圖片
 return Images.bees[0];//返回小蜜蜂圖
 }else if (isDead()){//若死了 則返回airs[1~4]圖片
 BufferedImage img = Images.bees[index++];//獲取爆破圖
 if (index == Images.bees.length){//若index到了5 則表示到了最后一張
 state = REMOVE;//將當(dāng)前狀態(tài)修改為REMOVE刪除的
 }
 return img;//返回爆炸圖
 }
 return null;
 }

 /**
 * 重寫(xiě)getAwardType()方法
 * @return
 */
 public int getAwardType(){
 return awardType;//返回獎(jiǎng)勵(lì)類型
 }
}

天空類Sky:

這里有一點(diǎn)需要強(qiáng)調(diào),就是為了實(shí)現(xiàn)天空?qǐng)D片向下移動(dòng)后會(huì)出現(xiàn)移動(dòng)過(guò)的位置出現(xiàn)圖片丟失的情況,就使用了兩張圖上下拼接起來(lái),當(dāng)?shù)谀硰執(zhí)炜請(qǐng)D完全移出窗口的時(shí)候會(huì)讓它重新出現(xiàn)在窗口上方繼續(xù)向下移動(dòng)

package cn.tedu.shoot;

import java.awt.image.BufferedImage;

/**
 * 天空
 */
public class Sky extends Flyer{
 // 移動(dòng)速度,y1
 private int y1;//第二張圖片的y坐標(biāo)
 private int speed;//移動(dòng)速度

 public Sky(){//設(shè)置初始值(默認(rèn)值)
 //此處的寬高用常量是因?yàn)樘炜盏膶捀吆痛翱谑且恢碌?,x軸和y軸為若不為0就和窗口不匹配了
 super(World.WIDTH,World.HEIGHT,0,0);//初始化圖片坐標(biāo)及寬,高
 speed = 1;//初始化移動(dòng)速度
 y1 = -World.HEIGHT;//第二張圖片設(shè)置在第一張圖片上方
 }
 /**重寫(xiě)step方法(移動(dòng))*/
 public void step(){
 y += speed;//第一張圖向下移動(dòng)
 y1 += speed;//第二張圖向下移動(dòng)
 if (y >= World.HEIGHT){//若y>=窗口的高
 y = -World.HEIGHT;//將移動(dòng)出去的第一張?zhí)炜张驳酱翱谏戏?
 }
 if (y1 >= World.HEIGHT){//若第二張?zhí)炜张渤龃翱?
 y1 = -World.HEIGHT;//將第二張?zhí)炜张驳酱翱谏戏?
 }
 }
 /**重寫(xiě)getImage()獲取對(duì)象圖片*/
 @Override
 public BufferedImage getImage() {//10毫秒走一次
 return Images.sky;//返回天空?qǐng)D片即可
 }

 /**
 * 獲取y1坐標(biāo)
 */
 public int getY1(){
 return y1;//返回y1
 }
}

英雄機(jī)類Hero:

package cn.tedu.shoot;

import java.awt.image.BufferedImage;
/**
 * 英雄機(jī)
 */
public class Hero extends Flyer {
 // 命數(shù),火力值
 private int life;//命數(shù)
 private int fire;//火力

 /**
 * 初始化英雄機(jī)坐標(biāo)機(jī)具體數(shù)據(jù)
 */
 public Hero() {
 super(97,139,140,400);//寬,高,及初始坐標(biāo)
 fire = 0;//初始火力值 0:?jiǎn)伪痘鹆?
 life = 3;//初始生命值
 }
 /**重寫(xiě)step方法(移動(dòng))*/
 public void step(){//每10毫秒走一次
 //因?yàn)橛⑿蹤C(jī)是跟隨鼠標(biāo)移動(dòng)的,而鼠標(biāo)是在窗口上的所以這里就沒(méi)有寫(xiě)具體的方法,而是在窗口類中去用鼠標(biāo)的具體坐標(biāo)計(jì)算出英雄機(jī)的移動(dòng)位置
 }

 int index = 0;//下標(biāo)
 /**重寫(xiě)getImage()獲取對(duì)象圖片*/
 @Override
 public BufferedImage getImage() {//每10毫秒走一次
 return Images.heros[index++ % Images.heros.length];//heros[0]和heros[1]來(lái)回切換
 /**
 *過(guò)程
 *index = 0
 *10M 返回heros[0] index = 1
 *20M 返回heros[1] index = 2
 *30M 返回heros[0] index = 3
 *40M 返回heros[1] index = 4
 *50M 返回heros[0] index = 5
 *60M 返回heros[1] index = 6
 *...........
 */
 }
 /**
 * 英雄機(jī)發(fā)射子彈(生成子彈對(duì)象)
 */
 public Bullet[] shoot(){
 int xStep = this.width/4;//子彈x坐標(biāo)
 int yStep = 5;//子彈y坐標(biāo)
 System.out.println(this.x+"\t"+this.y);
 if (fire>0){//雙倍火力
 Bullet[] bs = new Bullet[3];//2發(fā)子彈
 bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);//子彈坐標(biāo)1
 bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);//子彈坐標(biāo)2
 bs[2] = new Bullet(this.x+2*xStep,this.y-yStep);
 fire -= 2;//發(fā)射一次雙倍活力,則火力值-2
 return bs;
 } else {//單倍火力
 Bullet[] bs = new Bullet[1];//1發(fā)子彈
 bs[0] = new Bullet(this.x+2*xStep,this.y-yStep);//x:英雄機(jī)的x+2/4英雄機(jī)的寬,y:英雄機(jī)的y-
 return bs;
 }
 }

 /**
 * 英雄機(jī)移動(dòng)
 */
 public void moveTo(int x,int y){//形參列表:鼠標(biāo)的x坐標(biāo),y坐標(biāo)
 this.x = x - this.width/2;//英雄機(jī)的x = 鼠標(biāo)的x減1/2英雄機(jī)的寬
 this.y = y - this.height/2;//英雄機(jī)的y = 鼠標(biāo)的y減1/2英雄機(jī)的高
 }
 /**
 * 英雄機(jī)增生命值
 */
 public void addLife(){
 life++;//生命值+1
 }

 /**
 * 獲取英雄機(jī)生命值
 * @return
 */
 public int getLife(){
 return life;//返回生命值
 }

 /**
 * 英雄機(jī)減少生命值
 */
 public void subtractLife(){
 life--;//生命值減1
 }
 /**
 * 英雄機(jī)增火力值
 */
 public void addFier(){
 fire += 40;//火力值+40
 }

 /**
 * 清空火力值
 */
 public void clearFier(){
 fire = 0;//火力值歸零
 }
}

子彈類Bullet:

package cn.tedu.shoot;

import java.awt.image.BufferedImage;

/**
 * 子彈
 */
public class Bullet extends Flyer {
 // 移動(dòng)速度
 private int speed;
 public Bullet(int x,int y) {//子彈有多個(gè),每個(gè)子彈的初始坐標(biāo)都不同,所以要寫(xiě)活
 super(8,20,x,y);
 speed = 3;//初始移動(dòng)速度
 }
 /**重寫(xiě)step方法(移動(dòng))*/
 public void step(){
 y -= speed;//y-:表示直線向上移動(dòng)
 }

 /**
 * 重寫(xiě)getImage()獲取對(duì)象圖片
 * @return
 */
 @Override
 public BufferedImage getImage() {//10毫秒走一次
 if (isLive()){//若活著則返回bullet圖片
 return Images.bullet;
 }else if (isDead()){//若死了則將state修改為REMOVE
 state = REMOVE;
 }
 return null;//死了的和刪除的都返回null空值
 /**
 * 若活著 則返回bullet圖片
 * 若死了 則修改REMOVE 再返回空值
 * 若刪除 則返回空值
 */
 }

 /**
 * 判斷子彈是否越界
 * @return
 */
 public boolean isOutOfBounds(){
 return y <= -height;若子彈的y軸坐標(biāo)小于自己的高則說(shuō)明移動(dòng)到了窗口外部
 }
}

獎(jiǎng)勵(lì)值接口 EnemyAward:

package cn.tedu.shoot;

/**
 * 獎(jiǎng)勵(lì)值接口
 */
public interface EnemyAward {
 public int FIRE = 0;//火力
 public int LIFE = 1;//生命值
 /**
 * 獲取獎(jiǎng)勵(lì)值類型
 * @return
 */
 int getAwardType();
}

得分接口 EnemyScore:

package cn.tedu.shoot;
/*得分接口*/
public interface EnemyScore {
 /*得分*/
 public int getScore();
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論