首页>代码>JAVA swing开发飞机大战小游戏>/javatfly/src/javatfly/Ep.java
package javatfly;

import java.util.Random;

/**
 * 敌机类
 * @author XXX
 *游戏中的敌机
 *1.敌机的数量是未知的  
 *2.敌机的数量很多  JAVA中存放多个教程   1.使用数组(因不能明确敌机的数量,因此不用数组)  2.使用集合
 */
public class Ep extends FlyObject {
         //构建各种敌机的速度
	    int sp;//敌机的速度
		int hp;//敌机的血量
		int type;//敌机的类型
		
	    /**
		 * 构造器:给敌机定型
		 */
		public Ep() {
			//定义随机数的类
			Random rd=new Random();
			//nextInt(num)表示会在[0,num]之间随机生成0~num-1
			//生成一个随机数,用来选取图片
			int index =rd.nextInt(15)+1;
			//设置敌机的类型版本
			type=index;
			//三目运算符
			//格式:条件表达式?式1:式2;
			//表示如果条件true,执行式1,false,执行式2
			String path="/img/ep"+(index<10?"0":"")+index+".png";
			//确定敌机显示图片
			img= App.getlmg(path);
			//确定敌机的大小
			//使用图片的大小,作为飞机的大小
			w=img.getWidth();
			h=img.getHeight();
			//确定敌机的位置 
			x=rd.nextInt(512-w);
		    y=-h;
		    //设置速度
		    sp=18-index;
		    //设置血量
		    hp=index;
		}
		/**
		 * 敌机移动的方法
		 * 目前:让敌机向下移动
		 */
		public void move()
		{
			if(type==11)
			{
				x-=2;
				y+=sp;
			}else if(type==9)
			{
				x+=2;
				y+=sp;
			}else
			{
			y+=sp;
			}
		}
		/**
		 * 当子弹跟敌机碰撞后消失
		 * @param f
		 * @return
		 */
		public boolean shootBy(Fire f) {
			// TODO Auto-generated method stub
			boolean hit = (x<=f.x+f.w  && 
					      x>=f.x-w  &&  
					      y<=f.y+f.h &&
					      y>=f.y-h);
			return hit;
		}
		
		public boolean hitBy(Hero f) {
			boolean hit = (x<=f.x+f.w  && 
				      x>=f.x-w  &&  
				      y<=f.y+f.h &&
				      y>=f.y-h);
		return hit;
		}
}
最近下载更多
bssheep  LV1 2023年12月31日
陈小灏  LV14 2023年12月15日
我的java  LV8 2023年12月14日
1061404770  LV3 2022年9月26日
agjbvfhjbg  LV6 2022年9月9日
Jasonast  LV1 2022年6月12日
testuser1234567  LV24 2022年5月31日
包呼和  LV10 2022年5月11日
wyx065747  LV67 2022年5月7日
rodert  LV14 2022年4月29日
最近浏览更多
gzcznb  LV8 4月1日
bssheep  LV1 2023年12月31日
我的java  LV8 2023年12月14日
jkjfdgbkl  LV2 2023年11月1日
1766545549  LV1 2022年11月17日
李沛阳  LV1 2022年11月6日
豆子小兔子  LV9 2022年11月3日
微信网友_6191697646571520  LV6 2022年10月28日
1061404770  LV3 2022年9月26日
迷迭香  LV10 2022年9月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友