主頁 >  其他 > 飛機大戰

飛機大戰

2020-12-18 12:55:33 其他

飛機大戰

1.主要功能

英雄機受玩家滑鼠控制進行移動并發出子彈對敵人進行攻擊,打中敵機會加分,打中小心心會隨機獲得獎勵,獎勵型別分為三種:英雄機生命值加一,英雄機雙倍火力,英雄機三倍火力(要達到三倍火力首先需要達到雙倍火力才行),當英雄機撞到敵機時,火力加成會消失,當分數達到50的時候會出現boss,boss會進行移動并且發射子彈,擊敗boss會判定游戲勝利,當英雄機撞到敵機,小心心或者被boss發射的子彈擊中時,生命值會減一,當生命值為零的時候會判定游戲失敗,英雄機如果撞到了boss那就直接白給,游戲期間用滑鼠進行控制,點擊滑鼠游戲開始,將滑鼠移動至游戲界面外可以暫停游戲,

2.效果展示圖

3.分析

1. 素材準備

在開發之前需要先去下載一些素材圖片,包括背景圖,英雄機圖,敵機圖,子彈圖,boss圖,開始,暫停,勝利以及結束圖片等,

2.設計構思

  • 首先添加開始,勝利,結束等游戲畫面
  • 設計獎勵介面以及得分介面添加獎勵與得分的方法
  • 設計飛行物父類,在類中添加相關的方法
  • 設計敵機類以及小心心類,分別執行得分介面與獎勵介面,并且繼承飛行物父類
  • 設計英雄機類,boss類,英雄機子彈類以及boss子彈類分別繼承飛行物父類
  • 再設計實作類,對他們進行初始化等操作
  • 滑鼠監聽控制英雄機的移動軌跡

3.運用到的知識

  • 陣列
  • swing組件:圖形界面工具
  • 滑鼠監聽
  • 面向物件:封裝繼承多型
  • ImageIO流

4.代碼展示

測驗類代碼出現了一點小BUG,已經標出,按理來說這樣寫當判定游戲結束或者勝利時會先清理現場,然后再修改為啟動狀態,但是我這個游戲結束后還是戰后現場,界面上boss機發射出來的子彈并不能清除,這就很迷惑,

獎勵介面

package shoot;
//小心心:獎勵
public interface Award {
	int TRIPLE_FIRE = 2;//三倍火力
	int DOUBLE_FIRE = 1;//雙倍火力
	int LIFE =0 ;//1條命
	public int getType();//獲取獎勵型別

}

得分介面

package shoot;
//敵機:得分
public interface Enemy {

	public int getScope();
	
}

飛行物類

package shoot;

import java.awt.image.BufferedImage;

//飛行物類 父類
public abstract class FlyingObject {
	
	protected int width;//寬
	protected int height;//高
	protected int x;//x坐標
	protected int y;//y坐標
	protected BufferedImage image;//圖片
	//走步
	public abstract void step();
	//敵人(小飛機,小心心)被子彈打
	public boolean shootBy(Bullet b){
		int x = b.x;//子彈的x和y
		int y = b.y;
		//子彈x在心的x和心的x加寬之間
		//并且
		//子彈y在心的y和心的y加高之間
		return x>this.x && x<this.x+width
			   &&
			   y>this.y && y<this.y+height;
	}	
	//檢測是否出界
	public abstract boolean outofBounds();	
}

敵機類

package shoot;

import java.util.Random;

//小飛機
public class Airplane extends FlyingObject
       implements Enemy {
	private int speed = 2;//敵機移動的步數	
	//得分
	public int getScope() {
		return 5;
	}	
	public Airplane(){
		image = ShootGame.airplane;
		width = image.getWidth();
		height = image.getHeight();
		y = -height;
		Random r = new Random();
		x = r.nextInt(ShootGame.WIDTH - width);
	}
	//敵機走步
	public void step() {
		y += speed;
	}
	//重寫出界
	public boolean outofBounds() {
		return y>ShootGame.HEIGHT;
	}

}

獎勵類

package shoot;

import java.util.Random;

//小心心
public class Bee extends FlyingObject 
     implements Award {

	private int xSpeed = 1;//x坐標走步
	private int ySpeed = 2;//y坐標走步
	private int awardType;//獎勵型別
	
	//獲取獎勵
	public int getType() {
		return awardType;
	}
	//初始化實體變數
	public Bee(){
		image = ShootGame.bee;
		width = image.getWidth();//獲取當前圖片的寬
		height = image.getHeight();//獲取當前圖片的高
		y = -height;
		//x = (int)(Math.random()*(ShootGame.WIDTH - width));
		Random rand = new Random();
		x = rand.nextInt(ShootGame.WIDTH - width);
		awardType = rand.nextInt(4);//0,1,2隨機生成	
	}
	
	//小心心走步
	public void step() {
		x += xSpeed;
		y += ySpeed;
		if(x<0){
			xSpeed = 1;//往右
		}
		if(x>ShootGame.WIDTH - width){
			xSpeed = -1;//往左
		}
	}
	//重寫出界
	public boolean outofBounds() {
		return y>ShootGame.HEIGHT;//y大于面板的高
	}
}

英雄機類

package shoot;

import java.awt.image.BufferedImage;

//英雄機
public class Hero extends FlyingObject{
	private BufferedImage[] images = {};//存放英雄機圖片陣列
	private int index;//下標	
	private int doubleFire;//雙倍火力
	protected int tripleFire;//三倍火力
	public  int life;//命
	
	//初始化實體變數
	public Hero(){
		image = ShootGame.hero0;
		width = image.getWidth();
		height = image.getHeight();
		x = 250;
		y = 700;
	    doubleFire = 0;//單倍火力
	    tripleFire = 0;//單倍火力
	    life = 3;//命3條
	    images = new BufferedImage[]{ShootGame.hero0,ShootGame.hero1};
	}
	//英雄機走步
	public void step() {
		int num = index++/10%images.length;
		image = images[num];
	}
	//發射子彈
	public Bullet[] shoot(){
		int xstep = this.width/4;//4半
		int ystep = 20;
		if(doubleFire<=0&&tripleFire>0){
			tripleFire=0;
			life++;
			Bullet[] bullets = new Bullet[1];
			bullets[0] = new Bullet(this.x+2*xstep,this.y-ystep);
			return bullets;
		}else if(tripleFire>0&&doubleFire>0){
			Bullet[] bullets = new Bullet[3];
			bullets[0] = new Bullet(this.x+1*xstep,this.y-ystep);
			bullets[1] = new Bullet(this.x+3*xstep,this.y-ystep);
			bullets[2] = new Bullet(this.x+2*xstep,this.y-ystep);
			return bullets;
		}else if(doubleFire > 0&&tripleFire<=0){//雙倍火力
			Bullet[] bullets = new Bullet[2];
			bullets[0] = new Bullet(this.x+1*xstep,this.y-ystep);
			bullets[1] = new Bullet(this.x+3*xstep,this.y-ystep);
			return bullets;
		}else{//單倍火力
			Bullet[] bullets = new Bullet[1];
			bullets[0] = new Bullet(this.x+2*xstep,this.y-ystep);
			return bullets;
		}
		
	}
	//移動  傳入的是滑鼠的x和y
	public void moveTo(int x,int y){
		this.x = x-this.width/2;
		this.y = y-this.height/2;
	}	
	//添加三倍火力
	public void addTripleFire(){
		tripleFire+=40;
	}
	//添加雙倍火力
	public void addDoubleFire(){
		doubleFire += 40;
	}
	//添加命
	public void addLife(){
		life++;
	}
	//獲取命
	public int getLife(){
		return life;
	}
	//重寫出界
	public boolean outofBounds() {
		return false;//永不出界
	}
	//判斷英雄機與敵人是否發生碰撞
	//other:敵人
	public boolean hit(FlyingObject other){
		int x1 = other.x-this.width/2;
		int x2 = other.x+other.width+this.width/2;
		int y1 = other.y-this.height/2;
		int y2 = other.y+other.height+this.height/2;
		int heroX = this.x+this.width/2;
		int heroY = this.y+this.height/2;
		return heroX>x1 && heroX<x2
			   &&
			   heroY>y1 && heroY<y2;	
	}
	//英雄機撞到boss
	public boolean bosshit(Boss bo){
		int X1=bo.x-this.width/2;
		int X2=bo.x+bo.width+this.width/2;
		int Y1=bo.y-this.height/2;
		int Y2=bo.y+bo.height+this.height/2;
		int HeroX=this.x+this.width/2;
		int HeroY=this.y+this.height/2;
		return HeroX>X1&&HeroX<X2
				&&
				HeroY>Y1&&HeroY<Y2;
		
	}
	//boss子彈打中英雄機
	public boolean heroShootBy(BossBullet a){
		int x = a.x;//子彈的x和y
		int y = a.y;
		//子彈x在英雄機的x和英雄機的x加寬之間
		//并且
		//子彈y在英雄機的y和英雄機的y加高之間
		return x>this.x && x<this.x+width
			   &&
			   y>this.y && y<this.y+height;
	}	
	//減命
	public void subLife(){
		life--;
	}
	public void setDoubleFire(int doubleFire){
		this.doubleFire = doubleFire;
	}
}

英雄機子彈類

package shoot;
//子彈
public class Bullet extends FlyingObject {
	private int speed = 5;//移動走步	
	public Bullet(int x,int y){
		image = ShootGame.bullet;
		width = image.getWidth();
		height = image.getHeight();
		this.x = x;//跟隨英雄機坐標
		this.y = y;
	}
	//子彈走步
	public void step() {
		y -= speed;
	}
	//重寫出界
	public boolean outofBounds() {
		return y<-height;
	}
	
}

boss類

package shoot;

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

public  class Boss extends FlyingObject{
	protected BufferedImage image=ShootGame.boss;//圖片
	protected int Xspeed=2;
	protected int Yspeed=1;
	public int bossLife=1000;
	protected int width=image.getWidth();//寬
	protected int height=image.getHeight();//高
	Random bx=new Random();
	protected int x=bx.nextInt(ShootGame.WIDTH-width);//x坐標
	protected int y=-height;//y坐標
	//private BufferedImage image=ShootGame.boss;//圖片
	
//	public Boss(){
//		image=ShootGame.boss;
//		width=image.getWidth();
//		height=image.getHeight();
//		x=100;
//		y=100;
//		bossLife=100;
//	}
	//boss被子彈打
	public boolean bossShootBy(Bullet b){
		int x = b.x;//子彈的x和y
		int y = b.y;
		//子彈x在boss的x和boss的x加寬之間
		//并且
		//子彈y在boss的y和boss的y加高之間
		return x>this.x && x<this.x+width
			   &&
			   y>this.y && y<this.y+height;
	}	
	//boss打出子彈
	public BossBullet[] bossshoot(){
		int x_step = this.width/4;//4半
		int y_step = this.height-40;
		BossBullet[] Bullets = new BossBullet[2];
		Bullets[0] = new BossBullet(this.x+1*x_step,this.y+y_step);
		Bullets[1] = new BossBullet(this.x+3*x_step,this.y+y_step);
		//Bullets[2] = new BossBullet(this.x+2*x_step,this.y+y_step);
		return Bullets;
	}
	
	@Override
	public void step() {//boss移動
		y+=Yspeed;
		x+=Xspeed;
		if(y<=0){
			Yspeed=1;
		}
		if(y>=height){
			Yspeed=-1;
		}
		if(x<=0){
			Xspeed=2;
		}
		if(x>=ShootGame.WIDTH-width){
			Xspeed=-2;
		}
	}
	@Override
	public boolean outofBounds() {//檢測出界
		// TODO Auto-generated method stub
		return false;
	}
}

boss子彈類

package shoot;

import java.awt.image.BufferedImage;

public class BossBullet extends FlyingObject {
	private int B_Bulspeed=2;//設定Boss子彈速度
	public BossBullet(int x,int y){
		image=ShootGame.bossbullet;
		width=image.getWidth();
		height=image.getHeight();
		this.x=x;//跟隨boss坐標
		this.y=y;
	}
	@Override
	public void step() {
		// TODO Auto-generated method stub
		y+=B_Bulspeed;
	}
	@Override
	public boolean outofBounds() {
		// TODO Auto-generated method stub
		return y>ShootGame.HEIGHT+height;
	}
}

測驗類

package shoot;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.imageio.ImageIO;

//游戲主界面,主類
public class ShootGame extends JPanel{
	public static final int WIDTH = 600;//游戲界面的寬
	public static final int HEIGHT = 900;//游戲界面的高
	public static BufferedImage background;
	public static BufferedImage airplane;
	public static BufferedImage bee;
	public static BufferedImage bullet;
	public static BufferedImage gameover;
	public static BufferedImage hero0;
	public static BufferedImage hero1;
	public static BufferedImage start;
	public static BufferedImage pause;	
	public static BufferedImage boss;
	public static BufferedImage bossbullet;
	public static BufferedImage win;
	public Hero hero = new Hero();//英雄級物件
	public Bullet[] bullets = {};//子彈陣列
	public BossBullet[] Bullets={};//boss子彈陣列
	public FlyingObject[] flyings = {};//敵人	
	public Boss bosses=new Boss();//boss
	//定時器
	private Timer timer;
	//時間間隔(毫秒)
	
	private int intervar = 10;
	public int score = 0;//紀錄分數
	private int state;//狀態
	public static final int START = 0;
	public static final int RUNNING = 1;
	public static final int PAUSE = 2;
	public static final int GAME_OVER = 3;
	public static final int WIN=4;
	//加載靜態資源
	
	static{
		try {
			background = ImageIO.read(ShootGame.class.getResource("background.png"));
			airplane = ImageIO.read(ShootGame.class.getResource("airplane.png"));
			bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
			bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
			gameover = ImageIO.read(ShootGame.class.getResource("gameover.png"));
			hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
			hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
			start = ImageIO.read(ShootGame.class.getResource("start.png"));
			pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
			boss=ImageIO.read(ShootGame.class.getResource("boss.png"));
			bossbullet=ImageIO.read(ShootGame.class.getResource("bossbullet.png"));
			win=ImageIO.read(ShootGame.class.getResource("win.png"));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//重寫繪制方法
	//g就是畫筆
	public void paint(Graphics g){
	    g.drawImage(background,0,0,null);
	    paintHero(g);//英雄
	    paintBullets(g);//子彈
	    paintFlyingObjects(g);//敵人
	    paintScore(g);//分數
	    paintState(g);//狀態
	    paintBoss(g);//boss
	    paintBosslife(g);//boss生命值
	    paintB_Bullet(g);//畫boss子彈
	}
	
	//畫游戲狀態
	public void paintState(Graphics g){
		switch(state){
		case START://啟動圖片
			g.drawImage(start, 0, 0,null);
			break;
		case PAUSE://暫停
			g.drawImage(pause, 0, 0,null);
		    break;
		case GAME_OVER:
			g.drawImage(gameover, 0, 0,null);
			break;
		case WIN://勝利
			//
			g.drawImage(win,0,0,null);
			
		}
	}
	
	//畫分數
	public void paintScore(Graphics g){
		int x = 10;
		int y = 25;
		//設定字體
		g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
		//設定字體顏色
		g.setColor(new Color(0xFF0000));
		g.drawString("SCORE:"+score, x, y);//畫分數
		g.drawString("LIFE:"+hero.getLife(), x, y+20);//畫英雄生命值
	}
	
	//畫boss生命值
	public void paintBosslife(Graphics g){
		int x=420;
		int y=25;
		//設定字體
		g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
		//設定字體顏色
		g.setColor(new Color(0xFF00));//畫字體顏色
		if(bosses.y>=0){
		g.drawString("BOSS LIFE:"+bosses.bossLife,x,y);//畫boss生命值
		}
	}
	
	//畫boss
	public void paintBoss(Graphics g){
		g.drawImage(bosses.image, bosses.x,bosses.y, null);
	}
	
	//畫boss子彈
	public void paintB_Bullet(Graphics g){
		for(int j=0;j<Bullets.length;j++){
			BossBullet bb=Bullets[j];
			g.drawImage(bb.image, bb.x, bb.y, null);
		}
	}
	
	//畫英雄機
	public void paintHero(Graphics g){
		g.drawImage(hero.image,hero.x,hero.y,null);
	}
	
	//畫子彈
	public void paintBullets(Graphics g){
		for(int i=0;i<bullets.length;i++){
			Bullet b = bullets[i];
			g.drawImage(b.image,b.x,b.y,null);
		}
	}
	
	//畫敵人
	public void paintFlyingObjects(Graphics g){
		for(int i=0;i<flyings.length;i++){
			FlyingObject f = flyings[i];
			g.drawImage(f.image,f.x,f.y,null);
		}
	}
	
	//啟動執行操作
	public void action(){
		//滑鼠事件配接器
		MouseAdapter l = new MouseAdapter() {
			//重寫,(滑鼠移動)方法
			public void mouseMoved(MouseEvent e){
				if(state == RUNNING){
					int x = e.getX();//得到滑鼠x
					int y = e.getY();//得到滑鼠y
					hero.moveTo(x, y);
				}
			}







///

/**
*游戲結束或勝利后,boss機已經發射出來的子彈會留在界面上不能消失
*這就很搞心態,按理來說這樣寫游戲結束后螢屏上的飛行物就會消失
*/



			//重寫,滑鼠點擊
			public void mouseClicked(MouseEvent e) {
				switch(state){
				case START:
					state = RUNNING;
					break;
				case WIN:					
				case GAME_OVER://游戲結束歸零
					state = START;
				    hero = new Hero();
				    bosses = new Boss();
					flyings = new FlyingObject[0];
					Bullets = new BossBullet[0];
					bullets = new Bullet[0];
					score = 0;
					//state = START;
					break;
				}
			}

///






			//重寫 滑鼠移入
			public void mouseEntered(MouseEvent e) {
				if(state == PAUSE){
					state = RUNNING;
				}
			}
			//重寫 滑鼠移出
			public void mouseExited(MouseEvent e) {
				if(state != GAME_OVER){
					state = PAUSE;
				}
			}
		};
		//給當前畫板添加滑鼠滑動偵聽
		this.addMouseMotionListener(l);
		//給當前畫板添加滑鼠點擊偵聽
		this.addMouseListener(l);
		//創建定時器物件
	    timer = new Timer();
	    //定時觸發
	    timer.schedule(new TimerTask(){
	    	//重寫run方法 ,定時執行的任務 
			public void run() {
				if(state == RUNNING){
					enterAction();//飛行物入場---new物件
					stepAction();//飛行物走步
					shootAction();//子彈入場
					if(bosses.y>=0){
					B_shootAction();//boss子彈入場
					}
					heroBangAction();//子彈打中英雄機
					bangAction();//子彈打敵人
					outofBoundsAction();//洗掉出界的飛行物
					checkGameOverAction();//檢測游戲是否結束
					checkWinAction();//檢測游戲是否勝利				
				}
				//重構畫板
				repaint();
			}
	    },intervar,intervar);
	}
	
	//檢測游戲是否結束
	public void checkGameOverAction(){
		if(isGameOver()){//判斷游戲是否結束
			state = GAME_OVER;
		}
	}
	
	//判斷是否結束
	public boolean isGameOver(){
		for(int i=0;i<flyings.length;i++){
			int index = -1;//記錄撞上飛行物的索引
			FlyingObject obj = flyings[i];
			if(hero.hit(obj)){//撞上
				hero.subLife();//減命
				hero.setDoubleFire(0);//設定火力
				index = i;//記錄索引
			}
			if(index != -1){//有撞上
				FlyingObject t = flyings[index];
				flyings[index] = flyings[flyings.length-1];
				flyings[flyings.length-1] = t;
				flyings = Arrays.copyOf(flyings,flyings.length-1);
			}
		}
		
		if(hero.bosshit(bosses)){//判斷boss是否與英雄機相撞
			hero.life=0;//英雄機死亡
		}		
		return hero.getLife()<=0;
	}
	
	//檢測游戲勝利
	public void checkWinAction(){
		if(isWIN()){
			state=WIN;
		}
	}
	
	//判斷游戲勝利
	public boolean isWIN(){
		return bosses.bossLife<=0;	
	}
	
	//洗掉出界的飛行物
	public void outofBoundsAction(){
		int index = 0;//下標
		FlyingObject[] flyingLives = new FlyingObject[flyings.length];
		for(int i=0;i<flyings.length;i++){
			FlyingObject f = flyings[i];//得到的每個敵人
			if(!f.outofBounds()){//若不出界
				flyingLives[index++] = f;
			}
		}
		flyings = Arrays.copyOf(flyingLives,index);
		//洗掉越界子彈
		index = 0;
		Bullet[] bulletsLives = new Bullet[bullets.length];
		for(int i=0;i<bullets.length;i++){
			Bullet b = bullets[i];
			if(!b.outofBounds()){
				bulletsLives[index++] = b;
			}
		}
		bullets = Arrays.copyOf(bulletsLives,index);
		//洗掉越界boss子彈
		index=0;
		BossBullet[] BulletsLives=new BossBullet[Bullets.length];
		for(int i=0;i<BulletsLives.length;i++){
			BossBullet a=Bullets[i];
			if(!a.outofBounds()){
				BulletsLives[index++]=a;
			}
		}
	}
	
	//子彈打敵人
	int flag=0;
	public void bangAction(){
		for(int i=0;i<bullets.length;i++){
			Bullet b = bullets[i];//每一個子彈
			flag=i;
			bang(b);//判斷子彈和敵人撞擊
			bossbang(b);//判斷子彈和boss撞擊
		}
	}
	
	//boss子彈列印雄機
	int B_flag=0;
	public void heroBangAction(){
		for(int i=0;i<Bullets.length;i++ ){
			BossBullet a=Bullets[i];//每一顆boss子彈
			B_flag=i;
			if(bosses.y>=0){
			herobang(a);
			}
		}
	}
	
	//判斷boss子彈與英雄機撞擊
	public void herobang(BossBullet a){
		if(hero.heroShootBy(a)){//是否被擊中
			hero.subLife();//英雄機生命值減一
			BossBullet heroone=Bullets[B_flag];//洗掉擊中后的子彈
			Bullets[B_flag]=Bullets[Bullets.length-1];
			Bullets[Bullets.length-1]=heroone;
			Bullets=Arrays.copyOf(Bullets, Bullets.length-1);
		}
	}
	
	//判斷子彈與boss撞擊
	public void bossbang(Bullet b){
		if(bosses.bossLife>0&&score>=50){
		if(bosses.bossShootBy(b)){//boss是否被擊中
			bosses.bossLife-=10;	
			Bullet bossone=bullets[flag];//洗掉擊中后的子彈
			bullets[flag]=bullets[bullets.length-1];
			bullets[bullets.length-1]=bossone;
			bullets=Arrays.copyOf(bullets, bullets.length-1);
		}
		}else{
			bosses.y=-bosses.height;
		}	
	}
	
	//判斷子彈和敵人撞擊
	public void bang(Bullet b){	
		int index = -1;
		//遍歷所有敵人
		for(int j=0;j<flyings.length;j++){
			FlyingObject obj =flyings[j];//每一個敵人
			if(obj.shootBy(b)){//是否撞上
				index = j;//紀錄被擊中的敵人的下標
				break;
			}
		}
		if(index != -1){//有擊中的敵人	
			FlyingObject one = flyings[index];//擊中的敵人
			//洗掉子彈
			Bullet m=bullets[flag];
			bullets[flag]=bullets[bullets.length-1];
			bullets[bullets.length-1]=m;
			bullets=Arrays.copyOf(bullets, bullets.length-1);
			//洗掉敵人
				FlyingObject t = flyings[index];
				flyings[index] = flyings[flyings.length-1];
				flyings[flyings.length-1] = t;
			flyings = Arrays.copyOf(flyings,flyings.length-1);//縮容
			
			//得分或的獎勵
			if(one instanceof Enemy){//若為敵機
				Enemy e = (Enemy)one;
				score += e.getScope();//設定加分
			}else if(one instanceof Award){//若為獎勵
				Award a = (Award)one;
				int type = a.getType();
				switch(type){//判斷獎勵型別	
				case 3:
				case Award.DOUBLE_FIRE:
					hero.addDoubleFire();//雙倍火力
					break;
				case Award.TRIPLE_FIRE:
				    hero.addTripleFire();//三倍火力
					break;
				case Award.LIFE:
					hero.addLife();//增加命
					break;
				}
			}
		}
	}
	
	int shootIndex = 0;
	int B_shootIndex=0;
	
	//boss子彈入場
	public void B_shootAction(){//每十毫秒調一次
		B_shootIndex++;//每調一次自增1
		if(B_shootIndex%50==0){//調30次發射一次
			BossBullet[] B_bs=bosses.bossshoot();//boss發射出來的子彈
			Bullets=Arrays.copyOf(Bullets, Bullets.length+B_bs.length);
			System.arraycopy(B_bs, 0,Bullets, Bullets.length-B_bs.length,B_bs.length );
		}
	}
	
	//子彈入場
	public void shootAction(){//每10毫秒調一次
		shootIndex++;//每調一次自增1
		if(shootIndex % 25 == 0){//調25次才進來一次
			Bullet[] bs = hero.shoot();//英雄機發射出來的子彈
			//擴容長度等于原陣列長度加上bs的實際長度
			bullets = Arrays.copyOf(bullets,bullets.length+bs.length);
		    System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
		}
	}
	
	//隨機生成敵人(敵機,小心心)
	public static FlyingObject nextOne(){
		Random r = new Random();
		int type = r.nextInt(10);
		//只有6,7,8,9,10的時候生成小心心
		if(type >5){
			return new Bee();
		}else{//其他則生成小飛機
			return new Airplane();
		}
	}
	
	int flyingIndex = 0;//飛行物入場計數
	//飛行物入場
	public void enterAction(){//每10毫秒調一次
		flyingIndex++;//調一次自增1
		if(flyingIndex % 80 == 0 ){//走30次,10*30 300毫秒
			FlyingObject obj = nextOne();//隨機生成的敵人
			flyings = Arrays.copyOf(flyings,flyings.length+1);//擴容
			flyings[flyings.length-1] = obj;//將生成的敵人放入擴容后的陣列最后一位
		}
	}
	
	//飛行物走步
	public void stepAction(){
		//敵人(小心心,小飛機)
		for(int i=0;i<flyings.length;i++){
			flyings[i].step();
		}
		//子彈走步
		for(int i=0;i<bullets.length;i++){
			bullets[i].step();
		}
		//boss子彈走步
		for(int i=0;i<Bullets.length;i++){
			Bullets[i].step();
		}
		if(score>=30){
		bosses.step();
		}
		hero.step();
	}
	
	public static void main(String[] args) {
		//畫框
		JFrame frame = new JFrame("FLY shoot");
		ShootGame game = new ShootGame();//畫板
	    frame.add(game); //將畫板嵌入畫框上
		frame.setSize(WIDTH,HEIGHT);//大小
		frame.setAlwaysOnTop(true);//總在最上
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默認關閉
		frame.setLocationRelativeTo(null);//初始表單位置
		frame.setVisible(true);//顯示,盡快呼叫paint()方法
	    game.action();//界面動起來	
	}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/236671.html

標籤:其他

上一篇:Unity控制物體生成--協程方法

下一篇:《學Unity的貓》——第十五章:Unity粒子系統ParticleSystem,下雪啦下雪啦

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more