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

📄 winmain.cpp

📁 入侵者游戏 编译环境VC 开发库DIRECTX8.1以上 2D游戏
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	bltText(buffer,150,340);

	strcpy(buffer,"MAURICIORITTER@HOTMAIL.COM\0");
	bltText(buffer,120,410);	

	tick = GetTickCount();
//	SndObjPlay(hsoGameOver, NULL);	
	while(GetTickCount() - tick < 6000);
}


void InitializeSound( void )
{
    bSoundEnabled = FALSE;
    if (SUCCEEDED(DirectSoundCreate(NULL, &lpDS, NULL)))
    {
		if (SUCCEEDED(lpDS->SetCooperativeLevel(hWndMain,
            DSSCL_NORMAL)))
        {
			hsoStop     = SndObjCreate(lpDS, "Stop", 1);
			hsoShoot    = SndObjCreate(lpDS, "Shoot",1);
			hsoBoom     = SndObjCreate(lpDS, "Boom", 8);
			hsoUfoShoot = SndObjCreate(lpDS, "UfoShoot", 8);
			hsoEnter    = SndObjCreate(lpDS, "Enter", 1);
			hsoSelect   = SndObjCreate(lpDS, "Select", 1);
			hsoGetExtra = SndObjCreate(lpDS, "GetExtra", 1);
			hsoGameOver = SndObjCreate(lpDS, "GameOver", 1);

            bSoundEnabled = TRUE;

        }
        else
        {
            lpDS->Release();
            lpDS = NULL;
        }
    }
}

void DestroySound( void )
{
//	if(!bWantSound)
//		return; //No work to be done
    bSoundEnabled = FALSE;
    if (lpDS != NULL)
    {
        SndObjDestroy(hsoEnter);
        hsoEnter = NULL;
        SndObjDestroy(hsoStop);
        hsoStop = NULL;
        SndObjDestroy(hsoShoot);
        hsoShoot = NULL;
        SndObjDestroy(hsoUfoShoot);
        hsoUfoShoot = NULL;
        SndObjDestroy(hsoBoom);
        hsoBoom = NULL;
        SndObjDestroy(hsoSelect);
        hsoSelect = NULL;
        SndObjDestroy(hsoGetExtra);
        hsoGetExtra = NULL;
        SndObjDestroy(hsoGameOver);
        hsoGameOver = NULL;
        lpDS->Release();
        lpDS = NULL;
    }
}

long FAR PASCAL MainWndproc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	Bullet*		pNewBullet, *pFirstBullet;

    switch( message )
    {
    case WM_TIMER:
		switch(wParam)
		{
		case 1:
			bShootEnable = TRUE;
			KillTimer(hWnd,1);
			break;
		}
		break;		
	case WM_ACTIVATEAPP:
		iAppState = APP_MAINMENU;
        bIsActive = (BOOL) wParam;
		break;
    case WM_KEYDOWN:
		// If We are at the help screen an hit any key, go back to MainMenu
		if (iAppState == APP_HELPSCREEN)
		{
			lastTickCount = 0;
			iAppState = APP_MAINMENU;
			return 0L;
		}

		switch (wParam)
        {
			case VK_RETURN:
				if (iAppState == APP_MAINMENU)
				{
					// if the user hit <enter> at the menu
					// change the app setting depending on the user selection
					switch(iOption)
					{
					case 0:
						lastTickCount = 0;

						SndObjPlay(hsoEnter, FALSE);

						// Set the app state to start the game
						iAppState = APP_GAMESCREEN;
						
						lastTickCount = 0;
						
						// Reset the score counter, level, ship state and ammo
						score = 0;
						iLevel = 0;
						iShipState = 0;
						iWeapon = 0;
						iLaserAmmo = 100;
						iPhotonAmmo = 0;
						iShield = 50;
						iMaxWeapon = 0;
						Lastscore = 99;

						// Initialize Level
						InitLevel(TRUE);

						break;
					case 1:
						// User selected the second option, credits
						iAppState = APP_CREDITS;
						break;
					case 2:
						// User selected the help screen
						iAppState = APP_HELPSCREEN;
						lastTickCount = 0;
						break;
					case 3:
						// User want磗 to quit
						// Cleanup up everything
						DestroyGame();
						PostQuitMessage(0);
						break;
					}
				}
				else
				{
					// If we are at the game screen, enter can be used to stop the
					// ship
					
					// If the ship is not ok, doen磘 need to stop it, 
					if (iShipState != SHIP_OK)
						break;

					// If the ship is already stopped, get out
					if (iShipMov == 0)
						break;

					// Stop the ship
					bShipStop = TRUE;
					iShipMov = 0;
				}
				break;

			case VK_CONTROL:
				// Control key is used to change the weapon

				// If not in game screen, get out
				if (iAppState != APP_GAMESCREEN)
					break;

				// Increment weapon type
				iWeapon++;

				// If we reached the Maximum weapon avaible for the ship
				// reset to the first weapon
				if (iWeapon > iMaxWeapon)
					iWeapon = 0;
				break;

			case VK_LEFT:
				// If not in game screen, get out

				if (iAppState != APP_GAMESCREEN)
					break;
				
				// If the ship is not ok, can磘 be moved
				if (iShipState != SHIP_OK)
					break;

				// Set the movement rate to -4 pixel
				iShipMov = -4;
				break;

			case VK_UP:
				
				// Up key is just used in the main menu screen
				// If not in main menu, get out

				if (iAppState != APP_MAINMENU)
					break;
				
				// User hit up key, raise up 1 menu option
				iOption--;

				// Play the select option sound
				SndObjStop(hsoSelect);
				SndObjPlay(hsoSelect,NULL);

				// If we reach the first option, go the last one
				if (iOption < 0)
					iOption = 3;
				break;

			case VK_RIGHT:
				
				// Right key is just used during the game
				// If we aren磘 in the game screen, get out

				if (iAppState != APP_GAMESCREEN)
					break;

				// If the ship is not ok, can磘 be moved
				if (iShipState != SHIP_OK)
					break;

				// Set the movement rate to +4 pixels
				iShipMov = 4;
				break;

			case VK_DOWN:
				
				// Down key can be used in the main screen or at the
				// the game screen to stp the ship
				switch (iAppState)
				{
				case APP_MAINMENU:
					// Select the down key at main menu
					// increment option counter
					iOption++;
					SndObjStop(hsoSelect);
					SndObjPlay(hsoSelect,NULL);
					if (iOption > 3)
						iOption = 0;
					break;
				case APP_GAMESCREEN:
					// Select down key at the game screen, stop the
					// ship if it磗 ok and not destroyed

					// If the ship is not ok, doen磘 need to stop it, 
					if (iShipState != SHIP_OK)
						break;

					// If the ship is already stopped, get out
					if (iShipMov == 0)
						break;

					bShipStop = TRUE;
					iShipMov = 0;
					break;
				}
				break;
			case VK_SPACE:
				// SPACE BAR is used to shoot during the game screen

				// Not at the game screen
				if (iAppState != APP_GAMESCREEN)
					break;

				// If ship is not ok, get out
				if (iShipState != SHIP_OK)
					break;

				// Check if we can shoot or we are waiting for our weapon
				// recharge
				if (bShootEnable == FALSE)
					break;

				// Start shooting flag
				bShoot = TRUE;
				
				// Check the weapon we are using
				switch (iWeapon)
				{
				case WP_1_LASER:
					// using single laser

					// if we are out of ammo, get energy from the shields
					if (iLaserAmmo == 0)
						iShield -= 1;
					else
						iLaserAmmo--;

					// Create a new bullet object and add it to bullet list
					pNewBullet = new Bullet;
					if (pBullet == NULL)
					{
						pBullet = pNewBullet;
						pNewBullet->SetXY(iShipPos+24, 380);
						pBullet->SetType(2);
					}
					else
					{
						pFirstBullet = pBullet;
						while (pBullet->GetNext() != NULL)
							pBullet = pBullet->GetNext();
						pBullet->SetNext(pNewBullet);
						pNewBullet->SetPrev(pBullet);
						pNewBullet->SetXY(iShipPos+24, 380);
						pNewBullet->SetType(2);
						pBullet = pFirstBullet;
					}
					break;

				case WP_2_LASER:

					// If we are out of ammo, not shoot
					if (iLaserAmmo < 2)
						return 0L;

					iLaserAmmo-=2;

					// Create a new bullet object and add it to bullet list
					pNewBullet = new Bullet;
					if (pBullet == NULL)
					{
						pBullet = pNewBullet;
						pNewBullet->SetXY(iShipPos+11, 380);
						pNewBullet->SetType(2);
						pFirstBullet = pBullet;
					}
					else
					{
						pFirstBullet = pBullet;
						while (pBullet->GetNext() != NULL)
							pBullet = pBullet->GetNext();
						pBullet->SetNext(pNewBullet);
						pNewBullet->SetPrev(pBullet);
						pNewBullet->SetXY(iShipPos+11, 385);
						pNewBullet->SetType(2);
						pBullet = pFirstBullet;
					}
					pNewBullet = new Bullet;
					pFirstBullet = pBullet;
					while (pBullet->GetNext() != NULL)
						pBullet = pBullet->GetNext();
					pBullet->SetNext(pNewBullet);
					pNewBullet->SetPrev(pBullet);
					pNewBullet->SetXY(iShipPos+34, 385);
					pNewBullet->SetType(2);
					pBullet = pFirstBullet;
					break;

				case WP_3_LASER:
			
					// If we are out of ammo, not shoot
					if (iLaserAmmo < 3)
						return 0L;
					
					iLaserAmmo-=3;
					
					pNewBullet = new Bullet;
					
					if (pBullet == NULL)
					{
						pBullet = pNewBullet;
						pNewBullet->SetXY(iShipPos+11, 380);
						pNewBullet->SetType(2);
						pFirstBullet = pBullet;
					}
					else
					{
						pFirstBullet = pBullet;
						while (pBullet->GetNext() != NULL)
							pBullet = pBullet->GetNext();
						pBullet->SetNext(pNewBullet);
						pNewBullet->SetPrev(pBullet);
						pNewBullet->SetXY(iShipPos+11, 385);
						pNewBullet->SetType(2);
						pBullet = pFirstBullet;
					}
					pNewBullet = new Bullet;
					pFirstBullet = pBullet;
					while (pBullet->GetNext() != NULL)
						pBullet = pBullet->GetNext();
					pBullet->SetNext(pNewBullet);
					pNewBullet->SetPrev(pBullet);
					pNewBullet->SetXY(iShipPos+35, 385);
					pNewBullet->SetType(2);
					pBullet = pFirstBullet;

					pNewBullet = new Bullet;
					pFirstBullet = pBullet;
					while (pBullet->GetNext() != NULL)
						pBullet = pBullet->GetNext();
					pBullet->SetNext(pNewBullet);
					pNewBullet->SetPrev(pBullet);
					pNewBullet->SetXY(iShipPos+23, 380);
					pNewBullet->SetType(2);
					pBullet = pFirstBullet;
					break;

				
				case  WP_1_PHOTON:


					if (iPhotonAmmo == 0)
						return 0L;

					iPhotonAmmo--;

					pNewBullet = new Bullet;
					if (pBullet == NULL)
					{
						pBullet = pNewBullet;
						pNewBullet->SetXY(iShipPos+24, 380);
					}
					else
					{
						pFirstBullet = pBullet;
						while (pBullet->GetNext() != NULL)
							pBullet = pBullet->GetNext();
						pBullet->SetNext(pNewBullet);
						pNewBullet->SetPrev(pBullet);
						pNewBullet->SetXY(iShipPos+24, 380);
						pBullet = pFirstBullet;
					}
					break;

				case  WP_2_PHOTON:

					if (iPhotonAmmo < 2)
						return 0L;
					iPhotonAmmo-=2;
					pNewBullet = new Bullet;
					if (pBullet == NULL)
					{
						pBullet = pNewBullet;
						pNewBullet->SetXY(iShipPos+11, 380);
						pFirstBullet = pBullet;
					}
					else
					{
						pFirstBullet = pBullet;
						while (pBullet->GetNext() != NULL)
							pBullet = pBullet->GetNext();
						pBullet->SetNext(pNewBullet);
						pNewBullet->SetPrev(pBullet);
						pNewBullet->SetXY(iShipPos+11, 385);
						pBullet = pFirstBullet;
					}
					pNewBullet = new Bullet;
					pFirstBullet = pBullet;
					while (pBullet->GetNext() != NULL)
						pBullet = pBullet->GetNext();
					pBullet->SetNext(pNewBullet);
					pNewBullet->SetPrev(pBullet);
					pNewBullet->SetXY(iShipPos+34, 385);
					pBullet = pFirstBullet;
					break;

				case  WP_3_PHOTON:

					if (iPhotonAmmo < 3)
						return 0L;
					iPhotonAmmo-=3;
					pNewBullet = new Bullet;
					if (pBullet == NULL)
					{
						pBullet = pNewBullet;
						pNewBullet->SetXY(iShipPos+11, 380);
						pFirstBullet = pBullet;
					}
					else
					{
						pFirstBullet = pBullet;
						while (pBullet->GetNext() != NULL)
							pBullet = pBullet->GetNext();
						pBullet->SetNext(pNewBullet);
						pNewBullet->SetPrev(pBullet);
						pNewBullet->SetXY(iShipPos+11, 385);
						pBullet = pFirstBullet;
					}
					pNewBullet = new Bullet;
					pFirstBullet = pBullet;
					while (pBullet->GetNext() != NULL)
						pBullet = pBullet->GetNext();
					pBullet->SetNext(pNewBullet);
					pNewBullet->SetPrev(pBullet);
					pNewBullet->SetXY(iShipPos+35, 385);
					pBullet = pFirstBullet;

					pNewBullet = new Bullet;
					pFirstBullet = pBullet;
					while (pBullet->GetNext() != NULL)
						pBullet = pBullet->GetNext();
					pBullet->SetNext(pNewBullet);
					pNewBullet->SetPrev(pBullet);
					pNewBullet->SetXY(iShipPos+23, 380);
					pBullet = pFirstBullet;
					break;
				}
					
				SndObjStop(hsoShoot);
				SndObjPlay(hsoShoot,0);
				bShootEnable = FALSE;
				if (iWeapon == 0)
					SetTimer(hWnd,1,400,NULL);
				else
					SetTimer(hWnd,1,680,NULL);
				break;

			case VK_F12:
			case VK_ESCAPE:
				
				// ESCAPE KEY AND F12 IS THE SAME AS QUIT

				// Quit if main menu, gameover if gamescreen
				if (iAppState == APP_MAINMENU)
				{
					DestroyGame();
					PostQuitMessage(0);
					break;
				}
				else
				{
					Ovni*	auxpUFO;
					Bullet* auxpBullet;
					Extra* auxpExtra;
					
					while(pExtra !=NULL)
					{
						auxpExtra = pExtra->GetNext();
						delete(pExtra);
						pExtra = auxpExtra;
					}

					while(pUFO !=NULL)
					{
						auxpUFO = pUFO->GetNext();
						delete(pUFO);
						pUFO = auxpUFO;
					}

					while(pBullet !=NULL)
					{
						auxpBullet = pBullet->GetNext();
						delete(pBullet);
						pBullet = auxpBullet;
					}

					lastTickCount = 0;
					iAppState = 0;
				}
				break;
        }
       break;

	case WM_CREATE:
        break;

    case WM_DESTROY:
        DestroyGame();
        PostQuitMessage(0);
        break;

    default:
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);

} /* MainWndproc */

void DrawBackGround(LPDIRECTDRAWSURFACE7 lpDest, int x, int y, int wx, int wy)
{
	// Blit the background to the destination surface
	RECT		rcRect;
	HRESULT		hRet;
	rcRect.left = x;
    rcRect.top = y;
    rcRect.right = wx;
    rcRect.bottom = wy;

	while (1)
    {
        hRet = lpDest->BltFast(x, y, lpBkGround, &rcRect, FALSE);
        if (hRet == DD_OK)
        {
            break;
        }
        if (hRet == DDERR_SURFACELOST)
        {
            hRet = RestoreSurfaces();
            if (hRet != DD_OK)
                break;
        }
        if (hRet != DDERR_WASSTILLDRAWING)
            break;
    }
}

void DestroyBullet(Bullet*	pDeadBullet)
{
	HRESULT			hRet;
	RECT			rcRect;

	// Destroy a bullet in the screen
		rcRect.left = pDeadBullet->GetX();

⌨️ 快捷键说明

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