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

📄 cgamemain.java

📁 一个3D的保龄球的源代码
💻 JAVA
字号:
/**
 * <p>Project Name: Bowling 3D</p>
 * <p>Description: This class has the state switch of main flow of this software.
 * It also includes game menu render, key mapping, game data load and release,
 * public resource manager.</p>
 * <p>Platform: Sony Ericsson K700i</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Gameloft Shanghai</p>
 * @author Tao Qing
 */

import javax.microedition.lcdui.*;

class CGameMain extends Canvas implements Runnable, CommandListener {

	/////////////////////////////////////////////////////////////////////////////
	///////////////////////////////    KEY    ///////////////////////////////////
	/////////////////////////////////////////////////////////////////////////////

	// standard phone keyCode
	public static final int STD_UP_ARROW = -1;
	public static final int STD_DOWN_ARROW = -2;
	public static final int STD_LEFT_ARROW = -3;
	public static final int STD_RIGTH_ARROW = -4;

	//public static final int STD_LEFT_SOFT =-20;
	//public static final int STD_RIGHT_SOFT =-21;
	//public static final int STD_M = -22 ;

	///////////////////////////////////////////
	// game keyCode
	public static final int gk_UP = 1;
	public static final int gk_UPRIGHT = 1 << 1;
	public static final int gk_RIGHT = 1 << 2;
	public static final int gk_DOWNRIGHT = 1 << 3;
	public static final int gk_DOWN = 1 << 4;
	public static final int gk_DOWNLEFT = 1 << 5;
	public static final int gk_LEFT = 1 << 6;
	public static final int gk_UPLEFT = 1 << 7;

	public static final int gk_A = 1 << 8;
	public static final int gk_B = 1 << 9;
	public static final int gk_C = 1 << 10;
	public static final int gk_D = 1 << 11;

	public static final int gk_LEFT_SOFT = 1 << 16;
	public static final int gk_RIGHT_SOFT = 1 << 17;

	private long m_timeEnd;

	private long m_timeStart = System.currentTimeMillis();

	public static int getKey(int keyCode) {

		switch (keyCode) {
		case STD_UP_ARROW:
		case KEY_NUM2:
			return gk_UP;

		case STD_DOWN_ARROW:
		case KEY_NUM8:
			return gk_DOWN;

		case STD_LEFT_ARROW:
		case KEY_NUM4:
			return gk_LEFT;

		case STD_RIGTH_ARROW:
		case KEY_NUM6:
			return gk_RIGHT;
		/////////////////////////////////////
		case def.KEY_LEFT_MENU:
			return gk_LEFT_SOFT;

		case def.KEY_RIGHT_MENU:
			return gk_RIGHT_SOFT;
		////////////////////////////////////////////////

		case KEY_NUM3:
			return gk_UPRIGHT;

		case KEY_NUM9:
			return gk_DOWNRIGHT;

		case KEY_NUM7:
			return gk_DOWNLEFT;

		case KEY_NUM1:
			return gk_UPLEFT;

		/////////////////////////////////////////
		case def.KEY_MENU:
		case KEY_NUM5:
			return gk_A;

		case KEY_NUM0:
			return gk_C;

		case KEY_POUND:
			return gk_B;

		case KEY_STAR:
			return gk_D;

		default:
			return 0;
		} // end switch
	} // end getKey

	////////////////////////////////////////////////////

	int m_runState;

	boolean m_isPainting = false;
	CGamePlay m_gamePlay = new CGamePlay();

	public int m_iKeyCode = def.KEYNOTPRESSED; //current key value
	public int m_iKey = def.KEY_EMPTY; //temp variable
	public int m_iLastKey = def.KEY_EMPTY; //last key value
	public CMIDlet m_parent;

	/**
	 * <B>constructers</B></BR>
	 * construct an GameMain
	 */
	public CGameMain(CMIDlet parent) {
		//def.staRound = def.ROUND_INIT;
		m_gamePlay.setRoundState(m_gamePlay.ROUND_INIT);
		//m_gamePlay.staRound = m_gamePlay.ROUND_INIT;

		addCommand(new Command("Exit", Command.EXIT, 1));
		setCommandListener(this);

		m_parent = parent;
		setFullScreenMode(true);
	}

	/**
	 * all game paint
	 * @param g Graphics
	 */
	protected void paint(Graphics g) {
		m_isPainting = true;
		//painting
		/* added by Milo */
		//g.drawString("Press * to start play",30,30,Graphics.LEFT | Graphics.TOP);
		//InputTick(iKeyCode);
		//g.setColor(255, 255, 255);
		//g.fillRect(0, 0, def.DISPLAY_WIDTH, def.DISPLAY_HEIGHT + 15);
		//g.setColor(0);
		//m_timeEnd = System.currentTimeMillis();

		updateRunState(g);

		/* end */

		//updateRunState(g);
		showRate(g);
		//drawMsg(g);
		m_isPainting = false;
	}

	long m_lTickCounter = 0;

	/**
	 * top loop for this programe
	 */
	public void run() {
		while (true) {
			if (!m_isPainting) {
				m_timeEnd = System.currentTimeMillis();
				if ((m_timeEnd - m_timeStart) > 75) {
					repaint();
					serviceRepaints();
					m_timeStart = m_timeEnd;
				}
				if ((m_lTickCounter % 100) == 0)
					System.gc();
				m_lTickCounter++;
			}
		}
	}

	static long _tick, _timeEnd, _fps, _timeStart;

	/**
	 * DEBUG: show out the frame rate
	 * @param g Graphics
	 */
	static void showRate(Graphics g) {
		_tick++;
		_timeEnd = System.currentTimeMillis();

		if (_timeEnd - _timeStart > 1000) {
			_fps = (_tick * 1000) / (_timeEnd - _timeStart);
			_timeStart = _timeEnd;
			_tick = 0;
		}
		g.setColor(0, 0, 255);
		//g.setClip(20, 60, 20, 15);
		g.drawString("" + _fps, 20, 60, g.LEFT | g.TOP);
	}

	static String _msg = "";

	/**
	 * DEBUG: show out the string _msg on screen
	 * @param g Graphics
	 */
	static void drawMsg(Graphics g) {
		int len = _msg.length();
		for (int i = 0; i < (len + 39) / 40; i++)
			g.drawString(_msg.substring(i * 40, (i + 1) * 40 > len ? len
					: (i + 1) * 40), 2, i * 12, 0);
	}

	/**
	 * update run state, switch them, and determinate what should be render.
	 */
	void updateRunState(Graphics g) {
		/* added by Milo */
		m_gamePlay.Update(g); //call CGamePlay::Update()
		/* end*/
	}

	/**
	 * Respond the key event
	 */
	void InputTick(int keyCode) {
		//call CGamePlay::InputTick(iKeyCode)
		m_gamePlay.InputTick(m_iKeyCode);
	}

	protected void keyRepeated(int keyCode) {
		/*key mapping start*/
		int gameKey = getKey(keyCode);
		//System.out.print(gameKey);
		//System.out.println(keyCode);
		m_iKeyCode |= gameKey;
		/*key mapping end*/
		//System.out.println(iKeyCode);
		//iKeyCode = keyCode;
		//System.out.println(iKeyCode);
		/* added by Milo */
		InputTick(m_iKeyCode);
		/*end */

	}

	protected void keyPressed(int keyCode) {
		/*key mapping start*/
		int gameKey = getKey(keyCode);
		//System.out.print(gameKey);
		//System.out.println(keyCode);
		m_iKeyCode |= gameKey;
		/*key mapping end*/
		//System.out.println(iKeyCode);
		//iKeyCode = keyCode;
		//System.out.println(iKeyCode);
		/* added by Milo */
		InputTick(m_iKeyCode);
		/*end */

	}

	protected void keyReleased(int keyCode) {
		//if (iKeyCode == keyCode) {
		//  iKeyCode = def.KEYNOTPRESSED;
		//}
		int gameKey = getKey(keyCode);
		m_iLastKey = m_iKeyCode;
		m_iKeyCode &= ~gameKey;
	}

	public void commandAction(Command c, Displayable d) {
		m_parent.notifyDestroyed();
	}

}

⌨️ 快捷键说明

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