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


package demo; 
import javax.microedition.lcdui.Graphics; 
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 MyUI m_UI;						//用户界面对象 
	private Pictures m_Pictrues;			//相似图像对象 
	private int m_nX;						//当前位置的横坐标 
	private int m_nY;						//当前位置的纵坐标 
	private int m_nTime = 1000;				//游戏时间 
	public MainCanvas(){ 
		super(true); 
		m_UI = new MyUI(); 
		m_Pictrues = new Pictures(getWidth(), getHeight()); 
		m_nX = getWidth()/2; 
		m_nY = getHeight()/2; 
		setState( GAME_UI ); 
		Start();								//启动线程 
	} 
	private void setState( int state ){		//设置当前的游戏状态 
		if( state < 0 || state >= GAME_STATE_NUM ) 
			return; 
		m_nState = state; 
	} 
	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 > 100 ){			    //间隔100毫秒 
				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:						//进入游戏 
			if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 ){ 
				m_Pictrues.CheckDiffer(m_nX, m_nY); 
				if( m_Pictrues.getFindNum() == m_Pictrues.getDiffNum() ) 
					setState(GAME_END); 
			} 
			else if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 ) 
				m_nY -= 3; 
			else if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 ) 
				m_nX += 3; 
			else if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 ) 
				m_nY += 3; 
			else if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 ) 
				m_nX -= 3; 
			break; 
		case GAME_END:							//游戏结束 
			break; 
		} 
	} 
	public void Logic(){ 
		switch( m_nState ){ 
		case GAME_UI: 
			break;			 
		case GAME_GAMING: 
			m_nTime --; 
			if( m_nTime < 0 ) 
				setState( GAME_END ); 
			break; 
		case GAME_END: 
			break; 
		} 
	} 
	public void Paint(){ 
		Graphics g = getGraphics(); 
		//用黑色清屏 
		g.setColor(0); 
		g.fillRect(0, 0, getWidth(), getHeight()); 
		 
		g.setColor(0xFFFF0000); 
		switch( m_nState ){ 
		case GAME_UI:				//显示界面 
			m_UI.Paint(g, getWidth(), getHeight()); 
			break;			 
		case GAME_GAMING:			//显示游戏画面 
			m_Pictrues.Paint(g); 
			//显示当前点的区域 
			g.setColor(0xFF0000FF); 
			g.fillRect(m_nX - 2, m_nY - 2, 4, 4); 
			break; 
		case GAME_END:				//游戏结束 
			if( m_Pictrues.getFindNum() == m_Pictrues.getDiffNum() ){ 
				g.drawString("全找对了", getWidth()/2-20, 
						getHeight()/2, 0); 
			} 
			else{ 
				g.drawString("继续努力", getWidth()/2-20, 
						getHeight()/2, 0); 
			} 
			break; 
		} 
		flushGraphics(); 
	} 
}