⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ufo.java

📁 j2me手机游戏。只开发了一部份。。可能可以让初学者学到一点。 开发环境midp1.0
💻 JAVA
字号:

import javax.microedition.lcdui.*;


class UFO extends Sprite {

	private	MyShip				myShip;
	
	private int				missileCount		= 0;
	
	private int				direction;
	
	static final int			DIRECTION_RIGHT		= 0;
	
	static final int			DIRECTION_LEFT		= 1;
	
	private static Image		ufoImg				= null;
	
	private static Image		burstImg			= null;

	
	UFO(MyShip myShip) {
		this.myShip = myShip;

		if(ufoImg == null) {
		//UFO图像为null时就取得图像
			try {
				ufoImg = Image.createImage("/z4.png");
				burstImg = Image.createImage("/imgenemybomb.png");
			}catch(Exception e) {}
		}

		//设定宽度与高度
		width = ufoImg.getWidth();
		height = ufoImg.getHeight();
	}

	
	void setAlive(boolean isAlive) {
		if(isAlive) {
		//由于在变成Alive状态时会将这个UFO复活,因此要把值予以初始化
			this.isHit = false;
			missileCount = 0;
			tickCount = 0;
		}
		super.setAlive(isAlive);
	}

	
	void doMove() {
		if(this.isAlive) {		//Alive时
			if(isHit()) {		//Hit时
				tickCount++;
				//Tick计数为5的话就会从画面消失
				if(tickCount > 4) {
					setAlive(false);
				}
			}else {				//不是Hit时
				//针对行进方向来移动UFO
				switch(direction) {
					case DIRECTION_RIGHT:
						x = x + (width / 2) + 1;
						break;

					case DIRECTION_LEFT:
						x = x - (width / 2) - 1;
						break;
				}
			}
		}
	}

	
	void doDraw(Graphics g) {
		if(isAlive) {
			//System.out.println(isHit);
			if(isHit()) {
			//Hit时就描绘爆炸图像
				//System.out.println("BOMB!!!!");
				g.drawImage(burstImg, x, y, Graphics.TOP|Graphics.LEFT);
			}else {
				g.drawImage(ufoImg, x, y, Graphics.TOP|Graphics.LEFT);
			}
		}
	}

	
	void setDirection(int direction) {
		this.direction = direction;
	}

	
	boolean isDropBomb() {
		if(missileCount < 3) {
		//飞弹的发射数比3发还少时
			//切割出UFO的中央坐标
			int center = x + (width /2);
			if(center >= myShip.getX() && center <= myShip.getX() + myShip.getWidth()) {
			//UFO的中央坐标与自机的X坐标重叠时
				//发射飞弹
				missileCount++;
				return true;
			}
		}
		//处理到这个地方就不发射飞弹
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -