飞机大战

飞机大战

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();//界面动起来	
	}
}

 

 

 

 

 

 

 

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值