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

📄 bricks.java

📁 J2ME编程的50个例子,适合掌上系统的编程
💻 JAVA
字号:
package demo;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;

public class Bricks {
	private TiledLayer	m_BackBrick;				//堆积的背景方块
	private BrickGroup	m_BrickGroup;				//当前的方块组合
	private int m_groupCol;						//当前下落的方块组合所在的列
	private int m_groupRow;						//当前下落的方块组合所在的行
	private int m_nNextX;							//下一个方块组合的预览位置
	private int m_nNextY;							//下一个方块组合的预览位置
	private int m_nTime;							//方块组合下落的间隔时间
	private int m_nDestroyNum;						//已经消除的行数
	
	//构造方法
	//参数backX、backY分别是背景方块区域的左上角的X、Y坐标
	//参数nextX、nextY分别是下一个方块预览位置的左上角的X、Y坐标
	public Bricks( int backX, int backY, int nextX, int nextY ){
		try{
			Image img = Image.createImage("/demo/brick.png");
			m_BackBrick = new TiledLayer( 10, 17, img, 9, 9);
			m_BackBrick.setPosition(backX, backY);
			m_BrickGroup = new BrickGroup(img);
			m_nNextX = nextX;
			m_nNextY = nextY;
		}
		catch(Exception e){}
		Reset();
	}
	//重新设置所有方块
	public void Reset(){
		for( int col = 0; col < m_BackBrick.getColumns(); col ++ ){
			for( int row = 0; row < m_BackBrick.getRows(); row ++ ){
				m_BackBrick.setCell(col, row, 0);
			}
		}
		m_BrickGroup.ResetBrick();
		NextGroup();
		m_nTime = 10;
		m_nDestroyNum = 0;
	}
	//设置新的移动的方块组合
	private void NextGroup(){
		m_groupCol 	= 3;
		m_groupRow	= 0;
		m_BrickGroup.NextBricks();
	}
	//获取已消除的行数
	public int getDestroyNum(){
		return m_nDestroyNum;
	}
	//处理按键的输入,参数keyStates是按键状态
	public void Input(int keyStates){
		if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 ){
			if( !Collides( m_groupCol - 1, m_groupRow ) )
			{//如果向左移动一个格子,不会和背景方块发生碰撞
				m_groupCol --;
			}
		}
		if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 ){
			if( !Collides( m_groupCol + 1, m_groupRow ) )
			{//如果向右移动一个格子,不会和背景方块发生碰撞
				m_groupCol ++;
			}
		}
		if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 ){
			if( !Collides( m_groupCol, m_groupRow + 1 ) )
			{//如果向下移动一个格子,不会和背景方块发生碰撞
				m_groupRow ++;
			}
		}
		if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 ){
			m_BrickGroup.RotCurBrick(true);
			if( Collides( m_groupCol, m_groupRow ) )
			{//如果旋转后与背景发生碰撞,则再转回原来角度
				m_BrickGroup.RotCurBrick(false);
			}
		}
			
	}
	public boolean Logic(){
		m_nTime --;
		if( m_nTime >= 0 )
			return false;
		m_nTime = 10;				//每秒自动向下移动一次,1秒=10*100毫秒
		if( Collides( m_groupCol, m_groupRow + 1 ) )
		{//如果向下移动一个格子,会和背景方块发生碰撞
			if( m_groupRow <= 1 )	//游戏结束
				return true;
			else					//出现下一个方块组合
				Stack();	
		}
		else
		{
			m_groupRow ++;
		}
		return false;
	}
	//显示所有方块,参数g对应手机屏幕
	public void Paint(Graphics g){
		m_BackBrick.paint(g);
		
		//显示移动的方块组合
		TiledLayer TL = m_BrickGroup.getCurBrick();
		int x = m_BackBrick.getX() + m_groupCol * 9;
		int y = m_BackBrick.getY() + m_groupRow * 9;
		TL.setPosition( x, y );
		TL.paint(g);
		
		//显示下一个方块组合
		TL = m_BrickGroup.getNextBrick();
		TL.setPosition( m_nNextX, m_nNextY );
		TL.paint(g);
	}
	//将方块组合堆积到背景上
	private void Stack(){
		//将当前移动的方块组合设置成背景方块
		TiledLayer TL = m_BrickGroup.getCurBrick();
		for( int col = 0; col < 4; col ++ ){
			for( int row = 0; row < 4; row ++ ){
				int index = TL.getCell(col, row);
				if( index != 0 )
				{
					m_BackBrick.setCell(m_groupCol + col, m_groupRow + row, index);
					//检测该行是否可以被消除
					boolean bDestroy = true;
					for( int c = 0; c < m_BackBrick.getColumns(); c ++ )
					{
						if( m_BackBrick.getCell(c, m_groupRow + row) == 0 )
						{
							bDestroy = false;
							break;
						}
					}
					if( bDestroy ){
						m_nDestroyNum ++;
						Destroy(m_groupRow + row);		//消除该行的方块
					}
				}
			}
		}
		NextGroup();			//设置新的移动的方块组合
	}
	//判断当前的移动方块组合与背景方块的碰撞
	//返回true表示发生碰撞,返回false未发生碰撞
	private boolean Collides( int groupCol, int groupRow ){
		TiledLayer TL = m_BrickGroup.getCurBrick();
		for( int col = 0; col < 4; col ++ ){
			for( int row = 0; row < 4; row ++ ){
				if( TL.getCell(col,row) != 0 )
				{
					//如果出界也相当于发生碰撞
					if( groupCol + col < 0 )
						return true;
					if( groupCol + col >= m_BackBrick.getColumns() )
						return true;
					if( groupRow < 0 )
						return true;
					if( groupRow + row >= m_BackBrick.getRows() )
						return true;
					//如果背景上也有方块,则碰撞
					if( m_BackBrick.getCell(groupCol + col, groupRow + row) != 0 )
						return true;
				}
			}
		}
		return false;
	}
	//消除某一行,参数row的行数
	private void Destroy( int row ){
		for( int r = row; r > 0; r -- )
		{//该行上面的方块都向下移动一格
			for( int c = 0; c < m_BackBrick.getColumns(); c ++ )
			{
				int index = m_BackBrick.getCell(c, r-1);
				m_BackBrick.setCell(c, r, index );
			}
		}
	}
}

⌨️ 快捷键说明

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