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

📄 battleplaneactor.java

📁 J2ME飞机设计游戏,希望对学习J2ME的程序员有所帮助
💻 JAVA
字号:
import java.io.IOException;

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

public class BattlePlaneActor extends Actor {
	private BattlePlaneActor() {
		// 初始化4个按键命令
		commands = new Command[4];
		gameMenu = GameMenu.getInstance();
		
 		//liftNum = 3;  //初始化人数
		liftNum = 1;
	}

	public static BattlePlaneActor getInstance(){
		if(planeActor == null){
			planeActor = new BattlePlaneActor();
		}

		return planeActor;
	}

	//清楚主角对象
	public void clean(){
		planeActor = null;
		iScore = 0;       //得分还原
		cleanSpirit();
	}

	public void initWeaponCommand(byte[] weaponType) {
		for (byte weaponIdx = 0; weaponIdx < weaponType.length; weaponIdx++) {
			switch (weaponType[weaponIdx]) {
			case 1: // 大型导弹
				setCommand(weaponIdx, new BigMissleCommand());
				break;
			case 2: // 机枪
				setCommand(weaponIdx, new MachineGunCommand());
				break;
			case 3: // 直射火箭
				setCommand(weaponIdx, new RocketCommand());
				break;
			case 4: // 加速
				setCommand(weaponIdx, new SpeedCommand());
				break;
			case 5: // 呼叫僚机
				setCommand(weaponIdx, new WingPlaneCommand());
				break;
			case 6: // 大型炸弹
				setCommand(weaponIdx, new BombCommand());
				break;
			}
		}
	}

	public void setCommand(int slot, Command command) {
		commands[slot] = command;
	}

	public void loadData(int planeType) {
		// 根据选择飞机的类型加载资源
		switch (planeType) {
		case 1:
			initSpiritDate("/su47.bin", "/battlePlane.png", "/su47Com.bin");
			LifeImage = new CImage("/su47.png");
			break;
		case 2:
			initSpiritDate("/f22.bin", "/battlePlane.png", "/f22Com.bin");
			LifeImage = new CImage("/f22.png");
			break;
		}

		planeInfo = new Image[3];
		for (int i = 0; i < planeInfo.length; i++) {
			try {
				planeInfo[i] = Image.createImage("/infobar" + (i + 1) + ".png");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		numImage = (new CImage("/unumber.png")).getClipImages(8,8);
	}

	//	@planeType 主角飞机的类型
	public void initSpiritDate(String indexbinName,String pngName,String indexcombin){
		super.initSpiritDate(indexbinName,pngName,indexcombin);
	}

	public void init(){
		// 开场动画的一些参数设定
		setIPosX(((GameConstant.iSCREEN_WIDTH>>1))<<8);
		setIPosY((GameConstant.iSCREEN_HEIGHT<<8) + (10<<8));
		setIWidth(6<<8);
		setIHeight(21<<8);

		//还原主角基本信息
		battle_life = 150;
		battle_magic = 100;
		isAddSpeed = false;
		xSpeed = 4;
		ySpeed = 6;
		speedCount = 100;
		magicCount = 0;
		//bulletLevel = 1;
		setAction((byte)0);
		setCurrentFrame(-1);
		isInvincible = false; //设置不是无敌

		PLANESTATE = GameConstant.STATE_GAME_APPEAR; //飞机处始状态
	}

	public void changeMyState(int releasekeyCode, int pressKeyCode) {
		isAutoMove = true; // 默认是有惯性的
		switch (PLANESTATE) {
		case GameConstant.STATE_GAME_APPEAR: // 主角出现
			dealAutoMove();
			battle_life = 150;
			if ((getIPosY() >> 8) <= 188) {
				PLANESTATE = GameConstant.STATE_GAME_FIGHT;
				setCurrentFrame(-1);
				iDy = 0;
				isFire = true;
				setAction(iPS_STILL); // 停留原地
			}
			
			break;
		case GameConstant.STATE_GAME_FIGHT: // 主角战斗
			if (battle_magic < 100) {
				if (magicCount % 2 == 0) {
					battle_magic += 1;
				}

				magicCount++;
			}

			keyPressed(releasekeyCode, pressKeyCode);
			newBullet();
			break;
		case GameConstant.STATE_GAME_DIE: // 主角死亡
			liftNum = (byte) (liftNum - 1);
			if (liftNum < 0) { // 如果没有人数
				// iScore = 0;
				if(dieCount == 80){
					if (world.isBossAppear() != true) { //BOSS没有死亡
						world.cleanNotUsedObject();

						if (world.getGameLevel() == 2) {
							world.cleanObjectBeforeSecBoss();
						} else if (world.getGameLevel() == 3) {
							world.cleanObjectBeforeThBoss();
						}
					}

					world.cleanAllObecjt();

					// 加载游戏菜单
					gameMenu.initGameMenu();

					GameScreen gameScreen = GameScreen.getInstance();
					gameScreen.StopSound();
					GameScreen.gameState = GameConstant.STATE_GAME_OVER;

					dieCount = 0;
				}

				dieCount++;
			}else{
				init(); // 重新初始化一次
			}
			bulletLevel = 1; // 子弹等级初始化
			break;
		case GameConstant.STATE_GAME_WIN: // 每关胜利后
			// 设置血量不变
			dealAutoMove();
			break;
		}
	}

	public void keyPressed(int keyCode,int pressKeyCode){
		if ((keyCode & GameConstant.KEY_U) != 0) {// 按方向键上
			isAutoMove = false;
			iDy = -ySpeed;
			moveUp(getIPosY() >> 8);
		}else if((keyCode & GameConstant.KEY_D) != 0){//按方向键下
			isAutoMove = false;
			iDy = ySpeed;
			moveDown(getIPosY() >> 8);
		}else if((keyCode & GameConstant.KEY_L) !=0){ //按方向键左
			isAutoMove = false;	
			iDx = - xSpeed;			
			moveLeft(getIPosX() >> 8);
		}else if((keyCode & GameConstant.KEY_R) != 0){ //按方向键右
			isAutoMove = false;
			iDx = xSpeed;
			moveRight(getIPosX() >> 8);
		}

		if((pressKeyCode & GameConstant.KEY_NUM1) != 0){ //按键1				
			commands[0].execute(getIPosX(), getIPosY());
		}else if((pressKeyCode & GameConstant.KEY_NUM3) != 0){ //按键3		
			commands[1].execute(getIPosX(),getIPosY());
		}else if((pressKeyCode & GameConstant.KEY_NUM7) != 0){ //按键7
			commands[2].execute(getIPosX(),getIPosY());
		}else if((pressKeyCode & GameConstant.KEY_NUM9) != 0){ //按键9			
			commands[3].execute(getIPosX(),getIPosY());			
		}else if((pressKeyCode & GameConstant.KEY_ATTACK) != 0){ //开火键
			isFire = !isFire;
		}

		autoMove();
	}

	public void moveUp(int yPos){
		if (yPos - ySpeed >= 32)
			setIPosY((yPos - ySpeed) << 8);

		setAction(iPS_AHEAD);
	}
	
	public void moveDown(int yPos){
		if (yPos + ySpeed <= GameConstant.iSCREEN_HEIGHT - 3)
			setIPosY((yPos + ySpeed) << 8);

		setAction(iPS_BACKWARD);
	}
	
	public void moveLeft(int xPos){
		if (xPos - xSpeed >= 14)
			setIPosX((xPos - xSpeed) << 8);
		
		setAction(iPS_MLEFT);
	}
	
	public void moveRight(int xPos){
		if (xPos + xSpeed <= GameConstant.iSCREEN_WIDTH - 14)
			setIPosX((xPos + xSpeed) <<8);

		setAction(iPS_MRIGHT);
	}

	public void autoMove(){
		int xPos = this.getIPosX() >> 8;
		int yPos = this.getIPosY() >> 8;

        if(isAutoMove)
        {
        	if(iDx > 0){
        		iDx -= 1;
        		if(xPos + iDx <= GameConstant.iSCREEN_WIDTH - 14){
        			setIPosX((xPos + iDx) << 8);
        		}
        	}
            
            if(iDx < 0){
            	iDx += 1;
            	if(xPos + iDx >= 14){
            		setIPosX((xPos + iDx) << 8);
            	}
            }

            if (iDy > 0) {
				iDy -= 1;
				if (yPos + iDy <= GameConstant.iSCREEN_HEIGHT - 3)
					setIPosY((yPos + iDy) << 8);
			}

            if (iDy < 0) {
				iDy += 1;
				if (yPos + iDy >= 32)
					setIPosY((yPos + iDy) << 8);
			}
        }
	}

	public void dealAutoMove() {
		iDy -= 1 << 8;
		setIPosY(getIPosY() + iDy);
	}
	
	//绘制游戏界面信息
	public void drawPlaneInfo(Graphics graphics){
		graphics.setClip(0,0,GameConstant.iSCREEN_WIDTH,GameConstant.iSCREEN_HEIGHT);

		graphics.drawImage(planeInfo[0], 2, GameConstant.iSCREEN_HEIGHT
				- GameConstant.IMAGE_PLANEINFO_HEIGHT, GameConstant.TOPLEFT);
		
		//绘制生命值
		graphics.setClip(2, GameConstant.iSCREEN_HEIGHT - 2 - battle_life
				* GameConstant.IMAGE_LIFT_MAGIC_HEIGHT / 150,
				GameConstant.IMAGE_LIFT_MAGIC_WIDTH,
				GameConstant.IMAGE_LIFT_MAGIC_HEIGHT * battle_life / 150);
		graphics.drawImage(planeInfo[1], 2, GameConstant.iSCREEN_HEIGHT - 2
				- GameConstant.IMAGE_LIFT_MAGIC_HEIGHT, GameConstant.TOPLEFT);

		//绘制蓝
		graphics.setClip(2 + GameConstant.IMAGE_PLANEINFO_WIDTH / 2 + 1,
				GameConstant.iSCREEN_HEIGHT - 2 - battle_magic
						* GameConstant.IMAGE_LIFT_MAGIC_HEIGHT / 100,
				GameConstant.IMAGE_LIFT_MAGIC_WIDTH,
				GameConstant.IMAGE_LIFT_MAGIC_HEIGHT * battle_magic / 100);
		graphics.drawImage(planeInfo[2],
				2 + GameConstant.IMAGE_PLANEINFO_WIDTH / 2 + 1,
				GameConstant.iSCREEN_HEIGHT - 2
						- GameConstant.IMAGE_LIFT_MAGIC_HEIGHT, GameConstant.TOPLEFT);

		//绘制得分
		graphics.setClip(0, 0, GameConstant.iSCREEN_WIDTH,
				GameConstant.iSCREEN_HEIGHT);
		
		numImage[iScore / 1000000].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 64, 5, GameConstant.TOPLEFT);
		numImage[(iScore % 1000000) / 100000].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 56, 5, GameConstant.TOPLEFT);
		numImage[(iScore % 100000) / 10000].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 48, 5, GameConstant.TOPLEFT);
		numImage[(iScore % 10000) / 1000].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 40, 5, GameConstant.TOPLEFT);
		numImage[(iScore % 1000) / 100].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 32, 5, GameConstant.TOPLEFT);
		numImage[(iScore % 100) / 10].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 24, 5, GameConstant.TOPLEFT);
		numImage[iScore % 10].drawGetClipImage(graphics,
				GameConstant.iSCREEN_WIDTH - 16, 5, GameConstant.TOPLEFT);


		//绘制生命个数
		//graphics.setClip(0,0,GameConstant.iSCREEN_WIDTH,GameConstant.iSCREEN_HEIGHT);
		for (int lifeIdx = 0; lifeIdx < liftNum; lifeIdx++) {
			LifeImage.drawImage(graphics, GameConstant.iSCREEN_WIDTH
					- (18 + 17 * lifeIdx), 13, GameConstant.TOPLEFT);
		}
	}
	
	public void newBullet() {
		if (isFire) {
			if (newBulletInterval <= 0) {
				world = World.getInstance();
//				world.newBullet(0, getIPosX() - (2 << 8), getIPosY()
//						- (32 << 8), 0, 10 << 8);
				bulletLevel(bulletLevel);
				newBulletInterval = 4;
			} else {
				newBulletInterval -= 1;
			}
		}
	}
	
	private void bulletLevel(byte bulletLevel){
		switch(bulletLevel){
		case 1:
			world.newBullet(0, getIPosX() - (2 << 8), getIPosY()
					- (32 << 8), 0, 10 << 8);
			break;
		case 2:
			world.newBullet(0, getIPosX() + (8 << 8), getIPosY()
					- (32 << 8), 0, 10 << 8);
			
			world.newBullet(0, getIPosX() - (12 << 8), getIPosY()
					- (32 << 8), 0, 10 << 8);
			break;
		case 3:
			world.newBullet(0, getIPosX() - (8 << 8), getIPosY()
					- (32 << 8), 0, 10 << 8);
			
			world.newBullet(0, getIPosX() - (2 << 8), getIPosY()
					- (32 << 8), 0, 10 << 8);
			
			world.newBullet(0, getIPosX() + (4 << 8), getIPosY()
					- (32 << 8), 0, 10 << 8);
			break;
		}
	}
	
	public int getBattle_life() {
		return battle_life;
	}

	public void setBattle_life(int battle_life) {
		this.battle_life = battle_life;
	}

	public int getBattle_magic() {
		return battle_magic;
	}

	public void setBattle_magic(int battle_magic) {
		this.battle_magic = battle_magic;
	}
	
	public boolean isFire() {
		return isFire;
	}

	public void setFire(boolean isFire) {
		this.isFire = isFire;
	}

	public void setXSpeed(int speed) {
		xSpeed = speed;
	}

	public void setYSpeed(int speed) {
		ySpeed = speed;
	}

	public int getXSpeed() {
		return xSpeed;
	}

	public int getYSpeed() {
		return ySpeed;
	}
	
	public boolean isAddSpeed() {
		return isAddSpeed;
	}

	public void setAddSpeed(boolean isAddSpeed) {
		this.isAddSpeed = isAddSpeed;
	}
	
	public byte getLiftNum() {
		return liftNum;
	}

	public void setLiftNum(byte liftNum) {
		this.liftNum = liftNum;
	}
	
	public byte getSpeedCount() {
		return speedCount;
	}

	public void setSpeedCount(byte speedCount) {
		this.speedCount = speedCount;
	}
	
	public void setBulletLevel(byte bulletLevel) {
		this.bulletLevel = bulletLevel;
	}
	
	public byte getBulletLevel() {
		return bulletLevel;
	}
	
	public void setIDy(int dy) {
		iDy = dy;
	}
	
	public boolean isInvincible() {
		return isInvincible;
	}

	public void setInvincible(boolean isInvincible) {
		this.isInvincible = isInvincible;
	}

	private static BattlePlaneActor planeActor;

	//屏幕显示主角信息的参数
	private int battle_life = 0;  	//生命值
	private int battle_magic = 0; 	//魔法值
	private int magicCount = 0;     //魔法增长记数
	private Image [] planeInfo;     //主角血和蓝图片
	private CImage LifeImage;       //生命个数的图片
	private byte liftNum = 1;       //生命个数
	private byte bulletLevel = 1;   //子弹级数

	//主角移动的参数
	private boolean isFire = false; 	//是否开火
	private boolean isAutoMove = true; 	//惯性
	private int iDx = 0; 				//衰减速度dx
	private int iDy = 0; 				//衰减速度dy
	private int xSpeed = 4;             //左右移动速度
	private int ySpeed = 6;				//上下移动速度
	private boolean isAddSpeed = false; //是否加速
	private byte speedCount = 100;      //飞机加速记数

	private Command [] commands;        //对应住角特殊服务器按键命令
	private byte newBulletInterval = 0; //产生子弹的间隔

	//与排行有关的参数
	public static int iScore;				 //主角得分
	private CImage [] numImage = null;       //分数图片

	//主角动作的状态
	private static byte iPS_STILL = 1; 		 //主角呆在原地
	private static byte iPS_AHEAD = 2; 		 //主角前进
	private static byte iPS_BACKWARD = 3;    //主角后退
	private static byte iPS_MRIGHT = 4;      //主角向右飞行
	private static byte iPS_MLEFT = 5;		 //主角向左飞行
	
	//飞机在游戏中的状态
	public static int PLANESTATE = 0;
	private World world;
	private GameMenu gameMenu;
	private byte dieCount;
	private boolean isInvincible = false;  //是否无敌
}

⌨️ 快捷键说明

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