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

📄 boss.java

📁 一个J2ME的游戏,有源程序.是原创!适合S60的176*208的规格.
💻 JAVA
字号:
package src;

import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Boss {

	KingCanvas KC;

	final byte TempRow = 8;

	final short[] BULLET_BOSS_OFFSET = { 13, 208, 13, 181, 13, 66, 13, 38, 2,
			108, 2, 140, 37, 108, 37, 140 };

	LeadingPlaneBullets BatteryBullets[];

	Random random = new Random();

	boolean bFlagBattery_HP[], bFlagBatteryInScreen[];

	boolean bFlagBossHP, bFlagBossState, bFlagBossDie;

	static Image Boss, BigBattery, SmallBattery, BatteryExplosion,
			BigExplosion;

	short Battery_X[], Battery_Y[], BatteryHP[];

	byte BatteryNumber[];

	int BossX, BossY;

	int ImgPlace, Temp, BossState;

	int BossDisplacementCounter, BossMoveCounter, TimeCounter;

	int BatteryExplosionNumber, BigExplosionNumber;

	int BigExplosion_X, BigExplosion_Y;

	public Boss(KingCanvas KC) {
		this.KC = KC;
		InitBoss();
	}

	/**
	 * 初始化BOSS相关属性以及加载相关图片处理方法
	 */

	public void InitBoss() {
		Battery_X = new short[TempRow];
		Battery_Y = new short[TempRow];
		BatteryHP = new short[TempRow];
		BatteryNumber = new byte[TempRow];
		bFlagBatteryInScreen = new boolean[TempRow];
		bFlagBattery_HP = new boolean[TempRow];

		BatteryBullets = new LeadingPlaneBullets[TempRow];
		for (byte i = 0; i < BatteryBullets.length; i++) {
			BatteryBullets[i] = new LeadingPlaneBullets();
		}

		BossX = KC.GetRandom(126);
		BossY = -KingCanvas.SCREEN_HEIGHT << 3;
		BossMoveCounter = -250;
		MoveBossState();
		InitBatteryHP();
		BatteryPlace();

		try {
			Boss = Image.createImage("/res/picture/boss.png");
			BigBattery = Image.createImage("/res/picture/BigBattery.png");
			SmallBattery = Image.createImage("/res/picture/SmallBattery.png");
			BatteryExplosion = Image
					.createImage("/res/picture/smallplanebom_.png");
			BigExplosion = Image.createImage("/res/picture/leadingbom_.png");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 移动BOSS处理方法
	 */

	public void MoveBoss() {
		bFlagBatteryBulletsDisplay();
		if (BossY <= BossMoveCounter) {
			BossY++;
			for (byte i = 0; i < TempRow; i++) {
				Battery_Y[i]++;
			}
		}
		if (bFlagBossState == false) {
			switch (BossState) {
			case 1:
				BossX--;
				for (byte i = 0; i < TempRow; i++) {
					Battery_X[i]--;
				}
				if (BossX == 0) {
					BossDisplacementCounter++;
				}
				if (BossX < 0) {
					BossState = 2;
				}
				break;
			case 2:
				BossX++;
				for (byte i = 0; i < TempRow; i++) {
					Battery_X[i]++;
				}
				if (BossX + Boss.getWidth() >= KingCanvas.SCREEN_WIDTH) {
					BossState = 1;
				}
				break;
			case 0:
				BossY++;
				for (byte i = 0; i < TempRow; i++) {
					Battery_Y[i]++;
				}
				BossState = 2;
				break;
			}
			if (BossDisplacementCounter == 2) {
				BossMoveCounter += 40;
				BossDisplacementCounter = 0;
			}

			if (BossMoveCounter > 260) {
				bFlagBossState = true;
				BossMoveCounter = -260;
			}
		}

		if (bFlagBossState == true) {
			switch (BossState) {
			case 1:
				BossX--;
				for (byte i = 0; i < TempRow; i++) {
					Battery_X[i]--;
				}
				if (BossX <= 0) {
					BossState = 2;
				}
				break;
			case 2:
				BossX++;
				for (byte i = 0; i < TempRow; i++) {
					Battery_X[i]++;
				}
				if (BossX + Boss.getWidth() >= KingCanvas.SCREEN_WIDTH) {
					BossState = 1;
				}
				break;
			}
			BossY -= 2;
			for (byte i = 0; i < TempRow; i++) {
				Battery_Y[i] -= 2;
			}
			if (BossY + Boss.getHeight() <= 0) {
				BossX = KC.GetRandom(126);
				BossY = -KingCanvas.SCREEN_HEIGHT << 3;
				BossDisplacementCounter = 0;
				MoveBossState();
				InitBatteryHP();
				BatteryPlace();
				bFlagBossState = false;
			}
		}
	}

	/**
	 * 炮塔位置处理方法
	 */

	public void BatteryPlace() {
		for (byte i = 0; i < TempRow; i++) {
			Battery_X[i] = (short) (BossX + BULLET_BOSS_OFFSET[i << 1]);
			Battery_Y[i] = (short) (BossY + BULLET_BOSS_OFFSET[(i << 1) + 1]);
		}
		for (byte i = 0; i < TempRow; i++) {
			bFlagBattery_HP[i] = false;
		}
	}

	/**
	 * 炮塔子弹位置处理方法
	 */

	public void BatteryBulletsPlace() {
		for (byte i = 0; i < TempRow; i++) {
			if (bFlagBatteryInScreen[i]) {
				if (BatteryBullets[i].BulletsState == LeadingPlaneBullets.BULLET_STATE_DIE) {
					int bt = LeadingPlaneBullets.BULLET_TYPE_ENEMY_1;
					int speed = 7;
					Image img = BigBattery;
					if (i > 3) {
						bt = LeadingPlaneBullets.BULLET_TYPE_ENEMY_2;
						speed = 4;
						img = SmallBattery;
					}
					BatteryBullets[i].init(bt, BossX
							+ BULLET_BOSS_OFFSET[i << 1] + 1, BossY
							+ BULLET_BOSS_OFFSET[(i << 1) + 1], speed,
							BatteryDirection(Battery_X[i], Battery_Y[i], img
									.getWidth(), img.getHeight()));
					bFlagBatteryInScreen[i] = false;
				}
			}
		}
	}

	/**
	 * 初始化炮塔血量处理方法
	 */

	public void InitBatteryHP() {
		for (byte i = 0; i < TempRow >> 1; i++) {
			BatteryHP[i] = 384;
		}

		for (byte i = TempRow >> 1; i < TempRow; i++) {
			BatteryHP[i] = 256;
		}
	}

	/**
	 * 判断炮塔子弹是否显示处理方法
	 */

	public void bFlagBatteryBulletsDisplay() {
		for (byte i = 0; i < TempRow; i++) {
			for (byte j = 0; j < TempRow; j++) {
				if (Battery_Y[j] > 0 && Battery_Y[j] < KingCanvas.SCREEN_HEIGHT) {
					bFlagBatteryInScreen[i] = true;
				} else {
					bFlagBatteryInScreen[i] = false;
				}
			}
		}
	}

	/**
	 * 炮塔方向处理方法
	 */

	public int BatteryDirection(int x, int y, int col, int row) {

		if ((x - 32 > KC.LeadingPlane_X
				+ KingCanvas.imageLeadingPlane.getWidth())
				&& (y < KC.LeadingPlane_Y)) {
			ImgPlace = 7;
			return LeadingPlaneBullets.BULLET_DIR_ENEMYLEFTDOWN;
		}

		if ((x - 32 > KC.LeadingPlane_X
				+ KingCanvas.imageLeadingPlane.getWidth())
				&& ((y - 24 <= KC.LeadingPlane_Y
						+ KingCanvas.imageLeadingPlane.getHeight()) && (y + row
						+ 24 >= KC.LeadingPlane_Y))) {
			ImgPlace = 0;
			return LeadingPlaneBullets.BULLET_DIR_LEFT;
		}

		if ((x - 32 > KC.LeadingPlane_X
				+ KingCanvas.imageLeadingPlane.getWidth())
				&& (y > KC.LeadingPlane_Y)) {
			ImgPlace = 4;
			return LeadingPlaneBullets.BULLET_DIR_ENEMYLEFTUP;
		}

		if ((x + col + 32 < KC.LeadingPlane_X) && (y < KC.LeadingPlane_Y)) {
			ImgPlace = 6;
			return LeadingPlaneBullets.BULLET_DIR_ENEMYRIGHTDOWN;
		}

		if ((x + col + 32 < KC.LeadingPlane_X)
				&& ((y - 24 <= KC.LeadingPlane_Y
						+ KingCanvas.imageLeadingPlane.getHeight()) && (y + row
						+ 24 >= KC.LeadingPlane_Y))) {
			ImgPlace = 2;
			return LeadingPlaneBullets.BULLET_DIR_RIGHT;
		}

		if ((x + col + 32 < KC.LeadingPlane_X) && (y > KC.LeadingPlane_Y)) {
			ImgPlace = 5;
			return LeadingPlaneBullets.BULLET_DIR_ENEMYRIGHTUP;
		}

		if (x == Battery_X[0] || x == Battery_X[1]) {
			if (((x - 32 <= KC.LeadingPlane_X
					+ KingCanvas.imageLeadingPlane.getWidth()) && (x + col + 32 >= KC.LeadingPlane_X))
					&& (y < KC.LeadingPlane_Y
							+ KingCanvas.imageLeadingPlane.getHeight())) {
				ImgPlace = 1;
				return LeadingPlaneBullets.BULLET_DIR_DOWN;
			}
		}

		if (x == Battery_X[2] || x == Battery_X[3]) {
			if (((x - 32 <= KC.LeadingPlane_X
					+ KingCanvas.imageLeadingPlane.getWidth()) && (x + col + 32 >= KC.LeadingPlane_X))
					&& (y > KC.LeadingPlane_Y
							+ KingCanvas.imageLeadingPlane.getHeight())) {
				ImgPlace = 3;
				return LeadingPlaneBullets.BULLET_DIR_UP;
			}
		}
		return LeadingPlaneBullets.BULLET_DIR_DOWN;
	}

	/**
	 * 确定炮塔子弹位置处理方法
	 */

	public void Fixedplace() {
		for (byte i = 0; i < TempRow >> 1; i++) {
			if (BatteryBullets[i].Bullets_Y > KingCanvas.SCREEN_HEIGHT
					|| BatteryBullets[i].Bullets_Y == Temp
					|| BatteryBullets[i].Bullets_Y
							+ LeadingPlaneBullets.BigBatteryBullets.getHeight() < 0
					|| BatteryBullets[i].Bullets_X
							+ LeadingPlaneBullets.BigBatteryBullets.getWidth() < 0
					|| BatteryBullets[i].Bullets_X > KingCanvas.SCREEN_WIDTH) {
				BatteryBullets[i].Bullets_X += KingCanvas.SCREEN_WIDTH;
				BatteryBullets[i].Bullets_Y += KingCanvas.SCREEN_HEIGHT;
			}
		}

		for (byte i = TempRow >> 1; i < TempRow; i++) {
			if (BatteryBullets[i].Bullets_Y > KingCanvas.SCREEN_HEIGHT
					|| BatteryBullets[i].Bullets_Y == Temp
					|| BatteryBullets[i].Bullets_Y
							+ LeadingPlaneBullets.SmallBatteryBullets
									.getHeight() < 0
					|| BatteryBullets[i].Bullets_X
							+ LeadingPlaneBullets.SmallBatteryBullets
									.getWidth() < 0
					|| BatteryBullets[i].Bullets_X > KingCanvas.SCREEN_WIDTH) {
				BatteryBullets[i].Bullets_X += KingCanvas.SCREEN_WIDTH;
				BatteryBullets[i].Bullets_Y += KingCanvas.SCREEN_HEIGHT;
			}
		}
		Temp = 0;
	}

	/**
	 * 炮塔子弹碰撞处理方法
	 */

	public void BulletsCollide() {
		if (KC.bFlagLeadingPlaneCollide == false) {
			for (byte i = 0; i < TempRow >> 1; i++) {
				if (KC
						.isRam(
								BatteryBullets[i].Bullets_X + 3,
								BatteryBullets[i].Bullets_Y + 3,
								LeadingPlaneBullets.BigBatteryBullets
										.getWidth() - 6,
								(LeadingPlaneBullets.BigBatteryBullets
										.getHeight() >> 2) - 6,
								KC.LeadingPlane_X + 4,
								KC.LeadingPlane_Y + 4,
								KingCanvas.imageLeadingPlane.getWidth() - 8,
								(KingCanvas.imageLeadingPlane.getHeight() / 3) - 8)) {
					Temp = BatteryBullets[i].Bullets_Y;
					KC.LeadingPlaneHP -= 2;
				}
			}

			for (byte i = TempRow >> 1; i < TempRow; i++) {
				if (KC.isRam(BatteryBullets[i].Bullets_X,
						BatteryBullets[i].Bullets_Y,
						LeadingPlaneBullets.SmallBatteryBullets.getWidth(),
						LeadingPlaneBullets.SmallBatteryBullets.getHeight(),
						KC.LeadingPlane_X + 4, KC.LeadingPlane_Y + 4,
						KingCanvas.imageLeadingPlane.getWidth() - 8,
						(KingCanvas.imageLeadingPlane.getHeight() / 3) - 8)) {
					Temp = BatteryBullets[i].Bullets_Y;
					KC.LeadingPlaneHP--;
				}
			}
		}
	}

	/**
	 * 炮塔状态处理方法
	 */

	public void AllBatteryState() {
		for (byte i = 0; i < TempRow >> 1; i++) {
			BatteryNumber[i] = (byte) ImgPlace;
			BatteryDirection(Battery_X[i], Battery_Y[i], BigBattery.getWidth(),
					BigBattery.getHeight());
		}

		for (byte i = TempRow >> 1; i < TempRow; i++) {
			BatteryNumber[i] = (byte) ImgPlace;
			BatteryDirection(Battery_X[i], Battery_Y[i], SmallBattery
					.getWidth(), SmallBattery.getHeight());
		}
	}

	/**
	 * 移动BOSS状态处理方法
	 */

	public void MoveBossState() {
		if (BossX + 25 > KC.LeadingPlane_X + KC.p24width) {
			BossState = 1;
		} else {
			BossState = 0;
			if (BossX + 25 < KC.LeadingPlane_X) {
				BossState = 2;
			}
		}
	}

	public int getRandom(int min, int max) {
		return (Math.abs(random.nextInt()) % ((max - min + 1))) + min;
	}

	// -------------------------------------------- 画笔

	/**
	 * BOSS画笔
	 */

	public void drawBoss(Graphics g) {
		KingCanvas.Brush(g, Boss, BossX, BossY, Boss.getWidth(), Boss
				.getHeight(), 0, 0);
	}

	/**
	 * 炮塔画笔
	 */

	public void drawBattery(Graphics g, Image img, int x, int y, int width,
			int height, int Number) {
		KingCanvas.Brush(g, img, x, y, width, height >> 3, Number, 0);
	}

	/**
	 * 小炮塔爆炸画笔
	 */

	public void drawExplosion(Graphics g, int x, int y) {
		KingCanvas.Brush(g, BatteryExplosion, x, y, KC.p16width, KC.p16height,
				BatteryExplosionNumber, 0);
	}

	/**
	 * 大炮塔爆炸画笔
	 */

	public void drawBigExplosion(Graphics g, int x, int y) {
		KingCanvas.Brush(g, BigExplosion, x, y, KC.p24width, KC.p24height,
				BigExplosionNumber, 0);
	}

	/**
	 * 小炮塔爆炸过程画笔
	 */

	public void drawExplosionAction(Graphics g, int x, int y) {
		drawExplosion(g, x, y);
		if (BatteryExplosionNumber < TempRow) {
			BatteryExplosionNumber++;
		} else {
			BatteryExplosionNumber = 0;
		}
	}

	/**
	 * 大炮塔爆炸过程画笔
	 */

	public void drawBigExplosionAction(Graphics g) {
		byte Counter = 0;
		for (byte i = 0; i < TempRow; i++) {
			if (bFlagBattery_HP[i] == true)
				Counter++;
		}
		if (Counter == TempRow) {
			drawBigExplosion(g, getRandom(BossX + 12, BossX + Boss.getWidth()
					- 12), getRandom(BossY + 12, BossY + Boss.getHeight() - 12));
			if (BigExplosionNumber < 6) {
				BigExplosionNumber++;
			} else {
				BigExplosionNumber = 0;
			}
			TimeCounter++;
			if (TimeCounter % 200 == 0) {
				bFlagBossHP = true;
				KC.ScoreCounter += TempRow << 4;
				bFlagBossDie = true;
			}
		}
	}

	/**
	 * 判断是否画炮塔或是画炮塔爆炸画笔
	 */

	public void drawBatteryAndExplosion(Graphics g) {
		for (byte i = 0; i < TempRow >> 1; i++) {
			if (bFlagBattery_HP[i] == false) {
				drawBattery(g, BigBattery, Battery_X[i], Battery_Y[i],
						BigBattery.getWidth(), BigBattery.getHeight(),
						BatteryNumber[i]);
			} else {
				drawExplosionAction(g, Battery_X[i] + 4, Battery_Y[i]);
				BatteryBullets[i].Bullets_X += KingCanvas.SCREEN_WIDTH;
				BatteryBullets[i].Bullets_Y += KingCanvas.SCREEN_HEIGHT;
				bFlagBatteryInScreen[i] = false;
			}
		}

		for (byte i = TempRow >> 1; i < TempRow; i++) {
			if (bFlagBattery_HP[i] == false) {
				drawBattery(g, SmallBattery, Battery_X[i], Battery_Y[i],
						SmallBattery.getWidth(), SmallBattery.getHeight(),
						BatteryNumber[i]);
			} else {
				drawExplosionAction(g, Battery_X[i] - 3, Battery_Y[i] - 3);
				BatteryBullets[i].Bullets_X += KingCanvas.SCREEN_WIDTH;
				BatteryBullets[i].Bullets_Y += KingCanvas.SCREEN_HEIGHT;
				bFlagBatteryInScreen[i] = false;
			}
		}
	}

	// -------------------------------------------------

	/**
	 * BOSS相关处理方法
	 */

	public void BossPacket() {
		MoveBoss();
		AllBatteryState();
		BulletsCollide();
		Fixedplace();
		for (byte i = 0; i < TempRow; i++) {
			if (bFlagBattery_HP[i] == false) {
				BatteryBulletsPlace();
				BatteryBullets[i].BulletsMove();
			}
		}
	}

	/**
	 * BOSS相关画笔
	 */

	public void drawMoveBoss(Graphics g) {
		if (bFlagBossHP == false) {
			drawBoss(g);
		}
		drawBatteryAndExplosion(g);
		drawBigExplosionAction(g);
		for (int i = 0; i < TempRow; i++) {
			if (bFlagBattery_HP[i] == false) {
				BatteryBullets[i].drawImage(g);
			}
		}
	}
}

⌨️ 快捷键说明

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