www.pudn.com > 50code.rar > MainCanvas.java, change:2007-02-26,size:6912b


package demo; 
import java.util.Random; 
 
import javax.microedition.lcdui.Graphics; 
import javax.microedition.lcdui.Image; 
import javax.microedition.lcdui.game.GameCanvas; 
 
 
public class MainCanvas extends GameCanvas implements Runnable{ 
	public static final int GAME_UI		= 0;	//进入用户界面 
	public static final int GAME_GAMING	= 1;	//进行游戏 
	public static final int GAME_END		= 2;	//游戏结束 
	public static final int GAME_STATE_NUM = 3;	//状态总数 
	private int m_nState	= GAME_UI;				//存储当前的游戏状态 
 
	private boolean	m_bRunning;					//控制线程运行 
	private Random m_Random;						//随机数对象 
	private MyUI m_UI;								//界面对象 
	private MyBack m_Back;							//背景对象 
	private MyRecord m_Record;						//记录对象 
	private BrickSprite m_aBrick[][];				//砖块对象数组 
	private BarSprite m_Bar;						//木棒对象 
	private BallSprite m_Ball;						//小球对象 
	private int m_nBallNum = 3;					//总共的小球个数 
	public MainCanvas(){ 
		super(true); 
		m_UI	 	= new MyUI(); 
		m_Back		= new MyBack(); 
		m_Random 	= new Random(); 
		m_Record 	= new MyRecord("BrickRecord"); 
		try{ 
			//读取砖块图像,并设置初始位置 
			Image img = Image.createImage("/demo/brick.png"); 
			m_aBrick = new BrickSprite[5][8]; 
			int x = getWidth()/2 - 15 * 4; 
			int y = 20; 
			for( int row = 0; row < m_aBrick.length; row ++ ){ 
				x = getWidth()/2 - 15 * 4; 
				for( int col = 0; col < m_aBrick[0].length; col ++ ){ 
					m_aBrick[row][col] = new BrickSprite(img,15,5); 
					m_aBrick[row][col].setPosition(x, y); 
					x = x + 15; 
				} 
				y = y + 5; 
			}	 
			//创建木棒 
			Image imgBar = Image.createImage("/demo/bar.png"); 
			m_Bar = new BarSprite(imgBar,41,7); 
			//创建小球 
			Image imgBall = Image.createImage("/demo/ball.png"); 
			m_Ball = new BallSprite(imgBall,5,5); 
		} 
		catch (Exception ex){} 
		setState(GAME_UI); 
		Reset();								//设置砖块 
		Start();								//启动线程 
	} 
	private void setState(int state){ 
		if(state < 0 || state >=GAME_STATE_NUM) 
			return; 
		m_nState = state; 
	} 
	private void Reset(){ 
		//随机设置所有砖块的种类 
		for( int row = 0; row < m_aBrick.length; row ++ ){ 
			for( int col = 0; col < m_aBrick[0].length; col ++ ){ 
				m_aBrick[row][col].setState(BrickSprite.BRICK_FIX); 
				int m = Math.abs(m_Random.nextInt() % 3); 
				m_aBrick[row][col].setFrame(m);				 
			} 
		}	 
		ResetBarBall();							//设置小球和木棒的初始位置 
	} 
	private void ResetBarBall(){				//设置小球和木棒的初始位置 
		m_nBallNum --; 
		if( m_nBallNum <= 0 ){ 
			setState(GAME_END); 
			return; 
		} 
		m_Bar.setLong(false);					//设置木棒的状态 
		m_Bar.setRefPixelPosition(getWidth()/2, getHeight() - 30); 
		m_Ball.setSpeed(-3, -3);				//设置小球速度 
		int y = m_Bar.getRefPixelY()  
			- m_Bar.getHeight()/2 - m_Ball.getWidth()/2 - 2; 
		m_Ball.setRefPixelPosition(m_Bar.getRefPixelX() - 5, y); 
		m_Ball.setVisible(true); 
	} 
	public void Start(){ 
		m_bRunning = true; 
		Thread thread = new Thread(this);		//分配新线程 
		thread.start();							//线程启动 
	} 
	public void run() {						//新线程自动调用此方法 
		//获取系统当前时间,并将时间换算成以毫秒为单位的数 
		long T1 = System.currentTimeMillis(); 
		long T2 = T1; 
		while(m_bRunning){ 
			T2 = System.currentTimeMillis(); 
			if( T2 - T1 > 50 ){			    	//间隔50毫秒 
				T1 = T2; 
				Input(); 
				Logic(); 
				Paint(); 
			} 
		} 
	} 
	public void Stop(){						//终止游戏 
		m_bRunning = false; 
	} 
	public void Input(){ 
		int keyStates = getKeyStates(); 
		switch( m_nState ){ 
		case GAME_UI:							//进入用户界面 
			m_UI.Input( keyStates ); 
			if( m_UI.getState() == MyUI.UI_HIDE ) 
				setState(GAME_GAMING); 
			break;			 
		case GAME_GAMING:						//进入游戏 
			m_Bar.Input(keyStates, getWidth()); 
			break; 
		} 
	} 
	public void Logic(){ 
		switch( m_nState ){ 
		case GAME_UI:							//进入用户界面 
			break;			 
		case GAME_GAMING:						//进入游戏 
			m_Bar.Logic(); 
			m_Ball.Logic(getWidth(), getHeight()); 
			boolean bReset = true; 
			for( int row = 0; row < m_aBrick.length; row ++ ){ 
				for( int col = 0; col < m_aBrick[0].length; col ++ ){ 
					m_aBrick[row][col].Logic(getHeight()); 
					if( m_aBrick[row][col].getState() != BrickSprite.BRICK_HIDE ) 
						bReset = false; 
				} 
			} 
			if( bReset ){ 
				//如果所有砖块都消失,则重新开始 
				Reset(); 
				return; 
			} 
			Collides();							//碰撞检测 
			if( m_Ball.isVisible() == false )	//如果小球消失,则重新再来 
				ResetBarBall(); 
		} 
	} 
	public void Collides(){					//碰撞检测 
		//如果小球和木棒发生碰撞 
		if( m_Ball.collidesWith( m_Bar, false ) ) 
			m_Ball.Reflect(false); 
		 
		for( int row = 0; row < m_aBrick.length; row ++ ){ 
			for( int col = 0; col < m_aBrick[0].length; col ++ ){ 
				if( m_aBrick[row][col].getState() == BrickSprite.BRICK_FIX ){ 
					if( m_aBrick[row][col].collidesWith(m_Ball,false) ) 
					{//检测小球与砖块的碰撞 
						int bx = m_Ball.getRefPixelX(); 
						int x1 = m_aBrick[row][col].getX(); 
						int x2 = m_aBrick[row][col].getX() +  
									m_aBrick[row][col].getWidth(); 
						if( bx > x1 && bx < x2 ) 
							m_Ball.Reflect(false); 
						else 
							m_Ball.Reflect(true); 
						//随机设置砖块下落 
						int r = m_Random.nextInt() % 3; 
						if( r == 0 ) 
							m_aBrick[row][col].setState(BrickSprite.BRICK_DROP); 
						else 
							m_aBrick[row][col].setState(BrickSprite.BRICK_HIDE); 
						int n = m_Record.getCurRecord(); 
						n += 5; 
						m_Record.setCurRecord(n); 
					} 
				} 
				else if( m_aBrick[row][col].getState() == BrickSprite.BRICK_DROP ){ 
					if( m_aBrick[row][col].collidesWith(m_Bar,false) ) 
					{//检测下落的砖块与木棒的碰撞 
						switch( m_aBrick[row][col].getFrame() ) 
						{//不同的砖块,不同功能 
						case 0: 
							m_Bar.setLong(true); 
							break; 
						case 1: 
							m_Ball.AddSpeed(2, 2); 
							break; 
						case 2: 
							m_Ball.AddSpeed(-2, -2); 
							break; 
						} 
						m_aBrick[row][col].setState(BrickSprite.BRICK_HIDE); 
					} 
				} 
			} 
		}	 
	} 
	public void Paint(){ 
		Graphics g = getGraphics(); 
		//用黑色清屏 
		g.setColor(0); 
		g.fillRect( 0, 0, getWidth(), getHeight() ); 
		 
		g.setColor(0xFFFFFFFF); 
		switch( m_nState ){ 
		case GAME_UI:					//显示界面 
			m_UI.Paint(g, getWidth(), getHeight()); 
			break;			 
		case GAME_GAMING:				//显示游戏画面 
			m_Back.Paint(g, getWidth(), getHeight()); 
			for( int row = 0; row < m_aBrick.length; row ++ ){ 
				for( int col = 0; col < m_aBrick[0].length; col ++ ){ 
					m_aBrick[row][col].paint(g); 
				} 
			}		 
			m_Ball.paint(g); 
			m_Bar.paint(g); 
			StringBuffer buffer = new StringBuffer(); 
			buffer.append("Score:"); 
			buffer.append(m_Record.getCurRecord()); 
			g.drawString(buffer.toString(), 10, 10, 0 ); 
			break; 
		case GAME_END:					//显示结束语 
			m_Back.Paint(g, getWidth(), getHeight()); 
			m_Record.Paint( g, getWidth(), getHeight() ); 
			break; 
		} 
		flushGraphics(); 
	} 
}