JAVA学习day44--飞机大战14子弹入场

子弹入场

package day19;

import java.awt.image.BufferedImage;

/**英雄机*/
public class Hero  extends FlyingObject{
	private static BufferedImage[] images;
	static{
		//读图片
		images = new BufferedImage[2];
		images[0] = loadImage("hero0.png");
		images[1] = loadImage("hero1.png");
	}
	private int life;         //命
	private int doubleFire;   //火力值
	/** 构造方法*/
	public Hero(){
		super(97,124,140,400);
		life = 3;
		doubleFire = 0;
	}
	
	/**重写stop    */
	public void step(){
		System.out.println("英雄机切换图片");
	}
	
	/** 英雄级随着鼠标移动*/
	void moveTo(int x,int y){
		System.out.println("英雄机随着鼠标移动");
	}
	
	/**重写getImage() */
	int index = 0;//下标
	public  BufferedImage getImage(){//每10毫秒走一次
		if(isLife()){//若活着的
			return images[index++%images.length];
		}
		return null;
	};
	/** 英雄机发射子弹()生成子弹对象*/
	public Bullet[] shoot(){
		int xStep = this.width/4;//1/4英雄机的宽
		int yStep = 20;//固定的值
		if(doubleFire>0){//双
			Bullet [] bs = new Bullet[2];//2反子弹
			bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);//x:英雄机的x+1/4英雄机的宽 y:英雄机y-固定的20
			bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);//x:英雄机的x+3/4英雄机的宽 y:英雄机y-固定的20
			doubleFire -=2;//发射一次双倍火力,则活力值减2
			return bs;
		}else{//单
			Bullet [] bs = new Bullet[1];//1发子弹
			bs[0] = new Bullet(this.x+2*xStep,this.y-yStep);//x:英雄机的x+1/4英雄机的宽 y:英雄机y-固定的20
			 
			return bs;
		}
	}


}

package day19;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.TimerTask;
import java.util.Timer;
import java.awt.Graphics;
import java.util.Random;
import java.util.Arrays;

/** 整个游戏世界*/
public class World extends JPanel{
	public static final int WINTH = 400;//窗口的宽
	public static final int HEIGHT = 700;//窗口的高
	
	private Sky sky = new Sky();
	private Hero hero  = new Hero();
	private FlyingObject[] enemies = {
			new Airplane(),
			new BigAirplane(),
			new Bee()
			
	};//敌人(小敌机,大敌机,小蜜蜂)
	private Bullet[] bullets = {};//子弹数组
	
	/** 生成敌人对象*/
	public FlyingObject nextOne(){
		Random rand = new Random();//随机数对象
		int type = rand.nextInt(20);//0到19之间
		if(type<5){//0到4,生成小蜜蜂
			return new Bee();
		}else if(type<12){//5到11,生成小敌机
			return new Airplane();
		}else{//12到19,生成大敌机
			return new BigAirplane();
		}
	}
	
	int enterIndex = 0;//敌人入场计数
	/** 敌人(小敌机,大敌机,小蜜蜂)入场*/
	public void enterAction(){//每10毫秒走一次
		enterIndex++;//每10毫秒增1
		if(enterIndex%40==0){//每400(40*10)毫秒走一次
			FlyingObject obj = nextOne();//获取敌人对象
			enemies = Arrays.copyOf(enemies,enemies.length+1);//扩容
			enemies[enemies.length-1] = obj;//将敌人添加到末尾
		}
		
	}
	int shootIndex =0;//子弹入场计数
	/** 子弹入场*/
	public void shootAction(){//每10毫秒走一次
		shootIndex++;//没10毫秒曾1
		if(shootIndex%30==0){//每300毫秒增加一次
			Bullet[] bs = hero.shoot();//获取子弹对象
			bullets =Arrays.copyOf(bullets,bullets.length+bs.length);//扩容
			
		}
	}
	
	/** 启动程序的执行*/
	public void action(){
		Timer timer = new Timer();//定时器对象
		int intervel = 10;//以毫秒为单位
		timer.schedule(new TimerTask(){
			public void run(){//定时做的事--每10毫秒执行
				enterAction();
				shootAction();
				repaint();//重画调用paint
			}
		},intervel,intervel);//定时计划
		

		
		}
	/** 重写paint()画*/
	public void paint(Graphics g){
		sky.paintObject(g);//画天空
		hero.paintObject(g);//画英雄机
		for(int i=0;i<enemies.length;i++){
			enemies[i].paintObject(g);//画敌人
		}
		for(int i=0;i<bullets.length;i++){
			enemies[i].paintObject(g);//画子弹
		}
	}

	
	

	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(WINTH,HEIGHT);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);//设置窗口可见
		
		world.action();//启动程序的执行


	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值