打飛機(jī)游戲是一款非常經(jīng)典的游戲,大家想知道用c++語言怎么實現(xiàn)呢?這里小編就給大家?guī)鞢++打飛機(jī)的游戲源代碼,詳細(xì)會對大家學(xué)習(xí)c++語言有很大的幫助。
C++打飛機(jī)的游戲代碼
void TitleDisplay(void) //顯示序
{
HRESULT ddrval;
//計算圖象卷軸的坐標(biāo)數(shù)據(jù)
RECT backR1, frontR1, backR2, frontR2,backR3,frontR3;
static int n=0;
static int x1=0,x2=0,x3=0;
if(x1==310&&bSoundDrive)
{
cDSound.Play(false,1);
}
if(x1<420||x2<640)
{
x1+=1;
if (x1>420) x1=420;
if(x1<200)
{
SetRect(&backR1,0,0,x1,480);
SetRect(&frontR1,640-x1,0,640,480);
}
else
{
SetRect(&backR1,0,0,200,480);
SetRect(&frontR1,640-x1,0,840-x1,480);
}
if(x1>100)
{
x2+=2;
}
if(x2>640)x2=640;
if(x2<220)
{
SetRect(&backR2,0,0,x2,480);
SetRect(&frontR2,640-x2,0,640,480);
SetRect(&backR3,220-x2,0,220,480);
SetRect(&frontR3,0,0,x2,480);
}
else
{
SetRect(&backR2,0,0,220,480);
SetRect(&frontR2,640-x2,0,860-x2,480);
SetRect(&backR3,0,0,220,480);
SetRect(&frontR3,x2-220,0,x2,480);
}
//清屏后臺緩沖區(qū)
DDBLTFX ddBltFx;
ddBltFx.dwSize = sizeof(DDBLTFX);
ddBltFx.dwFillColor = DDColorMatch(lpDDSBack, RGB(0,0,0));
lpDDSBack->Blt(NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, &ddBltFx);
//將背景圖象Blit到后臺緩沖區(qū)
lpDDSBack->Blt(&frontR1, lpDDSPic6, &backR1, DDBLT_WAIT, NULL);
//將前景圖象透明Blit到后臺緩沖區(qū)
lpDDSBack->Blt(&frontR2, lpDDSPic4, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
lpDDSBack->Blt(&frontR3, lpDDSPic5, &backR3,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
}
else ///移動開始
{
//清屏后臺緩沖區(qū)
DDBLTFX ddBltFx;
ddBltFx.dwSize = sizeof(DDBLTFX);
ddBltFx.dwFillColor = DDColorMatch(lpDDSBack, RGB(0,0,0));
lpDDSBack->Blt(NULL, NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, &ddBltFx);
SetRect(&frontR1,0,0,640,480);
SetRect(&backR1,0,0,640,480);
lpDDSBack->Blt(&frontR1, lpDDSPic1, &backR1, DDBLT_WAIT|DDBLT_KEYSRC, NULL);
x3+=1;
if(x3<540)
{
SetRect(&backR1,540-x3,0,540,100);
SetRect(&frontR1,0,380,x3,480);
}
else
{
if(x3<640)
{
SetRect(&backR1,0,0,540,100);
SetRect(&frontR1,x3-540,380,x3,480);
SetRect(&backR2,1080-x3,100,540,200);
SetRect(&frontR2,0,380,x3-540,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
}
if(x3>=640&&x3<1080)
{
SetRect(&backR1,0,0,540-(x3-640),100);
SetRect(&frontR1,100+x3-640,380,640,480);
SetRect(&backR2,1080-x3,100,540,200);
SetRect(&frontR2,0,380,x3-540,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
}
if(x3>=1080&&x3<1180)
{
SetRect(&backR1,0,0,540-(x3-640),100);
SetRect(&frontR1,100+x3-640,380,640,480);
SetRect(&backR2,0,100,540,200);
SetRect(&frontR2,x3-1080,380,x3-540,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
SetRect(&backR2,540-(x3-1080),0,540,100);
SetRect(&frontR2,0,380,x3-1080,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
}
if(x3>=1180&&x3<1620)
{
SetRect(&backR1,540-(x3-1080),0,540,100);
SetRect(&frontR1,0,380,x3-1080,480);
SetRect(&backR2,0,100,540-(x3-1180),200);
SetRect(&frontR2,x3-1080,380,640,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
}
if(x3>=1620&&x3<1720)
{
SetRect(&backR1,0,100,540-(x3-1180),200);
SetRect(&frontR1,100+x3-1180,380,640,480);
SetRect(&backR2,0,0,540,100);
SetRect(&frontR2,x3-1620,380,x3-1080,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
SetRect(&backR2,540-(x3-1620),100,540,200);
SetRect(&frontR2,0,380,x3-1620,480);
lpDDSBack->Blt(&frontR2, lpDDSPic8, &backR2,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
if(x3>=1719)x3=640;
}
}
lpDDSBack->Blt(&frontR1, lpDDSPic8, &backR1,DDBLT_WAIT|DDBLT_KEYSRC,NULL);
}
//打印刷新率fps
HDC hdc;
char temp[32];
lpDDSBack->GetDC(&hdc);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(0,255,0));
sprintf(temp, "%d 幀/秒", iFps);
TextOut(hdc, 0, 0, temp, strlen(temp));
lpDDSBack->ReleaseDC(hdc);
// 換頁
while( 1 )
{
ddrval = lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );//調(diào)用換頁函數(shù)
if( ddrval == DD_OK )//成功則退出while循環(huán)
break;
if( ddrval == DDERR_SURFACELOST )//如果頁面丟失,則恢復(fù)頁面
{
if(!InitThread())break; //重新提取背景
ddrval = lpDDSPrimary->Restore(); //恢復(fù)主頁面
if(ddrval!=DD_OK)
break;
ddrval=lpDDSPic1->Restore(); //恢復(fù)離屏頁面1
if(ddrval!=DD_OK)
break;
ddrval=lpDDSPic4->Restore(); //恢復(fù)離屏頁面4
if(ddrval!=DD_OK)
break;
ddrval=lpDDSPic5->Restore(); //恢復(fù)離屏頁面5
if(ddrval!=DD_OK)
break;
ddrval=lpDDSPic6->Restore(); //恢復(fù)離屏頁面6
if(ddrval!=DD_OK)
break;
ddrval=lpDDSPic8->Restore(); //恢復(fù)離屏頁面8
if(ddrval!=DD_OK)
break;
}
if( ddrval != DDERR_WASSTILLDRAWING)
break;
}
}
- PC官方版
- 安卓官方手機(jī)版
- IOS官方手機(jī)版