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

📄 game.cpp

📁 一个类似坦克大战的小小游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
							{
								m_playerShip[0].m_dwScore += 50;
							}
							break;
						}
					case 7:
						{
							if (m_playerShip[0].m_nSpeed < 4)
							{
								m_playerShip[0].m_nSpeed += 2;
								m_playerShip[0].m_nSpeedTemp += 2;
							}
							else
							{
								m_playerShip[0].m_nSpeed = 5;
							}
							break;
						}
					}
				}
			}
		}
		// Test player1 whether hit anyting
		for (itr = m_listScenery.begin(); itr != m_listScenery.end(); itr++)
		{
			if (m_playerShip[0].HitTest(*itr))
			{
				switch (itr->m_objType)
				{
				case OT_Seaweed:	// The player's ship will show down
					{
						m_playerShip[0].m_nSpeed = 1;
						m_playerShip[0].m_timeInSeaweed = timeGetTime();
						m_playerShip[0].m_bIsInSeaweed = true;
						break;
					}
				case OT_Reef:		// The player's hp will reduce a little
					{
						if ((timeGetTime() - m_playerShip[0].m_timeOnReef) > 500)
						{
							itr->m_nHp--;	
							m_playerShip[0].m_nHp--;
							m_pSound->Play(ST_Alarm);
							m_playerShip[0].m_timeOnReef = timeGetTime();
						}
						bCanMove = false;
						break;
					}
				default:
					{
						bCanMove = false;
					}
				} // End switch
			} // End if
		} // End for... end test
		if (!bCanMove)
		{
			m_playerShip[0].m_nX = ptTemp.x;
			m_playerShip[0].m_nY = ptTemp.y;
		}
	}

	bCanMove = true;

	// If player2's dead and its ship count > 0, reborn
	if (m_playerShip[1].m_nHp <= 0 && m_playerShip[1].m_bActive)
	{
		CFireworks fireworks;
		fireworks.m_objType = OT_FireworksNormal;
		fireworks.m_pTexture = m_pResource->m_textureFireworks[1].m_pTexture;
		fireworks.m_vectorPos.x = (float)m_playerShip[1].m_nX;
		fireworks.m_vectorPos.y = (float)m_playerShip[1].m_nY;
		fireworks.m_vectorPos.z = 0.0f;
		m_pSound->Play(ST_Explode);
		m_listFireworks.push_back(fireworks);
		if (m_playerShip[1].m_nShipCount > 0)
		{
			PlayerReborn(1);
		}
		else
		{
			m_pSound->Play(ST_Lost);
			m_playerShip[1].m_bActive = false;
		}
	}

	// Process player2's input //////////////////////////////////////////////
	if (m_playerShip[1].m_bActive)
	{
		ptTemp.x = m_playerShip[1].m_nX;
		ptTemp.y = m_playerShip[1].m_nY;

		if (m_playerShip[1].m_bIsInSeaweed && (timeGetTime() - m_playerShip[1].m_timeInSeaweed > 300))
		{
			m_playerShip[1].m_nSpeed = m_playerShip[1].m_nSpeedTemp;
			m_playerShip[1].m_bIsInSeaweed = false;
		}

		if (m_pInput->IsKeyDown(DIK_NUMPAD1))	// Player2 fired
		{
			m_playerShip[1].Fire(m_pResource, m_pSound);
		}

		if (m_pInput->IsKeyDown(DIK_UP))		 // Player2 move up
		{
			m_playerShip[1].m_nDirection = 0;
			m_playerShip[1].MoveUp(m_pResource);
			if (m_playerShip[1].m_nY < -30)
			{
				bCanMove = false;
			}
		}
		else if (m_pInput->IsKeyDown(DIK_RIGHT)) // Player1 turn right
		{
			m_playerShip[1].m_nDirection = 1;
			m_playerShip[1].MoveRight(m_pResource);
			if (m_playerShip[1].m_nX + m_playerShip[1].m_nWidth > 980)
			{
				bCanMove = false;
			}
		}
		else if (m_pInput->IsKeyDown(DIK_DOWN)) // Player1 move down
		{
			m_playerShip[1].m_nDirection = 2;
			m_playerShip[1].MoveDown(m_pResource);
			if (m_playerShip[1].m_nY + m_playerShip[1].m_nHeight > 560)
			{
				bCanMove = false;
			}
		}
		else if (m_pInput->IsKeyDown(DIK_LEFT)) // Player1 turn left
		{
			m_playerShip[1].m_nDirection = 3;
			m_playerShip[1].MoveLeft(m_pResource);
			if (m_playerShip[1].m_nX < -20)
			{
				bCanMove = false;
			}
		}
	
		// Eat bonus?
		int nBonusIndex;
		for (nBonusIndex = 0; nBonusIndex != MAX_BONUS; nBonusIndex++)
		{
			if (m_bonus[nBonusIndex].m_bActive)
			{
				if (m_bonus[nBonusIndex].HitTest(m_playerShip[1]))
				{
					m_bonus[nBonusIndex].m_bActive = false;
					m_pSound->Play(ST_Bonus);
					switch (m_bonus[nBonusIndex].m_nIndex)
					{
					case 0:
						{
							m_playerShip[1].m_nLevel = 2;
							m_playerShip[1].m_nBulletPower = 5;
							if (m_playerShip[1].m_nHp < 5)
							{
								m_playerShip[1].m_nHp = 5;
							}
							break;
						}
					case 1:
						{
							m_playerShip[1].m_nShipCount++;
							break;
						}
					case 2:
						{
							m_playerShip[1].m_nHp++;
							if (m_playerShip[1].m_nHp >=5)
							{
								m_playerShip[1].m_nLevel = 2;
							}
							break;
						}
					case 3:
						{
							m_playerShip[1].m_nHp += 2;
							if (m_playerShip[0].m_nHp >=5)
							{
								m_playerShip[1].m_nLevel = 2;
							}
							break;
						}
					case 4:
						{
							if (m_playerShip[1].m_nBulletPower < 5)
							{
								m_playerShip[1].m_nBulletPower++;
							}
							else
							{
								m_playerShip[1].m_dwScore += 50;
							}
							break;
						}
					case 5:
						{
							if (m_playerShip[1].m_nBulletPower < 4)
							{
								m_playerShip[1].m_nBulletPower += 2;
							}
							else
							{
								m_playerShip[1].m_nBulletPower = 5;
								if (m_playerShip[1].m_nBulletCount < MAX_BULLET_COUNT)
								{
									m_playerShip[1].m_nBulletCount++;
								}
							}
							break;
						}
					case 6:
						{
							if (m_playerShip[1].m_nSpeed < 5)
							{
								m_playerShip[1].m_nSpeed++;
								m_playerShip[1].m_nSpeedTemp++;
							}
							else
							{
								m_playerShip[1].m_dwScore += 50;
							}
							break;
						}
					case 7:
						{
							if (m_playerShip[1].m_nSpeed < 4)
							{
								m_playerShip[1].m_nSpeed += 2;
								m_playerShip[1].m_nSpeedTemp += 2;
							}
							else
							{
								m_playerShip[1].m_nSpeed = 5;
							}
							break;
						}
					}
				}
			}
		}
		// Test player2 whether hit anyting
		for (itr = m_listScenery.begin(); itr != m_listScenery.end(); itr++)
		{
			if (m_playerShip[1].HitTest(*itr))
			{
				switch (itr->m_objType)
				{
				case OT_Seaweed:	// The player's ship will show down
					{
						m_playerShip[1].m_nSpeed = 1;
						m_playerShip[1].m_timeInSeaweed = timeGetTime();
						m_playerShip[1].m_bIsInSeaweed = true;
						break;
					}
				case OT_Reef:		// The player's hp will reduce a little
					{
						if ((timeGetTime() - m_playerShip[1].m_timeOnReef) > 500)
						{
							itr->m_nHp--;	
							m_playerShip[1].m_nHp--;
							m_pSound->Play(ST_Alarm);
							m_playerShip[1].m_timeOnReef = timeGetTime();
						}
						bCanMove = false;
						break;
					}
				default:
					{
						bCanMove = false;
					}
				} // End switch
			} // End if
		} // End for... end test
		if (!bCanMove)
		{
			m_playerShip[1].m_nX = ptTemp.x;
			m_playerShip[1].m_nY = ptTemp.y;
		}
	}

}

// Process Bullet //////////////////////////////////////////////////

void CGame::ProcessBullet()
{
	list<CGameObject>::iterator itrScenery;
	list<CComputerShip>::iterator itrEnemy;

	// Process the buulets of player ship /////////////////////////
	int i, j;
	for (i = 0; i != 2; i++)
	{
		if (m_playerShip[i].m_bActive)
		{
			for (j = 0; j != m_playerShip[i].m_nBulletCount; j++)
			{
				if (m_playerShip[i].m_bullet[j].m_bActive)
				{
					switch (m_playerShip[i].m_bullet[j].m_nDirection)
					{
					case 0:		// Direction Up
						{
							m_playerShip[i].m_bullet[j].m_nY -= m_playerShip[i].m_bullet[j].m_nSpeed;
							break;
						}
					case 1:		// Direction Right
						{
							m_playerShip[i].m_bullet[j].m_nX += m_playerShip[i].m_bullet[j].m_nSpeed;
							break;
						}
					case 2:		// Direction Down
						{
							m_playerShip[i].m_bullet[j].m_nY += m_playerShip[i].m_bullet[j].m_nSpeed;
							break;
						}
					case 3:		// Direction Left
						{
							m_playerShip[i].m_bullet[j].m_nX -= m_playerShip[i].m_bullet[j].m_nSpeed;
							break;
						}
					} // End switch

					// Test whether out of screen
					if ((m_playerShip[i].m_bullet[j].m_nX < 0) || 
						(m_playerShip[i].m_bullet[j].m_nX > 970) ||
						(m_playerShip[i].m_bullet[j].m_nY > 570) ||
						(m_playerShip[i].m_bullet[j].m_nY < -20))
					{
						m_playerShip[i].m_bullet[j].m_bActive = false;
						m_playerShip[i].m_nBulletOut--;
					}

					// Now Hit test...
					for (itrScenery = m_listScenery.begin(); itrScenery != m_listScenery.end(); itrScenery++)
					{
						if (m_playerShip[i].m_bullet[j].HitTest(*itrScenery) &&
							itrScenery->m_objType != OT_Seaweed)
						{
							m_playerShip[i].m_bullet[j].m_bActive = false;
							m_playerShip[i].m_nBulletOut--;
							m_playerShip[i].m_dwScore += 5;
							// Play hit sound
							m_pSound->Play(ST_Hit);

							// Add bullet fireworks in the listFireworks
							CFireworks fireworks;
							fireworks.m_objType = OT_FireworksBullet;
							fireworks.m_pTexture = m_pResource->m_textureFireworks[0].m_pTexture;
							fireworks.m_vectorPos.x = (float)m_playerShip[i].m_bullet[j].m_nX;
							fireworks.m_vectorPos.y = (float)m_playerShip[i].m_bullet[j].m_nY;
							fireworks.m_vectorPos.z = 0.0f;
							m_listFireworks.push_back(fireworks);

							itrScenery->m_nHp -= m_playerShip[i].m_bullet[j].m_nPower;
							break;
						}
					}
					for (itrEnemy = m_listEnemy.begin(); itrEnemy != m_listEnemy.end(); itrEnemy++)
					{
						if ((m_playerShip[i].m_bullet[j].HitTest(*itrEnemy)) &&
							itrEnemy->m_nHp > 0)
						{
							m_playerShip[i].m_bullet[j].m_bActive = false;
							m_playerShip[i].m_nBulletOut--;
							m_playerShip[i].m_dwScore += itrEnemy->m_dwScore;

							// Play hit sound
							m_pSound->Play(ST_Hit);

							// Add fireworks to the list
							CFireworks fireworks;
							fireworks.m_objType = OT_FireworksBullet;
							fireworks.m_pTexture = m_pResource->m_textureFireworks[0].m_pTexture;
							fireworks.m_vectorPos.x = (float)m_playerShip[i].m_bullet[j].m_nX;
							fireworks.m_vectorPos.y = (float)m_playerShip[i].m_bullet[j].m_nY;
							fireworks.m_vectorPos.z = 0.0f;
							m_listFireworks.push_back(fireworks);

							itrEnemy->m_nHp -= m_playerShip[i].m_bullet[j].m_nPower;
							break;
						}
					}
				} // End if
			}
		}
	} // End process bullets of the player

	// Process computer ship //////////////////////////////////////
	for (itrEnemy = m_listEnemy.begin(); itrEnemy != m_listEnemy.end(); itrEnemy++)
	{
		for (i = 0; i != itrEnemy->m_nBulletCount; i++)
		{
			if (itrEnemy->m_bullet[i].m_bActive)
			{
				switch (itrEnemy->m_bullet[i].m_nDirection)
				{
				case 0:		// Direction up
					{
						itrEnemy->m_bullet[i].m_nY -= itrEnemy->m_bullet[i].m_nSpeed;
						break;
					}
				case 1:		// Direction Right
					{
						itrEnemy->m_bullet[i].m_nX += itrEnemy->m_bullet[i].m_nSpeed;
						break;
					}
				case 2:		// Direction Down
					{
						itrEnemy->m_bullet[i].m_nY += itrEnemy->m_bullet[i].m_nSpeed;
						break;
					}
				case 3:		// Direction Left
					{
						itrEnemy->m_bullet[i].m_nX -= itrEnemy->m_bullet[i].m_nSpeed;
						break;
					}
				} // End switch

				// Test whether out of screen
				if ((itrEnemy->m_bullet[i].m_nX < 0) || 
					(itrEnemy->m_bullet[i].m_nX > 970) ||
					(itrEnemy->m_bullet[i].m_nY > 570) ||
					(itrEnemy->m_bullet[i].m_nY < -10))
				{
					itrEnemy->m_bullet[i].m_bActive = false;
					itrEnemy->m_nBulletOut--;
				}

				// Hit test start..
				for (j = 0; j != 2; j++)
				{
					if (m_playerShip[j].m_bActive)
					{
						if (itrEnemy->m_bullet[i].HitTest(m_playerShip[j]))
						{
							itrEnemy->m_bullet[i].m_bActive = false;
							itrEnemy->m_nBulletOut--;

							// Play hit sound
							m_pSound->Play(ST_Hit);

							// Add fireworks to the list
							CFireworks fireworks;
							fireworks.m_objType = OT_FireworksBullet;
							fireworks.m_pTexture = m_pResource->m_textureFireworks[0].m_pTexture;
							fireworks.m_vectorPos.x = (float)itrEnemy->m_bullet[i].m_nX;
							fireworks.m_vectorPos.y = (float)itrEnemy->m_bullet[i].m_nY;
							fireworks.m_vectorPos.z = 0.0f;
							m_listFireworks.push_back(fireworks);

							m_playerShip[j].m_nHp -= itrEnemy->m_bullet[i].m_nPower;
							break;
						}
					}
				}
				for (itrScenery = m_listScenery.begin(); itrScenery != m_listScenery.end(); itrScenery++)
				{
					if (itrEnemy->m_bullet[i].HitTest(*itrScenery) &&
						itrScenery->m_objType != OT_Seaweed)
					{
						itrEnemy->m_bullet[i].m_bActive = false;
						itrEnemy->m_nBulletOut--;

						// Add fireworks to the list
						CFireworks fireworks;
						fireworks.m_objType = OT_FireworksBullet;
						fireworks.m_pTexture = m_pResource->m_textureFireworks[0].m_pTexture;
						fireworks.m_vectorPos.x = (float)itrEnemy->m_bullet[i].m_nX;
						fireworks.m_vectorPos.y = (float)itrEnemy->m_bullet[i].m_nY;
						fireworks.m_vectorPos.z = 0.0f;
						m_listFireworks.push_back(fireworks);

						itrScenery->m_nHp -= itrEnemy->m_bullet[i].m_nPower;
						break;
					}
				}
			} // End if	
		}
	} // End computer ship process
}

// Create enemy ships //////////////////////////////////////////////

void CGame::CreateEnemy()
{
	if ((timeGetTime() - m_timeCreateEnemy) > 5000 && 
		m_nEnemyCount < MAX_ENEMY)
	{
		CComputerShip enemyShip;

		// Homeplace of the enemy
		switch (::rand() % 3)
		{
		case 0:
			{
				enemyShip.m_nX = 6;		// Top left
				break;
			}
		case 1:
			{
				enemyShip.m_nX = 450;	// Top middle
				break;
			}
		case 2:
			{
				enemyShip.m_nX = 890;	// Top right
				break;
			}
		} // End switch
		enemyShip.m_nY = -10;
		enemyShip.m_nDirection = 2;	// Direction Down
		enemyShip.m_bActive = true;
		enemyShip.m_nBulletOut = 0;
		enemyShip.m_objType = OT_Boat_Computer;
		enemyShip.m_timeFired = timeGetTime();

⌨️ 快捷键说明

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