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

📄 gmcanvas.java

📁 蓝牙技术 本游戏来源于网络
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

		// This ends when animationThread is set to null, or when
		// it is subsequently set to a new thread; either way, the
		// current thread should terminate
		long startTime;
		while (currentThread == animationThread)
		{
			startTime = System.currentTimeMillis();
			btIn = false;


			// -------------------------------------------------------------------------------------------
			// Bluetooth stuff
			// -------------------------------------------------------------------------------------------
			if (bMultiplayer && !forceMenu)
			{
				// Recieve some data (if there's any to recieve)
				int i = disc.retrieve();
				btTick = (i & 0x0000ffff);

				if ((tickCount & 0x0000ffff) == (i & 0x0000ffff))
				{
					leftPressed[1]  = ((i&0x00010000) == 0x00010000);
					rightPressed[1] = ((i&0x00020000) == 0x00020000);
					upPressed[1] 	= ((i&0x00040000) == 0x00040000);
					downPressed[1]  = ((i&0x00080000) == 0x00080000);
					firePressed[1] 	= ((i&0x00100000) == 0x00100000);
					MILLIS_PER_TICK = 44;
					updateTicks = true;
					btMissCount = 0;
					btResent = 0;
				}
				else
				{
					updateTicks = false;
					btMissCount++;
					if (btMissCount > 10)
					{
						sendBlueTooth();
						btMissCount = 0;
						btResent++;
					}
					if (btResent > BT_DELAY)
					{
						MILLIS_PER_TICK = 44;
						updateDraw = true;
					}
					else
					{
						MILLIS_PER_TICK = 5;
						updateDraw = false;
					}
				}
			}

			// -------------------------------------------------------------------------------------------
			// Don't advance game or draw if canvas is covered by
			// a System screen.
			// -------------------------------------------------------------------------------------------
			if (isShown())
			{
				if (updateTicks) tick();
				else btAbort();

				if (animationThread != null)
				{
//#ifdef midp2
					if (updateDraw || updateTicks) draw();
					flushGraphics();
//#else
//					repaint(0, 0, VIS_WIDTH, VIS_HEIGHT);
//					serviceRepaints();
//#endif
				}
			}
			else if (gameMode == GM_FIRST || gameMode == GM_SPLASH) tick();

			if (quit) midlet.menuListQuit();

			endTime = System.currentTimeMillis();


			// -------------------------------------------------------------------------------------------
			// Frame count calculation stuff
			// -------------------------------------------------------------------------------------------
			if (endTime-fpsStart > 1000)
			{
				fpsStart = System.currentTimeMillis();
				fps = fpsCount;
				fpsCount = 0;
			}
			else
			{
				fpsCount++;
			}


			try
			{
				if (endTime-startTime < (MILLIS_PER_TICK+1))
				{
					synchronized (this)
					{
						wait((MILLIS_PER_TICK+1) - (endTime-startTime));
					}
//						Thread.sleep((MILLIS_PER_TICK+1) - (endTime-startTime));
					timing = (MILLIS_PER_TICK+1) - (endTime-startTime);
				}
				else
				{
					synchronized (this)
					{
						wait(2);
					}
					timing = 0;
				}
			}
			catch (InterruptedException ex)
			{
				// won't be thrown
//System.out.println("Shoite:- " +ex.getMessage());
			}
		}
    }



	// -------------------------------------------------------------------------------------------
	// The guts of it:- Performs actions based on current game state held in gameMode
	// -------------------------------------------------------------------------------------------
    void tick()
    {
		if (!isPaused && !bBTSetup)
		{
			tickCount++;

			switch (gameMode)
			{

				// -------------------------------------------------------------------------------------------
				// Load a title screen to be displayed while running GM_FIRST
				// -------------------------------------------------------------------------------------------
				case GM_SPLASH:	// Do any main game initialization here
			    	imageTitle   = GameMIDlet.createImage("/t.png");
			    	VIS_WIDTH = getWidth();
			    	VIS_HEIGHT = getHeight();
			    	gameMode = GM_FIRST;
					break;

				// -------------------------------------------------------------------------------------------
				// Do any main game initialization here
				// -------------------------------------------------------------------------------------------
				case GM_FIRST:	// Do any main game initialization here

					// Deal with sound first
//#ifndef nokiaSound
					try
					{
						Class.forName("javax.microedition.media.Player");
						ge = new GameEffects();
						noSound = false;
					}
					catch (ClassNotFoundException ignore2)
					{
						noSound = true;
					}
//#else
//						ge = new GameEffects();
//						noSound = false;
//#endif
					if (ge != null) GameEffects.soundOn = true;

					// Initialize screen display stuff
					VIS_WIDTH = getWidth();
					VIS_HEIGHT = getHeight();

					// Load the graphics
					init();


					// Init any other variable here


					// Setup settings
					// 1. The menu items available
					for (int i = 0; i < MAX_OPTIONS[0]; i++)
					{
						options[i] = true;
					}

					Standard.setupHiscore(18+6, (VIS_HEIGHT-fontHeight), fontHeight);
			        Standard.getSettings(this);
					Standard.setSoftKey(this);
//#ifdef useBT
					if (options[0]) option = 0;
					try
					{
						Class.forName("javax.bluetooth.BluetoothStateException");
					}
					catch (ClassNotFoundException ignore2)
					{
						options[2] = false;
					}
//#else
//					options[2] = false;
//#endif

					// Finally set new gameMode and reset the tick counter
					gameMode = GM_TITLE;
					tickCount = 0;
					break;


				// -------------------------------------------------------------------------------------------
				// Loads the title.png and set menus to start + any other stuff that may be
				// nulled during game but needed for title. Also validate payment if required.
				// -------------------------------------------------------------------------------------------
				case GM_TITLE:		// Not too sure about this game mode:- currently loads the title.png if need be
//						imageTitle = null;
//						imageTitle   = GameMIDlet.createImage("/title.png");
						optionPage = 0;

						// Setup text labels for menus if not already done
						if (Standard.imageText[0] == null)
						{
							for (int i = 0; i < 18; i++)
							{
								StringBuffer b = new StringBuffer(dict.getString(Dictionary.MENU_0+i));
								Standard.createImageText(i,GAME_FONT,b);
							}
						}


						// Reset any other variables here


						gameMode = GM_TITLE2;
					break;


				// -------------------------------------------------------------------------------------------
				// Start the music if need be and check menu
				// -------------------------------------------------------------------------------------------
				case GM_TITLE2:
//#ifdef useMusic
					if (ge != null && tickCount == 1) ge.playMenu();
//#endif
					// Check the menu to update gameMode
					if (tickCount>1) updateMenu((leftPressed[0] && !leftHeld[0]),(rightPressed[0] && !rightHeld[0]),(firePressed[0] && !fireHeld[0]));
					break;


				// -------------------------------------------------------------------------------------------
				// Init for the start of a new game
				// -------------------------------------------------------------------------------------------
				case GM_INIT:
					// Stop menu music
					if (ge != null) ge.stopSound();


					// Zero scores etc

					// Set new gameMode to init a new level
					gameMode = GM_INIT_LEVEL;
					options[0] = true;
					tickCount = 0;
					break;

				case GM_INIT_LEVEL:
					/* An example of what we could do here
					if (tickCount == 1)
					{
						// Init level variables
					}
					if (tickCount > 1 && tickCount < A_Pause_animation_time_for_the_init_level)
					{
						// Do some animation
					}

					if (tickCount >= A_Pause_animation_time_for_the_init_level)
					{
						gameMode = GM_PLAY;
					}
					*/

					// For now though we'll just start the game
					gameMode = GM_PLAY;
					break;

				case GM_PLAY:
					// Process game loop here
					break;

				case GM_LOOSE_LIFE:
					break;

				case GM_LEVEL_COMP:
//					gameLevel++;
					gameMode = GM_INIT_LEVEL;
					break;

				case GM_GAME_OVER:
					{
						gameMode = GM_TITLE;
						options[0] = false;
						StringBuffer buf = new StringBuffer(dict.getString(Dictionary.INT_CONN+Standard.hsSubmit));
						Standard.wrapperLines(GAME_FONT, fontHeight, buf, DISPLAY_WIDTH-24, (DISPLAY_HEIGHT>>1)-8);
						tickCount = 0;
					}
					break;
				case GM_CONNECTION_LOST:
					break;
				case GM_GAME_COMPLETE:
					if (tickCount == 1)
					{
						StringBuffer b = new StringBuffer(dict.getString(Dictionary.COMP2));
						Standard.wrapperLines(GAME_FONT,fontHeight,b,DISPLAY_WIDTH-20,((fontHeight*4)+8)-4);
						if (bMultiplayer)
						{
							if (disc != null) disc.close();
							bMultiplayer = false;
						}
					}
					else if ((tickCount>>1) > ((Standard.totLines+14)*fontHeight))
					{
						tickCount = 0;
						gameMode = GM_GAME_OVER;
					}
					break;

				case GM_EXIT:
					quit = true;
					break;
			}
			getKeys(true);
		}
		else
		{
			if (bBTSetup)
			{
//				//System.out.println("In bBTSetup");
				switch (btStatus)
				{
					case BT_SETUP_HOST:		// Host a game
						if (!bHost && BlueTooth.isConnected() && disc != null) disc.close();
						try
						{
							if (disc == null) disc = new BlueTooth();
							//System.out.println(BlueTooth.ErrorMessage);
							disc.hostGame();
							btStatus = BT_AWAIT_CLIENT;
							StringBuffer buf = new StringBuffer(dict.getString(Dictionary.BT_WAIT_JOIN));
							Standard.wrapperLines(GAME_FONT, fontHeight, buf, DISPLAY_WIDTH-24, (DISPLAY_HEIGHT>>1)-8);
							bHost = true;
						}
						catch (Exception e)
						{
							//System.out.println(e.getMessage());
						}
						break;
					case BT_AWAIT_CLIENT:		// Wait for connection from client
						tickCount++;
						if (BlueTooth.gameFound)
						{
				        	BlueTooth.send(0x11110000);
				        	disc.startReceive();
				        	btStatus = BT_AWAIT_RECEIVE;
						}
						else if (BlueTooth.ConnectCancelled)
						{
							bBTSetup = false;
							btStatus = 0;
						}
						break;
					case BT_SETUP_JOIN:		// Join a game
						if (bHost && BlueTooth.isConnected() && disc != null) disc.close();
						try
						{
							if (disc == null) disc = new BlueTooth();
							//System.out.println(BlueTooth.ErrorMessage);
							disc.joinGame();
							btStatus = BT_AWAIT_HOST;
							StringBuffer buf = new StringBuffer(dict.getString(Dictionary.BT_SEARCH_HOST));
							Standard.wrapperLines(GAME_FONT, fontHeight, buf, DISPLAY_WIDTH-24, (DISPLAY_HEIGHT>>1)-8);
							bHost = false;
////System.out.println("Join done!");
						}
						catch (Exception e)

⌨️ 快捷键说明

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