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

📄 game.cpp

📁 坦克大战
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	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_nArmor > 3 )
			player.m_nArmor = 3;
		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;
		if(enemy.m_type>5)
			enemy.m_type=5;
		break;

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


BOOL CGame::BoreBonus()
{
	m_bonus.m_active = TRUE;
	int temp = random(0,6);
	switch(temp)
	{
	case 0:
		m_bonus.m_type = BONUS_LIFE;
		break;
	case 1:
		m_bonus.m_type = BONUS_CLOCK;
		break;
	case 2:
		m_bonus.m_type = BONUS_SHOVEL;
		break;
	case 3:
		m_bonus.m_type = BONUS_BOMB;
		break;
	case 4:
		m_bonus.m_type = BONUS_HELMET;
		break;
	default:
		m_bonus.m_type = BONUS_STAR;
		break;
	}

	m_bonus.m_x = (float)random( 0, 570 - m_bonus.m_width );
	m_bonus.m_y = (float)random( 0, 440 - 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, 2 );
	int x, y;

	for( int i = 0; i < m_nMaxEnemys; i ++ )
		if( !m_enemy[i].m_active )
		{
			switch( which )
			{
			case 0:
				x = 0; y = 0;
				break;
			case 1:
				x = 288; y = 0;
				break;
			case 2:
				x = 576; y = 0;
				break;
			}
			int temp = rand() % 100+ m_nLevel*3 ;
			//temp=90;
			if( temp < 50 )
			{
				m_enemy[i].m_type = 0;
				m_enemy[i].m_speed = 2.8f;
			}
			else if( temp < 80 )
			{
				m_enemy[i].m_type = 1;
				m_enemy[i].m_speed = 3.8f;
			}
			else if(temp < 100)
			{
				m_enemy[i].m_type = 2;
				m_enemy[i].m_speed = 1.8f;
			} else if(temp<120)
			{
				m_enemy[i].m_type = 3;
				m_enemy[i].m_speed = 2.8f;
			} else if(temp<140)
			{
				m_enemy[i].m_type = 4;
				m_enemy[i].m_speed = 1.8f;
			} else
			{
				m_enemy[i].m_type = 5;
				m_enemy[i].m_speed = 3.2f;
			}

			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==7 || m_nEnemysLeft==14 ||
				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_type<=3)
				m_enemy[i].m_level = 2;
			
			if( m_enemy[i].m_type >3 )
				m_enemy[i].m_level = 4;

			m_enemy[i].Reborn();

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


BOOL CGame::LoadBitmaps() //Load all kind of bitmap
{
	// Loading bitmaps...
	
	m_pBmpList[0] = DDLoadBitmap( "Tile" );
	DDSetColorKey( m_pBmpList[0], 0 );

	m_pBmpList[1] = DDLoadBitmap( "Player1" );
	DDSetColorKey( m_pBmpList[1], RGB(255,255,255) );

	m_pBmpList[2] = DDLoadBitmap( "Bullet" );
	DDSetColorKey( m_pBmpList[2], 0 );
	
	m_pBmpList[3] = DDLoadBitmap( "Explode1" );
	DDSetColorKey( m_pBmpList[3], 0 );

	m_pBmpList[4] = DDLoadBitmap( "Explode2" );
	DDSetColorKey( m_pBmpList[4], 0 );

	m_pBmpList[5] = DDLoadBitmap( "Enemys" );
	DDSetColorKey( m_pBmpList[5], RGB(255,255,255) );

	m_pBmpList[6] = DDLoadBitmap( "Bonus" );
	DDSetColorKey( m_pBmpList[6], RGB(12,12,12) );

	m_pBmpList[7] = DDLoadBitmap( "Bore" );
	DDSetColorKey( m_pBmpList[7], 0 );
	
	m_pBmpList[8] = DDLoadBitmap( "Misc" );
	DDSetColorKey( m_pBmpList[8], RGB(255,255,255) );

	m_pBmpList[9] = DDLoadBitmap( "Player2" );
	DDSetColorKey( m_pBmpList[9], RGB(255,255,255) );

	m_pBmpList[10] = DDLoadBitmap( "Splash" );
	DDSetColorKey( m_pBmpList[10], 0 );

	m_pBmpList[11] = DDLoadBitmap( "GameOver" );
	DDSetColorKey( m_pBmpList[11], 0 );

	m_pBmpList[12] = NULL;
	//DDSetColorKey( m_pBmpList[12], RGB(255,255,255) );

	m_pBmpList[13] = NULL;
	//DDSetColorKey( m_pBmpList[13], RGB(255,255,255) );

	m_pBmpList[14] = DDLoadBitmap( "Shield" );
	DDSetColorKey( m_pBmpList[14], 0 );

	m_pBmpList[15] = DDLoadBitmap("About");
	DDSetColorKey(m_pBmpList[15],0);

	m_pBmpList[16] = DDLoadBitmap("Logo");
	DDSetColorKey(m_pBmpList[16],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( 32, 32, TRUE );
	m_player[1].Create( 32, 32, TRUE );

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

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

	m_bonus.Create( 32, 32, FALSE );

	m_lastTime=timeGetTime();
	m_gameState = GS_BEGIN;

	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;
	m_player[0].m_bullet[2].m_active = FALSE;
	m_player[1].m_bullet[2].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 = 5;
	m_player[0].m_type = 0;
	m_player[0].m_nScore = 0;
	m_player[0].m_nArmor=0;

	if( !m_bSingle )
	{
		m_player[1].m_nLife = 5;
		m_player[1].m_type = 0;
		m_player[1].m_nScore = 0;
		m_player[1].m_nArmor=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();
}

void CGame::DoAbout()
{
	HRESULT hr;
	
	DDClear( NULL, 0 );

	hr = m_pddsBackBuffer->BltFast(0, 0, m_pBmpList[15], NULL,
			DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT );
	if( hr == DDERR_SURFACELOST )
		RestoreSurfaces();
}

void CGame::DrawGround()
{
	HRESULT hr;
	RECT rc;
	rc.left=256;
	rc.top=0;
	rc.bottom=32;
	rc.right=256+32;
	for(int x=0;x<19;x++)
	{
		for(int y=0;y<15;y++)
		{
			hr=m_pddsBackBuffer->BltFast(x*32,y*32,m_pBmpList[0],&rc,
				DDBLTFAST_WAIT);
			if(hr==DDERR_SURFACELOST)
				RestoreSurfaces();
		}
	}
}

void CGame::DoBegin()
{
	if(timeGetTime()-m_lastTime<200)
		return;
	m_lastTime=timeGetTime();
	

	DDClear( NULL, RGB(255,255,255) );
	
	RECT rc;
	rc.left = 0;
	rc.right = 428;
	rc.top = 68*m_nFrame;
	rc.bottom = 68*m_nFrame+68;
	HRESULT hr;

	hr=m_pddsBackBuffer->BltFast( 106, 200,
		m_pBmpList[16], &rc, DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT );
	if(hr==DDERR_SURFACELOST)
		RestoreSurfaces();

	if(++m_nFrame>4)
	{
		//Sleep(2000);
		m_gameState=GS_SPLASH;	
	}
}

⌨️ 快捷键说明

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