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

📄 player.java

📁 实现了 移动 碰撞检测
💻 JAVA
字号:
import javax.microedition.lcdui.Graphics;

public class Player
{
	//各种状态常量
	final static int FIRE = 0; //射击
	final static int GENERAL = 1; //正常
	final static int DEAD = 2; //死亡

	int spriteX, spriteY; //精灵的位置
	int width, height; //瞄准器图片的宽和高

	private int hp = 100; //生命值
	private int collisionW, collisionH; //检测碰撞的宽和高
	private int collisionX, collisionY; //检测碰撞区域的起始坐标

	private Gameworld gameworld; //Canvas对象的实例

	private int currentFrame; //单前的帧数
	private int totalFrame; //总帧数
	private int tempFrame; //用来临时保存当前的状态的起始帧
	private int tempTotalFrame; //用来临时保存当前状态的结束帧

	int playerState; //精灵的状态
	private int nextState; //精灵的下一个状态
	private boolean isChangeState;//状态是否更新

	int airAttack = 5; //空袭数
	static int bulletCount; //子弹数量
	private int execution; //杀伤力
	boolean isAirAttack; //是否使用了空袭
	private long lastTime; //时钟周期,射击延时

	//构造
	public Player(int w, int h, Gameworld world)
	{
		width = 16;
		height = 16;
		gameworld = world;
		//初始化玩家的状态
		setPosition(gameworld.screenWidth / 2, gameworld.screenHeight / 2);
		playerState = Player.GENERAL;
		nextState = playerState;
		changeState(playerState);
		//玩家视角属性
		collisionW = 4;
		collisionH = 4;
		collisionX = 4;
		collisionY = 4;
		execution = 1;
		bulletCount = 30;
	}

	//增加空袭,颗数可以限制
	final void addAir()
	{//一次增加的空袭导弹个数限制为 add
		int add = 0;
		if (airAttack < 5)
			add = 1;
		for (int i = 0; i < add; i++)
			Gameworld.bombVector.addElement(Gameworld.itemImage);
		airAttack = airAttack + add;
	}

	//请求使用一次空袭
	final void questAirAttack()
	{
		if (airAttack > 0)
		{
			isAirAttack = true;
			airAttack--;
			Gameworld.bombVector.removeElementAt(0);//生命图标减一
		}
	}

	//增加子弹,颗数可以限制
	final void addBullet()
	{
		int add = 0;
		if (bulletCount <= 20)
			add = 10;
		else if (bulletCount > 20)
			add = 30 - bulletCount;
		for (int i = 0; i < add; i++)
			Gameworld.bulletVector.addElement(Gameworld.itemImage);
		bulletCount = bulletCount + add;
	}

	//发射一颗子弹,图标减一
	final void bulletFire()
	{
		if (bulletCount < 0)
		{
			bulletCount = 0;
		}
		if (bulletCount > 0)
		{
			Gameworld.bulletVector.removeElementAt(0);//子弹图标减一
		}
		bulletCount--;
	}

	/**
	 * 更新精灵的状态
	 * 
	 * @param st
	 *            要改变的状态
	 */
	public final void changeState(int st)
	{
		playerState = st;
		//根据状态设置起始帧和结束帧
		switch (st)
		{
		case FIRE: //设置开火的动画
			tempFrame = 0; //起始帧
			tempTotalFrame = 1; //最大帧数,即结束帧
			break;
		case GENERAL: //设置普通状态动画
			tempFrame = 0;
			tempTotalFrame = 0;
			break;
		case DEAD: //设置游戏结束动画
			tempFrame = 0;
			tempTotalFrame = 0;
			break;
		}
		isChangeState = true;
	}

	//准确设置生命值
	public final void setHP(int i)
	{
		if (i > 100)
		{
			hp = 100;
		} else if (i < 10)
		{
			hp = 10;
		} else
		{
			hp = i;
		}
	}

	//获取现有的生命值
	public final int getHP()
	{
		return hp;
	}

	//设置精灵位置
	public final void setPosition(int x, int y)
	{
		spriteX = x;
		spriteY = y;
	}

	//增加生命值
	public final void addHP()
	{
		int size = 20;
		if (hp > 70)
			size = 20;
		else if (hp > 50)
			size = 40;
		else
			size = 50;
		hp += size;
		setHP(hp);
	}

	/**
	 * 被子弹击中后,扣掉生命值
	 * 
	 * @param i_hp
	 *            损伤的生命值
	 */
	public final void killHP(int i_hp)
	{
		hp -= i_hp;
		setHP(hp);
		if (hp <= 0)
		{
			playerState = Player.DEAD; //设置为游戏结束状态
			if (gameworld.sound)
			{
				try
				{
					gameworld.soundSFX[3].start();
				} catch (Exception ex)
				{
					ex.printStackTrace();
				}
			}
		}
	}

	//与敌人的碰撞检测,矩形检测
	final boolean isCollision1(Enemy sp, int x, int y)
	{
		if (sp.collisionY + sp.collisionH < y || sp.collisionY > y + collisionH
				|| sp.collisionX + gameworld.offsetX + sp.collisionW < x
				|| sp.collisionX + gameworld.offsetX > x + collisionW)
		{
			return false;
		}
		return true;
	}

	//动作处理
	public final void action()
	{
		nextState = FIRE; //设置下一个状态为开火
		//循环取出屏幕上的每一个精灵
		for (int i = 0; i < gameworld.allEnemy.length; i++)
		{
			if (gameworld.allEnemy[i].visible
					&& gameworld.allEnemy[i].enemyState != Enemy.STA_DEAD
					&& gameworld.allEnemy[i].enemyState != Enemy.STA_SPUER)
			{
				//检测是否击中敌人
				if (isCollision1(gameworld.allEnemy[i], collisionX + spriteX,
						collisionY + spriteY))
				{
					if (gameworld.allEnemy[i].spriteType == 16
							|| gameworld.allEnemy[i].spriteType == 23)
					{
						if (gameworld.sound)
						{
							try
							{
								gameworld.soundSFX[2].start();
							} catch (Exception ex)
							{
								ex.printStackTrace();
							}
						}
					}
					gameworld.allEnemy[i].killHP(execution);
				}
			}
		}
	}

	/**
	 * 根据精灵状态播放动画帧
	 * 
	 * @return 所有动画播放完后返回true 否则返回false
	 */
	public final boolean playFrame()
	{
		boolean temp = false;
		if (isChangeState)
		{//是否需要重新设置起始帧和结束帧
			this.currentFrame = tempFrame;
			this.totalFrame = tempTotalFrame;
			isChangeState = false;
		}
		currentFrame++;
		if (currentFrame > totalFrame)
		{
			temp = true;
			currentFrame = tempFrame;
		}
		return temp;
	}

	//移动精灵
	public final void move(int i, int j)
	{
		spriteX += i;
		spriteY += j;
		if (spriteX <= -this.width / 2)
		{
			spriteX = -this.width / 2;
		}
		//控制其不超出游戏的左右边界
		if (spriteX >= gameworld.screenWidth - this.width / 2)
		{
			spriteX = gameworld.screenWidth - this.width / 2;
		}

		if (spriteY <= 0)
		{
			spriteY = 0;
		}
		//控制其不超出游戏的上下边界
		if (spriteY >= gameworld.screenHeight - this.height)
		{
			spriteY = gameworld.screenHeight - this.height;
		}

	}

	//游戏逻辑
	public final void logic()
	{
		if (lastTime == 0)
		{
			lastTime = System.currentTimeMillis();
		}
		boolean temp = playFrame(); //播放精灵动画
		switch (playerState)
		{ //根据状态来更新动画
		case FIRE: //开火
		case GENERAL: //瞄准
			if (temp)
			{ //上一个动画播放完
				changeState(nextState); //改变当前的状态为下一个状态
			}
			break;
		case DEAD: //死亡

			break;
		}
		nextState = Player.GENERAL;
		lastTime = System.currentTimeMillis();
	}

	//绘制
	final void paint(Graphics g)
	{
		gameworld.drawFrame(gameworld.playerImage, currentFrame, spriteX,
				spriteY, 19, 19, 0, g);
	}
}

⌨️ 快捷键说明

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