swing 飞机大战 四 生成敌人飞机和敌人子弹

敌人飞机类,因为后面还想做些关卡弄不同的飞机,所以加了个飞机类型,线程根据不同的飞机类型类控制移动的规则

package Game;

import java.util.Vector;

import javax.swing.ImageIcon;

public class FoeFly extends Fly implements Runnable{
	public Vector<FoeFly> foeFlyArr;
	public ImageIcon img;
	public int type; //敌人飞机类型
	public FoeFly(){
		foeFlyArr = new Vector<>(); //实例化敌人飞机集合
	}
	public FoeFly(int x, int y, int hp, boolean doom, int type, String path){
		super(x,y,hp,doom);
		this.type = type;
		img = new ImageIcon(path);
	}
	@Override
	public void run() { //敌人飞机移动线程
		while (true){
			try {
				Thread.sleep(8);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			for (int i=0; i<foeFlyArr.size(); i++){
				if (foeFlyArr.get(i).type == 1){ //类型一飞机,直线向下移动
					if (foeFlyArr.get(i).y > 600){
						foeFlyArr.remove(i);
						i--;
						continue;
					}
					foeFlyArr.get(i).y += 1;
				}
			}
		}
	}
}
通过一个线程类来创造敌人飞机

package pass;

import Game.FoeFly;

/**
 * 
 * @author Administrator
 *关卡1的创建飞机线程
 */
public class ProduceNo1 implements Runnable{
	FoeFly foefly;
	public ProduceNo1(FoeFly foefly) {
		this.foefly = foefly;
	}
	@Override
	public void run() {
		while (true){
			try {
				Thread.sleep(2000);//间隔两秒生成一架敌人飞机
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			int x = (int)Math.round(Math.random()*350);
			int y = (int)Math.round(Math.random()*(-20));
			foefly.foeFlyArr.add(new FoeFly(x,y,40,true,1,"img/Foefly-1.png"));
		}
	}
}

敌人子弹通过线程创建出来(在敌人飞机位置生成)

package Game;
/**
 * 
 * @author Administrator
 *生成敌人子弹类线程
 */
public class Pfb implements Runnable{
	FoeFly foefly;
	FoeBomb foebomb;
	public Pfb(FoeFly foefly, FoeBomb foebomb){
		this.foefly = foefly;
		this.foebomb = foebomb;
	}
	@Override
	public void run() {
		while (true){
			//给每个子弹类在敌人飞机位置生成 子弹
			for (int i=0 ;i<foefly.foeFlyArr.size(); i++){
					foebomb.bombarr.add(new FoeBomb(foefly.foeFlyArr.get(i).x+25, foefly.foeFlyArr.get(i).y+50, true,"img/drzd-1.png"));
			}
			try {
				Thread.sleep(1500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
敌人子弹类

package Game;

import java.util.Vector;

import javax.swing.ImageIcon;

/**
 * 
 * @author Administrator
 *敌人子弹线程
 */
public class FoeBomb extends Bomb implements Runnable{
	public FoeBomb(){
		bombarr = new Vector<>();
	}
	public FoeBomb(int x, int y, boolean vis, String path) {
		super(x, y, vis);
		img = new ImageIcon(path);
	}
	@Override
	public void run() {
		while (true){
			try {
				Thread.sleep(7);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			for (int i=0; i<bombarr.size(); i++){
				if (bombarr.get(i).y > 600){//超出边界
					bombarr.remove(i);
					i--;
					continue;
				}
				bombarr.get(i).y += 1;
			}
		}
	}
}

效果图


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值