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

📄 racex.cpp

📁 Visual C++ 游戏开发与设计实例 源代码(所有)
💻 CPP
📖 第 1 页 / 共 5 页
字号:
		if(m_pMultiPlayer.IsHosting())
		{
			m_txVerdana.WriteText(m_pBackBuffer, "PRESS ENTER TO START THE RACE", 140, 420);
		}
		else
		{
			m_txVerdana.WriteText(m_pBackBuffer, "WAITING START MESSAGE FROM GAME CREATOR", 40, 420);
		}
		
		// Process the keyboard for input
		m_Keyboard.Process();

		if(GetTickCount() - lLastOption > 200)
		{
			if(m_pMultiPlayer.IsHosting())
			{
				if(m_Keyboard.CheckKey(DIK_RETURN) || 
					m_Keyboard.CheckKey(DIK_NUMPADENTER) )
				{
					
					for(i=0;i<4;i++)
					{
						m_surfBigCars[i].Destroy();
					}

					m_sptrCar.Destroy();
					
					if(m_pCompetition.GetCompetitionMode() == TRUE)
					{
						if(m_pCompetition.AllPlayersReady())
						{						
							// Multiplayer Player - Competition
							// Go to the Competition Status Screen
							m_pCompetition.ResetReadyState();
							m_iState = GS_COMPETITIONSTATUS;
							iStart = 0;
							m_sndSelect.Play();
							lLastOption = GetTickCount();
							m_pMultiPlayer.SendTo(0, MSG_COMPETITIONSTATUS, NULL, 0, DPNSEND_SYNC | DPNSEND_NOLOOPBACK | DPNSEND_GUARANTEED);
						}
						else
						{
							// Players are not ready (some are without name and car color)
							// Go to the wait screen
							m_iState = GS_WAIT;
							iStart = 0;
							lLastOption = GetTickCount();
						}
					}
					else
					{
						if(m_pCompetition.AllPlayersReady())
						{
							// All the players are ready !
							// LET THE RACE BEGIN !
							strcpy(buffer, pRaceTrack.GetFileName());
							i = strlen(buffer);

							// Send a STARTRACE message to all the peers
							m_pMultiPlayer.SendTo(0, MSG_START_RACE, (unsigned char *) &buffer[0], i, DPNSEND_SYNC | DPNSEND_NOLOOPBACK | DPNSEND_GUARANTEED);
							m_iState = GS_RACE;
							iStart = 0;
							lLastOption = GetTickCount();
						}
						else
						{
							// Players are not ready (some are without name and car color)
							// Go to the wait screen
							m_iState = GS_WAIT;
							iStart = 0;
							lLastOption = GetTickCount();
						}
					}
					break;
				}
			}
		}
		

		break;
	case GS_CREATEGAME:
		// This is used in the multiplayer game
		// When a user is creating a new gaming session, show the
		// new game panel so that he can enter the game name
		if(iStart == 0)
		{
			m_iOption = 1;

			m_surfTitle.Create(370,160);
			m_surfTitle.LoadBitmap(GetInstHandle(), IDB_BACKGROUND3);

			m_surfCaret.Create(2,15);
			m_surfCaret.LoadBitmap(GetInstHandle(), IDB_CARET);

			m_surfCursor.Create(32, 32, RGB(0,0,0));
			m_surfCursor.LoadBitmap(GetInstHandle(), IDB_CURSOR);

			m_sndSelect.Create(MAKEINTRESOURCE(IDR_SELECT), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);
			m_sndChangeOption.Create(MAKEINTRESOURCE(IDR_CHANGEOPTION), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);

			m_sndType.Create(MAKEINTRESOURCE(IDR_TYPE), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);			

			m_sGameName = "";
			iStart = 1;
		}


		iCaretPos = 172;
		
		if(GetTickCount() - lLastCaret > 100)
		{
			bShowCaret = !bShowCaret;
			lLastCaret = GetTickCount();
		}

		m_surfTitle.Draw(m_pBackBuffer, 135, 140);

		if(m_sGameName.length() > 0)
		{
			iCaretPos = m_txDigital.WriteText(m_pBackBuffer, &m_sGameName[0], 170, 220);
		}

		if(bShowCaret)
		{
			m_surfCaret.Draw(m_pBackBuffer, iCaretPos+2, 221);
		}

		m_Keyboard.Process();
		m_Mouse.Process();

		m_surfCursor.Draw(m_pBackBuffer, m_Mouse.GetX(), m_Mouse.GetY(), 0, 0, m_Mouse.GetX() > 640-32 ? 640-m_Mouse.GetX() : 0, m_Mouse.GetY() > 480-32 ? 480-m_Mouse.GetY() : 0 );

		CheckInput(&m_sGameName, 20);

		if(GetTickCount() - lLastOption > 200)
		{
			if(m_Keyboard.CheckKey(DIK_ESCAPE))
			{
				m_iState = GS_MAINSCREEN;
				iStart = 0;
				m_surfHelmet.Destroy();
				pRaceTrack.Destroy();
				m_sptrCar.Destroy();
				lLastOption = GetTickCount();
			}


			if(m_Keyboard.CheckKey(DIK_RETURN) ||
			   m_Keyboard.CheckKey(DIK_NUMPADENTER) )
			{
				lLastOption = GetTickCount();
				if(m_sGameName.size() == 0)
				{
					m_sGameName = "NEW GAME";
				}
				else
				{
					m_pMultiPlayer.Host(&m_sGameName[0]);
					m_iState = GS_SELECTTRACK_AND_CAR;
					iStart = 0;
					m_sndSelect.Play();
				}
			}

		}
		break;
	case GS_JOINGAME:
		// The user is trying to join a multiplayer game
		if(iStart == 0)
		{
			m_iOption = 1;

			m_surfTitle.Create(450,300);
			m_surfTitle.LoadBitmap(GetInstHandle(), IDB_BACKGROUND4);

			m_surfCursor.Create(32, 32, RGB(0,0,0));
			m_surfCursor.LoadBitmap(GetInstHandle(), IDB_CURSOR);

			m_sndSelect.Create(MAKEINTRESOURCE(IDR_SELECT), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);
			m_sndChangeOption.Create(MAKEINTRESOURCE(IDR_CHANGEOPTION), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);

			m_sndType.Create(MAKEINTRESOURCE(IDR_TYPE), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);			
			
			m_iOption = -1;
			
			iStart = 1;

			lLastSprite = 70;
			lLastCaret = 3;
		}
		else
		{
			if(iStart == 1)
			{
				iStart = 2;
				m_pMultiPlayer.EnumSessions();
			}
		}

		if(iStart == 1)
		{
			// We磖e still searching for sessions...
			// Wait ultil we have finished
			m_txVerdana.WriteText(m_pBackBuffer, "SEARCHING FOR RACEX SESSIONS", 150, 150);
			m_txVerdana.WriteText(m_pBackBuffer, "PLEASE WAIT...", 250, 200);
			m_iOption = 0;
		}
		else
		{
			// Show the avaible games to the user
			m_surfTitle.Draw(m_pBackBuffer, 95, 90);

			if(m_pMultiPlayer.GetNumSessions() == 0)
			{
				// No games wore found in the network
				m_txVerdana.WriteText(m_pBackBuffer, "NO NETWORK GAMES FOUND", 165, 190);
				m_txVerdana.WriteText(m_pBackBuffer, "USE THE SEARCH BUTTON", 175, 230);
				m_txVerdana.WriteText(m_pBackBuffer, "TO TRY AGAIN.", 230, 250);
				
			}
			else
			{
				// Show all the games
				HDC hdc;
				m_pBackBuffer->GetDC(&hdc);
				SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
				HPEN hPen = CreatePen(PS_SOLID,1,RGB(0,(int) lLastSprite,0));
				lLastSprite+=lLastCaret;
				if(lLastSprite > 170 || lLastSprite < 70)
				{
					lLastCaret = lLastCaret * -1;
					lLastSprite+=lLastCaret;
				}

				
				HPEN hOldPen = (HPEN) SelectObject(hdc, hPen);
				Rectangle(hdc, 120, 163 + (m_iOption * 24), 520, 182 + (m_iOption * 24));
				SelectObject(hdc, hOldPen);
				DeleteObject(hPen);
				m_pBackBuffer->ReleaseDC(hdc);
				
				for(i=0;i< (m_pMultiPlayer.GetNumSessions() < 7 ? m_pMultiPlayer.GetNumSessions() : 7);i++)
				{
					m_pMultiPlayer.GetSessionName(i, buffer, 255);
					m_txDigital.WriteText(m_pBackBuffer, buffer, 122, 165 + (i * 24));
				}
			}
		}

		if(iStart > 1)
			m_surfCursor.Draw(m_pBackBuffer, m_Mouse.GetX(), m_Mouse.GetY(), 0, 0, m_Mouse.GetX() > 640-32 ? 640-m_Mouse.GetX() : 0, m_Mouse.GetY() > 480-32 ? 480-m_Mouse.GetY() : 0 );


		// Process the keyboard and mouse for input
		m_Keyboard.Process();
		m_Mouse.Process();

		if(GetTickCount() - lLastOption > 200)
		{

			int	iX, iY;

			iX = m_Mouse.GetX();
			iY = m_Mouse.GetY();
			
			// Check if the user hit the Search Button
			if( iX > 233+95 && iY > 251+90 &&
				iX < 357+95 && iY < 277+90 &&
				m_Mouse.GetLeftButton() == FALSE)
			{
				iStart = 0;
			}

			// Check if the user hit the Join Button
			if( iX > 87+95 && iY > 251+90 &&
				iX < 211+95 && iY < 277+90 &&
				m_Mouse.GetLeftButton() == FALSE)
			{
				m_iState = GS_SELECTTRACK_AND_CAR;
				iStart = 0;
				m_sndSelect.Play();
				lLastOption = GetTickCount();
				m_pMultiPlayer.Connect(m_iOption);
			}


			if( iX > 120 && iY > 163  &&
				iX < 520 && iY < 327 &&
				m_Mouse.GetLeftButton() == FALSE)
			{
				if( (iY - 163) / 24 < m_pMultiPlayer.GetNumSessions() )
					m_iOption = (iY - 163) / 24;
			}

			if(m_Keyboard.CheckKey(DIK_RETURN) ||
			   m_Keyboard.CheckKey(DIK_NUMPADENTER) )
			{
				m_sndSelect.Play();
				m_surfHelmet.Destroy();
				m_sptrCar.Destroy();
			}

			if(m_Keyboard.CheckKey(DIK_ESCAPE))
			{
				m_iState = GS_MAINSCREEN;
				iStart = 0;
				m_surfHelmet.Destroy();
				pRaceTrack.Destroy();
				m_sptrCar.Destroy();
				lLastOption = GetTickCount();
			}

		}


		break;
	case GS_MAINSCREEN:

		// The iStart is set to 0 when we are starting the program 
		// or we are coming from another screen, so that we can
		// initialize the objects necessary to create the screen
		if(iStart == 0)
		{
			m_iOption = 1;
			m_txVerdana.Create(2);
			m_txDigitalSmall.Create(3);

			m_surfTitle.Create(450,260);
			m_surfTitle.LoadBitmap(GetInstHandle(), IDB_TITLE);

			m_surfHelmet.Create(28,26);
			m_surfHelmet.LoadBitmap(GetInstHandle(), IDB_HELMET);

			m_sndSelect.Create(MAKEINTRESOURCE(IDR_SELECT), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);
			m_sndChangeOption.Create(MAKEINTRESOURCE(IDR_CHANGEOPTION), DSBCAPS_CTRL3D, DS3DALG_NO_VIRTUALIZATION);

			m_pCompetition.Reset();

			iStart = 1;
		}

		// Draw the Title in the middle of the screen
		m_surfTitle.Draw(m_pBackBuffer, 104, 20);
		j = 280;
		// If we are in the menu (option less then 6), draw the top-level menu
		if(m_iOption <= 6)
		{
			m_txVerdana.WriteText(m_pBackBuffer, "SINGLE PLAYER GAME", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "MULTIPLAYER GAME", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "HELP", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "OPTIONS", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "CREDITS", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "QUIT", 200, j);
		}
		// option is greater than 6, states the we are in the submenu
		else if (m_iOption > 10 && m_iOption < 21)
		{
			m_txVerdana.WriteText(m_pBackBuffer, "SINGLE TRACK MODE", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "COMPETITION MODE", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "BACK", 200, j);
			j+= 26;
		}
		else if (m_iOption > 20 && m_iOption < 31)
		{
			m_txVerdana.WriteText(m_pBackBuffer, "CREATE A NEW GAME", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "JOIN A GAME", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "BACK", 200, j);
		}
		else if (m_iOption > 30 && m_iOption < 41)
		{
			m_txVerdana.WriteText(m_pBackBuffer, "SINGLE TRACK MODE", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "COMPETITION MODE", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "BACK", 200, j);
		}
		else if (m_iOption > 40 && m_iOption < 51)
		{
			m_txVerdana.WriteText(m_pBackBuffer, "MUTIPLAYER PROVIDER - ", 100, j);
			m_txVerdana.WriteText(m_pBackBuffer, m_pMultiPlayer.GetSPName(m_pMultiPlayer.GetSPIndex()), 384, j);
			j+= 26;
			i = 0;
			while(i < m_pMultiPlayer.GetNumSPOptions())
			{
				m_txVerdana.WriteText(m_pBackBuffer, m_pMultiPlayer.GetSPOptionName(i), 100, j);
				m_pMultiPlayer.GetSPOption(i, buffer, 255);
				m_txVerdana.WriteText(m_pBackBuffer, buffer, 384, j);
				i++;
				j+= 26;
			}
			//m_txVerdana.WriteText(m_pBackBuffer, "COMPETITION MODE", 200, 310);
			m_txVerdana.WriteText(m_pBackBuffer, "BACK", 100, j);
		}
		else if (m_iOption > 50 && m_iOption < 61)
		{
			m_txVerdana.WriteText(m_pBackBuffer, "MULTIPLAYER OPTIONS", 200, j);
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "DIFFICULTY LEVEL- ", 200, j);
			switch(m_iDifficultyLevel)
			{
			case 0:
				m_txVerdana.WriteText(m_pBackBuffer, "EASY", 420, j);
				break;
			case 20:
				m_txVerdana.WriteText(m_pBackBuffer, "NORMAL", 420, j);
				break;
			case 40:
				m_txVerdana.WriteText(m_pBackBuffer, "PRETTY HARD", 420, j);
				break;
			case 60:
				m_txVerdana.WriteText(m_pBackBuffer, "HARD", 420, j);
				break;
			case 80:
				m_txVerdana.WriteText(m_pBackBuffer, "HELL HARD", 420, j);
				break;
			}
			j+= 26;
			m_txVerdana.WriteText(m_pBackBuffer, "BACK", 200, j);
		}


		// Process the keyboard for input
		m_Keyboard.Process();

		// Draw the helmet icon according to the selected option
		if (m_iOption > 40 && m_iOption < 51)
			m_surfHelmet.Draw(m_pBackBuffer, 63, 277 + ( ( (m_iOption%10) -1) * 26));
		else
			m_surfHelmet.Draw(m_pBackBuffer, 163, 277 + ( ( (m_iOption%10) -1) * 26));

		if(GetTickCount() - lLastOption > 200)
		{
			// If user hit the down key, change the option
			if(m_Keyboard.CheckKey(DIK_DOWN))
			{
				m_iOption++;
				if(m_iOption > 6 && m_iOption < 11) m_iOption = 1;
				if(m_iOption > 13 && m_iOption < 21) m_iOption = 11;
				if(m_iOption > 23 && m_iOption < 31) m_iOption = 21;
				if(m_iOption > 33 && m_iOption < 41) m_iOption = 31;
				if(m_iOption > 42 + m_pMultiPlayer.GetNumSPOptions() && m_iOption < 51) m_iOption = 41;
				if(m_iOption > 53 && m_iOption < 61) m_iOption = 51;
				lLastOption = GetTickCount();
				m_sndChangeOption.Play();
			}

			// If user hit the up key, change the option
			if(m_Keyboard.CheckKey(DIK_UP))
			{
				m_iOption--;
				if(m_iOption < 1) m_iOption = 6;
				if(m_iOption == 10) m_iOption = 13;
				if(m_iOption == 20) m_iOption = 23;
				if(m_iOption == 30) m_iOption = 33;

⌨️ 快捷键说明

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