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

📄 box.java

📁 简单的用Java做的小游戏主要是用了自己的框架来
💻 JAVA
字号:
package role;

import java.awt.Image;

import assistant.Music;
import assistant.PublicVar;

/**
 * 箱子类
 * @author Administrator
 *
 */
public class Box extends SportRole {
	/**箱子图片*/
	public static Image[][] boxImage;
	
	/**箱子类型*/
	private int boxType;
	
	/**移动状态*/
	private int moveState;
	
	/**铁箱子是否下落*/
	private boolean isDOWN;
	
	/**是否产生花道具*/
	public boolean flowerProp;
	
	/**是否产生心道具*/
	public boolean hartProp;
	
	/**木箱子*/
	public static final int ISWOOD = 0;
	
	/**铁箱子*/
	public static final int ISIRON = 1;
	
	/**直接放置*/
	public static final int START = 0;
	
	/** 被松鼠拿在手上*/
	public static final int ACTIVE = 1;

	/**被扔出*/
	public static final int ISMOVE = 2;
	
	/**击中敌人*/
	public static final int ISHIT = 3;
	
	/** 被松鼠蹲下*/
	public static final int SQUAT = 4;
	
	/**松鼠死亡时,箱子也死亡*/
	private boolean isDie;
	
	/**
	 * 构造方法
	 * @param x X坐标
	 * @param y Y坐标
	 * @param boxType  箱子类型
	 * @param flowerProp 是否能产生花道具
	 * @param hartProp 是否能产生心道具
	 */
	public Box(int x, int y,int boxType,
			boolean flowerProp,boolean hartProp) {
		super(x, y, 40, 30, START);
		this.speed = 10;
		this.boxType = boxType;
		this.flowerProp = flowerProp;
		this.hartProp = hartProp;

	}

	@Override
	/**
	 * 重写父类移动方法
	 */
	public void move() {
		// TODO 自动生成方法存根
		this.setImage();//设置图片

		this.setMove();//箱子移动
		
		this.setIron();//设置铁箱子状态
		
		this.setHit();//设置碰撞
		
		this.setDie();//设置松鼠死亡时,箱子状态

	}
	
	/**
	 * 设置箱子移动
	 *
	 */
	private void setMove(){
		if (this.state == Box.ISMOVE) {//如果箱子处于运动状态
			switch (this.moveState) {
			case PublicVar.UP://向上
				this.y -= this.speed;
				break;//向左
			case PublicVar.LEFT:
				this.x -= this.speed;
				break;
			case PublicVar.RIGHT://向右
				this.x += this.speed;
				break;
			}
			this.removeThis();
			
		}
		if(this!= PublicVar.chip.box && this.state == Box.ACTIVE){
			this.isDie=true;
		}
	}
	
	/**
	 * 设置铁箱子状态
	 *
	 */
	private void setIron(){
		if(this.boxType == Box.ISIRON && isDOWN == false &&
				this.state == Box.ISMOVE){//铁箱子被扔出的状态
			if(this.x<0){
				this.x = 0;
				setStart();
			}
			if(this.y<0){
				this.y = 0;
				setStart();
			}
			if(this.x>610){
				this.x = 610;
				setStart();
			}
		}
		if(isDOWN){//处于下落状态
			this.y+=this.speed;
			
			if(this.y >= 415){//箱子停止下落
				this.y = 415;
				isDOWN = false;
			}
		}
	}
	
	/**
	 * 设置碰撞
	 *
	 */
	private void setHit(){
		for(int i=0;i<PublicVar.roleList.size();i++){//遍历角色集合
			
			BaseRole role= PublicVar.roleList.get(i);
			
			if((role instanceof EnemyRole  || role instanceof ON_OFF) &&
					(this.state == Box.ISMOVE || this.state == Box.SQUAT)
					&&	this.rect.intersects(role.rect)){

				if(role instanceof EnemyRole){//击中敌人
					Music.playSingleMusic(Music.PLAY_HITENEMY);
				((EnemyRole)role).setLife(this.moveState);//调用敌人设置生命方法
				}
				else if(role instanceof ON_OFF){//击中开关
					((ON_OFF)role).setOFF();
				}
				
				if(this.boxType == ISWOOD){//木箱子飞出
					this.state=Box.ISHIT;
				}
				else{//铁箱子复位
					setStart();
					this.y+=this.speed*2;
				}
				
				PublicVar.chip.box = null;
			}
			else if(role instanceof EnemyBullet && this.state == Box.START
					&& role.rect.intersects(this.rect)){
				PublicVar.roleList.remove(role);
			}
			
		}
		
		/*设置木箱子击中敌人后飞行方向*/
		if(this.state == Box.ISHIT){
			this.y-=7;
			
			switch (this.moveState) {//根据移动状态,设置X坐标
			
			case PublicVar.LEFT:
				this.x += this.speed;
				break;
			case PublicVar.UP:
			case PublicVar.RIGHT:
				this.x -= this.speed;
				break;

			}
			this.removeThis();
		}
	}
	
	/**
	 * 设置松鼠死亡时,箱子状态
	 *
	 */
	private void setDie(){
		if(PublicVar.isChipDie && this == PublicVar.chip.box){//松鼠生命值为零
			this.isDie = true;
		}
		
		if(this.isDie){//如果松鼠死亡
			if(this.boxType == Box.ISIRON){//铁箱子
				setStart();
				this.isDie = false;
			}
			else{//木箱子
				this.y-=10;
				this.removeThis();
			}
		}
	}
	
	/**
	 * 设置状态复位
	 *
	 */
	private void setStart(){
		this.state = Box.START;
		isDOWN = true;
	}
	
	/**
	 * 
	 *设置图片
	 */
	private void setImage() {

		if (this.boxType == Box.ISWOOD) {//木箱子
			switch (this.state) {
			case START:
				this.img = boxImage[0][0];
				break;
			case ACTIVE:
			case ISHIT:
			case ISMOVE:

				this.img = boxImage[0][1];
				break;
			case SQUAT:

				this.img = boxImage[0][3];
				break;
			}
			if(this.isDie){
				this.img = boxImage[0][1];
			}
		} else if (this.boxType == Box.ISIRON) {//铁箱子
			this.img = boxImage[0][2];
		}
	}
	
	/**
	 * 设置状态
	 * @param state 状态
	 */
	public void setState(int state) {
		this.state = state;
	}

	/**
	 * 设置运动状态
	 * @param moveState 要设置的 moveState
	 */
	public void setMoveState(int moveState) {
		this.moveState = moveState;
	}

}

⌨️ 快捷键说明

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