编程语言
首页 > 编程语言> > JAVA学习day46--飞机大战16飞行物入场2

JAVA学习day46--飞机大战16飞行物入场2

作者:互联网

天空移动

package day19;
import java.awt.image.BufferedImage;
/** 天空*/
public class Sky extends FlyingObject{
	private static BufferedImage image;
	static{
		image = loadImage("background.png");
	}

	private int speed;    //移动速度
	private int y1;       //第二张图的y坐标
	/** 构造方法*/
	public Sky(){
		super(World.WINTH,World.HEIGHT,0,0);
		speed = 1;
		y1 = -World.HEIGHT;
		
	}
	/** step*/
	public void step(){
		y+=speed;//y+(向下)
		y1+=speed;//y1+(向下)
		if(y>=World.HEIGHT){//若y>=窗口的高,表示,表示已经移出去了
			y-=World.HEIGHT;//则将y修改为负的窗口的高(挪到最上面去)
		}
		if(y1>=World.WINTH){//若y1>=窗口的高,表示,表示已经移出去了
			y1-=World.HEIGHT;//则将y1修改为负的窗口的高(挪到最上面去)
		}
	}
	/**重写getImage() */
	public  BufferedImage getImage(){
		return image;//直接返回image即可
	};

}

主程序

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 stepAction(){//每10毫秒走一次
		sky.step();//天空东
		for(int i=0;i<enemies.length;i++){//遍历所有敌人
			enemies[i].step();//敌人动
		}
		for(int i=0;i<bullets.length;i++){//遍历所有子弹
			bullets[i].step();//子弹动
		}
	}
	
	/** 启动程序的执行*/
	public void action(){
		Timer timer = new Timer();//定时器对象
		int intervel = 10;//以毫秒为单位
		timer.schedule(new TimerTask(){
			public void run(){//定时做的事--每10毫秒执行
				enterAction();//敌人
				shootAction();//子弹入场
				stepAction();//飞行物移动
				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();//启动程序的执行


	}

}

标签:JAVA,16,飞行物,int,World,new,毫秒,public,enemies
来源: https://blog.csdn.net/qq_39247931/article/details/117048873