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

📄 game.cpp

📁 VC环境下运行的坦克大战
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	static int frameCount;
	static float fps;

	if( m_gameState == GS_OVER || m_gameState == GS_ACTIVE ||
		m_gameState == GS_WIN )
		ProcessInput();

	switch( m_gameState )
	{
	case GS_SPLASH:
		DoSplash();
		break;
	
	case GS_OVER:
		DrawWorld();
		m_pddsBackBuffer->BltFast( 200, 200, m_pBmpList[11],
			NULL, DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT );
		break;

	case GS_ACTIVE:
		DrawWorld();
		break;
		
	case GS_WIN:
		DrawWorld();
		
		if( timeGetTime() - m_lastTime > 3000 ||
			timeGetTime() < m_lastTime )
		{
			m_gameState = GS_ACTIVE;
			m_nLevel ++;
			InitLevel();
		}
		break;
	}

	frameCount ++;
	DWORD thisTick = timeGetTime();
	if( thisTick - lastTick > 1000 )
	{
		fps = (float)frameCount / (thisTick - lastTick) * 1000;
		frameCount = 0;
		lastTick = thisTick;
	}
	
	if( m_bShowStats )
	{
		// Show FPS
		char buf[255];
		sprintf( buf, "FPS: %.2f", fps );
		OutputText( 10, 10, buf );
	}
	
	FlipScreen();
}


void CGame::Explode( CSprite& sprite, BOOL bExplode )
{
	for( int i = 0; i < NUM_EXPLODES; i ++ )
		if( !m_explode[i].m_active )
		{
			int x = (int)sprite.m_x + sprite.m_width/2;
			int y = (int)sprite.m_y + sprite.m_height/2;
			m_explode[i].m_active = TRUE;
			m_explode[i].m_x = (float)(x - m_explode[i].m_width/2);
			m_explode[i].m_y = (float)(y - m_explode[i].m_height/2);
			m_explode[i].m_time = timeGetTime();
			m_explode[i].m_bExplode = bExplode;
			break;
		}
}


void CGame::EatBonus( CPlayer& player )
{
	m_DirectSound.Play( EFFECT_PICK );

	int i;
	switch( m_bonus.m_type )
	{
	case BONUS_LIFE:
		player.m_nLife ++;
		m_DirectSound.Play( EFFECT_LIFE );
		break;
	case BONUS_CLOCK:
		m_bEnemyLocked = TRUE;
		m_lockTime = timeGetTime();
		break;
	case BONUS_SHOVEL:
		m_plane.Protect();
		break;
	case BONUS_BOMB:
		for( i = 0; i < m_nMaxEnemys; i ++ )
			if( m_enemy[i].m_active &&
				!m_enemy[i].m_bBoring )
			{
				Explode( m_enemy[i], TRUE );
				m_enemy[i].m_active = FALSE;
				m_nEnemys --;
			}
		m_DirectSound.Play( EFFECT_EXPLODE );
		break;
	case BONUS_STAR:
		if( ++player.m_type > 3 )
			player.m_type = 3;
		break;
	case BONUS_HELMET:
		player.Shield( 10000 );
		break;
	}
	m_bonus.m_active = FALSE;
}


void CGame::PlayerBeenHit( CPlayer& player )
{
	if( !player.m_active )
		return;

	Explode( player, TRUE );
	if( --player.m_nLife <= 0 )
	{
		player.m_active = FALSE;
		return;
	}

	ResetPlayer( player );
	player.m_type = 0;
}


void CGame::EatBonus( CEnemy& enemy )
{
	switch( m_bonus.m_type )
	{
	case BONUS_LIFE:
		m_nEnemys += 5;
		m_nEnemysLeft += 5;
		break;

	case BONUS_CLOCK:
		m_player[0].Lock();
		m_player[1].Lock();
		break;

	case BONUS_SHOVEL:
		m_plane.Bare();
		break;

	case BONUS_BOMB:
		PlayerBeenHit( m_player[0] );
		PlayerBeenHit( m_player[1] );
		m_DirectSound.Play( EFFECT_EXPLODE );
		break;

	case BONUS_STAR:
		enemy.m_type = 2;
		enemy.m_level = 2;
		break;

	case BONUS_HELMET:
		enemy.Shield( 10000 );
		break;
	}
	m_bonus.m_active = FALSE;
}


BOOL CGame::BoreBonus()
{
	m_bonus.m_active = TRUE;
	int temp = rand() % 100;
	if( temp < 10 )
		m_bonus.m_type = BONUS_LIFE;
	else if( temp < 30 )
		m_bonus.m_type = BONUS_CLOCK;
	else if( temp < 50 )
		m_bonus.m_type = BONUS_SHOVEL;
	else if( temp < 65 )
		m_bonus.m_type = BONUS_BOMB;
	else if( temp < 85 )
		m_bonus.m_type = BONUS_STAR;
	else
		m_bonus.m_type = BONUS_HELMET;

	m_bonus.m_x = (float)random( 0, 416 - m_bonus.m_width );
	m_bonus.m_y = (float)random( 0, 416 - m_bonus.m_height );
	m_bonus.m_lastTime = 
	m_bonus.m_flickerTime = timeGetTime();

	return TRUE;
}


BOOL CGame::BoreEnemy()
{
	if( m_nEnemysLeft <= 0 )
		return FALSE;
	
	int which = random( 0, 4 );
	int x, y;

	for( int i = 0; i < m_nMaxEnemys; i ++ )
		if( !m_enemy[i].m_active )
		{
			switch( which )
			{
			case 0:
				x = 2; y = 2;
				break;
			case 1:
				x = 194; y = 2;
				break;
			case 2:
				x = 386; y = 2;
				break;
			case 3:
				x = 2; y = 160;
				break;
			case 4:
				x = 386; y = 160;
				break;
			}
			int temp = rand() % 100;
			if( temp < 50 )
			{
				m_enemy[i].m_type = 0;
				m_enemy[i].m_speed = 0.7f;
			}
			else if( temp < 80 )
			{
				m_enemy[i].m_type = 1;
				m_enemy[i].m_speed = 1.2f;
			}
			else
			{
				m_enemy[i].m_type = 2;
				m_enemy[i].m_speed = 0.5f;
			}
			m_enemy[i].m_x = (float)x;
			m_enemy[i].m_y = (float)y;
			m_enemy[i].m_dir = DIR_DOWN;
			if( m_nEnemysLeft == 4 ||
				m_nEnemysLeft == 11 ||
				m_nEnemysLeft == 18 )
			{
				m_enemy[i].m_bBonus = TRUE;
				m_enemy[i].m_redTime = 0;
			}
			else
				m_enemy[i].m_bBonus = FALSE;

			if( m_enemy[i].m_type == 2 )
				m_enemy[i].m_level = 2;

			m_enemy[i].Reborn();

			m_nEnemysLeft --;
			if( ++which > 2 )
				which = 0;
			return TRUE;
		}
	return FALSE;
}


BOOL CGame::LoadBitmaps()
{
	// Loading bitmaps...
	
	m_pBmpList[0] = DDLoadBitmap( "graphics\\tile.bmp" );
	DDSetColorKey( m_pBmpList[0], 0 );

	m_pBmpList[1] = DDLoadBitmap( "graphics\\player1.bmp" );
	DDSetColorKey( m_pBmpList[1], 0 );

	m_pBmpList[2] = DDLoadBitmap( "graphics\\bullet.bmp" );
	DDSetColorKey( m_pBmpList[2], 0 );
	
	m_pBmpList[3] = DDLoadBitmap( "graphics\\explode1.bmp" );
	DDSetColorKey( m_pBmpList[3], 0 );

	m_pBmpList[4] = DDLoadBitmap( "graphics\\explode2.bmp" );
	DDSetColorKey( m_pBmpList[4], 0 );

	m_pBmpList[5] = DDLoadBitmap( "graphics\\enemy.bmp" );
	DDSetColorKey( m_pBmpList[5], 0 );

	m_pBmpList[6] = DDLoadBitmap( "graphics\\bonus.bmp" );
	DDSetColorKey( m_pBmpList[6], 0 );

	m_pBmpList[7] = DDLoadBitmap( "graphics\\bore.bmp" );
	DDSetColorKey( m_pBmpList[7], 0 );
	
	m_pBmpList[8] = DDLoadBitmap( "graphics\\misc.bmp" );
	DDSetColorKey( m_pBmpList[8], RGB(255,255,255) );

	m_pBmpList[9] = DDLoadBitmap( "graphics\\player2.bmp" );
	DDSetColorKey( m_pBmpList[9], 0 );

	m_pBmpList[10] = DDLoadBitmap( "graphics\\splash.bmp" );
	DDSetColorKey( m_pBmpList[10], 0 );

	m_pBmpList[11] = DDLoadBitmap( "graphics\\gameover.bmp" );
	DDSetColorKey( m_pBmpList[11], 0 );

	m_pBmpList[12] = DDLoadBitmap( "graphics\\flag.bmp" );
	DDSetColorKey( m_pBmpList[12], RGB(255,255,255) );

	m_pBmpList[13] = DDLoadBitmap( "graphics\\num.bmp" );
	DDSetColorKey( m_pBmpList[13], RGB(255,255,255) );

	m_pBmpList[14] = DDLoadBitmap( "graphics\\shield.bmp" );
	DDSetColorKey( m_pBmpList[14], 0 );

	return TRUE;
}


//-----------------------------------------------------------------------------
// Name: DDColorMatch()
// Desc: Convert a RGB color to a pysical color.
//       We do this by leting GDI SetPixel() do the color matching
//       then we lock the memory and see what it got mapped to.
//-----------------------------------------------------------------------------
DWORD DDColorMatch(LPDIRECTDRAWSURFACE pdds, COLORREF rgb)
{
    COLORREF                rgbT;
    HDC                     hdc;
    DWORD                   dw = CLR_INVALID;
    DDSURFACEDESC			ddsd;
    HRESULT                 hres;

    //
    //  Use GDI SetPixel to color match for us
    //
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
        rgbT = GetPixel(hdc, 0, 0);     // Save current pixel value
        SetPixel(hdc, 0, 0, rgb);       // Set our value
        pdds->ReleaseDC(hdc);
    }
    //
    // Now lock the surface so we can read back the converted color
    //
    ddsd.dwSize = sizeof(ddsd);
    while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
        ;
    if (hres == DD_OK)
    {
        dw = *(DWORD *) ddsd.lpSurface;                 // Get DWORD
        if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
            dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;  // Mask it to bpp
        pdds->Unlock(NULL);
    }
    //
    //  Now put the color that was there back.
    //
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
        SetPixel(hdc, 0, 0, rgbT);
        pdds->ReleaseDC(hdc);
    }
    return dw;
}


//-----------------------------------------------------------------------------
// Name: DDSetColorKey()
// Desc: Set a color key for a surface, given a RGB.
//       If you pass CLR_INVALID as the color key, the pixel
//       in the upper-left corner will be used.
//-----------------------------------------------------------------------------
HRESULT DDSetColorKey(LPDIRECTDRAWSURFACE pdds, COLORREF rgb)
{
    DDCOLORKEY              ddck;

    ddck.dwColorSpaceLowValue = DDColorMatch(pdds, rgb);
    ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
    return pdds->SetColorKey(DDCKEY_SRCBLT, &ddck);
}


void CGame::ResetPlayer( CPlayer& player )
{
	if( &player == &m_player[0] )
	{
		player.m_x = PLAYER1_STARTX;
		player.m_y = PLAYER1_STARTY;
	}
	else
	{
		player.m_x = PLAYER2_STARTX;
		player.m_y = PLAYER2_STARTY;
	}
	player.Reborn();
}



BOOL CGame::InitGame()
{
	srand( timeGetTime() );
	
	// initialize input devices
	m_DirectInput.Create( m_hInst, m_hWnd );
	
	// initizlize sound effects
	m_DirectSound.Init( m_hWnd );

	m_player[0].Create( 28, 28, TRUE );
	m_player[1].Create( 28, 28, TRUE );

	for( int i = 0; i < NUM_ENEMYS; i ++ )
		m_enemy[i].Create( 28, 28, FALSE );

	for( i = 0; i < NUM_EXPLODES;  i ++ )
		m_explode[i].Create( 28, 28, FALSE );

	m_bonus.Create( 30, 28, FALSE );

	m_gameState = GS_SPLASH;

	return TRUE;
}


void CGame::CleanUpGame()
{

}


BOOL CGame::InitLevel()
{
	char map[255];
	sprintf( map, "map\\level%d.map", m_nLevel );
	if( !m_plane.Create( map ) )
	{
		m_nLevel = 1;
		sprintf( map, "map\\level%d.map", m_nLevel );
		if( !m_plane.Create( map ) )
			return FALSE;
	}

	if( m_player[0].m_nLife > 0 )
		ResetPlayer( m_player[0] );
	if( m_bSingle )
		m_player[1].m_active = FALSE;
	else if( m_player[1].m_nLife > 0 )
		ResetPlayer( m_player[1] );

	m_player[0].m_bullet[0].m_active = FALSE;
	m_player[0].m_bullet[1].m_active = FALSE;
	m_player[1].m_bullet[0].m_active = FALSE;
	m_player[1].m_bullet[1].m_active = FALSE;


	for( int i = 0; i < NUM_ENEMYS; i ++ )
	{
		m_enemy[i].m_active = FALSE;
		m_enemy[i].m_bullet[0].m_active = FALSE;
	}
	for( i = 0; i < NUM_EXPLODES; i ++ )
		m_explode[i].m_active = FALSE;
	m_bonus.m_active = FALSE;

	m_bEnemyLocked = FALSE;
	m_nEnemysLeft = m_nEnemys = 20;

	return TRUE;
}


BOOL CGame::ResetGame()
{
	m_player[0].m_nLife = 3;
	m_player[0].m_type = 0;
	m_player[0].m_nScore = 0;

	if( !m_bSingle )
	{
		m_player[1].m_nLife = 3;
		m_player[1].m_type = 0;
		m_player[1].m_nScore = 0;
		m_nMaxEnemys = 6;
	}
	else
		m_nMaxEnemys = 4;

	m_gameState = GS_ACTIVE;

	m_nLevel = 1;
	return InitLevel();
}



///////////////////////////////////////////////////////
//		Main entry point for win32 application
///////////////////////////////////////////////////////

int WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR lpCmdLine,
				   int nCmdShow )
{
	CGame	game;
	
	game.Initialize( hInst );
	return game.Run();
}

⌨️ 快捷键说明

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