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

📄 fruitman.java

📁 J2ME编程的50个例子,适合掌上系统的编程
💻 JAVA
字号:
package demo;
import java.util.Random;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;


public class FruitMan {
	//水果Tile的种类
	public static final int FRUIT_BLUE 		= 1;			//蓝色未选种
	public static final int FRUIT_PINK 		= 2;			//粉色未选种
	public static final int FRUIT_GREEN 		= 3;			//绿色未选种
	public static final int FRUIT_YELLOW 		= 4;			//黄色未选种
	public static final int FRUIT_RED 		= 5;			//红色未选种
	public static final int FRUIT_BLUE_SET 	= 6;			//蓝色被选种
	public static final int FRUIT_PINK_SET  	= 7;			//粉色被选种
	public static final int FRUIT_GREEN_SET  	= 8;			//绿色被选种
	public static final int FRUIT_YELLOW_SET 	= 9;			//黄色被选种
	public static final int FRUIT_RED_SET  	= 10;			//红色被选种
	private TiledLayer	m_TLayer;								//水果Tile对象
	private int m_nCurCol = 2;									//当前位置的列号
	private int m_nCurRow = 2;									//当前位置的行号
	private Random m_Random;									//随机对象
	private Explosion m_aExplosion[];							//爆炸对象
	
	//构造方法,参数scrWidth、scrHeight分别是屏幕的宽和高
	public FruitMan( int scrWidth, int scrHeight ){
		m_Random = new Random();
		try{
			//创建水果Tile图像
			Image img = Image.createImage("/demo/fruit.png");
			m_TLayer = new TiledLayer(6, 6, img, 25, 25 );
			int x = scrWidth / 2 - m_TLayer.getWidth() / 2;
			int y = scrHeight / 2 - m_TLayer.getHeight() / 2;
			m_TLayer.setPosition(x, y);
			
			//创建爆炸对象
			Image imgE = Image.createImage("/demo/explosion.png");
			m_aExplosion = new Explosion[6];
			for( int n = 0; n < m_aExplosion.length; n ++ ){
				m_aExplosion[n] = new Explosion(imgE, 25, 25);
			}
		}
		catch (Exception ex){}
	}
	//重新设置各种颜色的水果位置
	public void Reset(){
		for( int col = 0; col < m_TLayer.getColumns(); col ++ ){
			for( int row = 0; row < m_TLayer.getRows(); row ++ ){
				int index = m_Random.nextInt() % (m_TLayer.getColumns() - 1);
				index = Math.abs(index) + 1;
				m_TLayer.setCell(col, row, index);
			}
		}
		m_nCurCol = 2;
		m_nCurRow = 2;
		int type = m_TLayer.getCell(m_nCurCol, m_nCurRow);
		getDestroy(m_nCurCol, m_nCurRow, type);			//获取当前可消除的水果
	}
	//处理按键输入,返回消除水果后取得的分数
	public int Input( int keyStates ){
		if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 )
		{//当前位置左移,并重新设置当前可消除的水果
			if( m_nCurCol > 0 ){
				m_nCurCol --;
				RsetDestroy();
			}
		}
		else if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 )
		{//当前位置右移,并重新设置当前可消除的水果
			if( m_nCurCol < m_TLayer.getColumns() - 1 ){
				m_nCurCol ++;
				RsetDestroy();
			}
		}
		else if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 )
		{//当前位置上移,并重新设置当前可消除的水果
			if( m_nCurRow > 0 ){
				m_nCurRow --;
				RsetDestroy();
			}
		}
		else if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 )
		{//当前位置下移,并重新设置当前可消除的水果
			if( m_nCurRow < m_TLayer.getRows() - 1 ){
				m_nCurRow ++;
				RsetDestroy();
			}
		}
		else if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 )
		{//消除水果
			int n = Destroy();
			RsetDestroy();
			return n;
		}
		return 0;
	}
	//逻辑操作,产生爆炸动画
	public void Logic(){
		for( int n = 0; n < m_aExplosion.length; n ++ )
			m_aExplosion[n].Logic();
	}
	//显示图象,参数g对应手机屏幕
	public void Paint(Graphics g){
		//显示水果
		m_TLayer.paint(g);
		//显示当前选择位置
		int x = m_TLayer.getX() + m_nCurCol * m_TLayer.getCellWidth();
		int y = m_TLayer.getY() + m_nCurRow * m_TLayer.getCellHeight();
		g.drawRect(x, y, m_TLayer.getCellWidth(), m_TLayer.getCellHeight());
		//显示爆炸
		for( int n = 0; n < m_aExplosion.length; n ++ )
			m_aExplosion[n].paint(g);
	}
	//判断是否完成游戏,true完成,false未完成
	public boolean isFinish(){
		for( int col = 0; col < m_TLayer.getColumns(); col ++ ){
			for( int row = 0; row < m_TLayer.getRows(); row ++ ){
				if( m_TLayer.getCell( col, row ) != 0 )
					return false;
			}
		}
		return true;
	}
	//重新设置可消除的水果
	private void RsetDestroy(){
		int dis = FRUIT_BLUE_SET - FRUIT_BLUE;
		for( int col = 0; col < m_TLayer.getColumns(); col ++ ){
			for( int row = 0; row < m_TLayer.getRows(); row ++ ){
				int index = m_TLayer.getCell(col, row);
				if( index >= FRUIT_BLUE_SET ){
					m_TLayer.setCell(col, row, index - dis);
				}
			}
		}
		int type = m_TLayer.getCell(m_nCurCol, m_nCurRow);
		getDestroy(m_nCurCol, m_nCurRow, type);
	}
	//递归查找指定位置及其周围可消除的水果
	//参数col、row指定位置,type为消除的水果类型
	private void getDestroy(int col, int row, int type){
		if( type <= 0 )
			return;
		if( col < 0 || col >= m_TLayer.getColumns() )
			return;
		if( row < 0 || row >= m_TLayer.getRows() )
			return;
		int index = m_TLayer.getCell(col, row);
		
		if( index >= FRUIT_BLUE_SET )
			return;
		
		if( index != type )
			return;
		
		//设置指定位置的水果
		int dis = FRUIT_BLUE_SET - FRUIT_BLUE;
		m_TLayer.setCell(col, row, type+dis);
		
		//重新查找周围的水果
		getDestroy( col - 1, row, type );
		getDestroy( col + 1, row, type );
		getDestroy( col, row - 1, type );
		getDestroy( col, row + 1, type );
	}
	//消除水果,返回本次所得分数
	private int Destroy(){
		int num = 0;	//分数因子
		int nE = 0;		//爆炸数量
		for( int col = 0; col < m_TLayer.getColumns(); col ++ ){
			for( int row = 0; row < m_TLayer.getRows(); row ++ ){
				int index = m_TLayer.getCell(col, row);
				if( index >= FRUIT_BLUE_SET )
				{//如果水果是可消除的
					num ++;
					if( nE < m_aExplosion.length )
					{//如果还可以产生爆炸
						int x = m_TLayer.getX() + m_TLayer.getCellWidth() * col;
						int y = m_TLayer.getY() + m_TLayer.getCellHeight() * row;
						m_aExplosion[nE].Start(x, y);
						nE ++;
					}
					//该位置上方的水果都向下掉落
					for( int r = row; r > 0; r -- )
					{
						int n = m_TLayer.getCell(col, r-1);
						m_TLayer.setCell(col, r, n);
					}
					m_TLayer.setCell( col, 0, 0);
				}
			}
		}
		return num * num;			//得分为分数因子的平方
	}
}

⌨️ 快捷键说明

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