?? bmframe.cpp
字號:
if (player[1].movetime == 20)
{
player[1].movetime = 0;
}
}
else
{
player[1].movetime = 0;
player[1].lastaction = 0;
}
player[1].offsetX = 60 * (player[1].movetime/10+1);
//位置移動
player[1].doMove();
}
else if (keybuffer[DIK_UP] & 0x80) //按下 上 鍵
{
//貼圖選擇
if (player[1].lastaction == 1)
{
player[1].movetime++;
if (player[1].movetime == 20)
{
player[1].movetime = 0;
}
}
else
{
player[1].movetime = 0;
player[1].lastaction = 1;
}
player[1].offsetX = 60 * (player[1].movetime/10+1);
//位置移動
player[1].doMove();
}
else if (keybuffer[DIK_LEFT] & 0x80) //按下 左 鍵
{
//貼圖選擇
if (player[1].lastaction == 2)
{
player[1].movetime++;
if (player[1].movetime == 20)
{
player[1].movetime = 0;
}
}
else
{
player[1].movetime = 0;
player[1].lastaction = 2;
}
player[1].offsetX = 60 * (player[1].movetime/10+1);
//位置移動
player[1].doMove();
}
else if (keybuffer[DIK_RIGHT] & 0x80) //按下 右 鍵
{
//貼圖選擇
if (player[1].lastaction == 3)
{
player[1].movetime++;
if (player[1].movetime == 20)
{
player[1].movetime = 0;
}
}
else
{
player[1].movetime = 0;
player[1].lastaction = 3;
}
player[1].offsetX = 60 * (player[1].movetime/10+1);
//位置移動
player[1].doMove();
}
else
{
player[1].movetime = 0;
player[1].offsetX = 0;
}
player[1].offsetY = 60 * player[1].lastaction;
if (keybuffer[DIK_NUMPADENTER] & 0x80) //按下 小鍵盤回車 鍵
{
i = ((int)player[1].posY+40)/40-1;
j = ((int)player[1].posX+30)/40-1;
if (player[1].laytime==0 && player[1].bombcount<player[1].bombsum && map[i][j]>='`')
{
player[1].laytime++;
bomb[bombtotal].index = 1;
bomb[bombtotal].setStart();
DSBuf[1]->Play(0,0,0);
player[1].bombcount ++;
bombtotal ++;
}
}
}
}
}
void BMFrame::renderFrame()
{
if (gamestate == MAINMENU)
{
//繪制主菜單
DDBuf->BltFast(0, 0, DDPla[5], CRect(0, 0, 800, 600), DDBLTFAST_WAIT);
if (gametime > 25)
gametime = 25;
if (menudown)
{
DDBuf->BltFast(300, 70-gametime*2, DDPla[6],
CRect(200*menuselect, 0, 200+200*menuselect, 100+gametime*2),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
DDBuf->BltFast(300, 20, DDPla[6],
CRect(200*menuselect, 100+gametime*2, 200+200*menuselect, 150),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
else
{
DDBuf->BltFast(300, 20, DDPla[6],
CRect(200*menuselect, 50-gametime*2, 200+200*menuselect, 150),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
DDBuf->BltFast(300, 120+gametime*2, DDPla[6],
CRect(200*menuselect, 0, 200+200*menuselect, 50-gametime*2),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
}
else
{
//繪制背景
DDBuf->BltFast(0, 0, DDPla[0], CRect(0, 0, 800, 600), DDBLTFAST_WAIT);
//搜索地圖
for (i = 0; i < 13; i++)
{
for (j = 17; j >= 0; j--)
{
isthing = false;
isstate = false;
//判斷物體類型
if (map[i][j] < '`')
isthing =true;
if (map[i][j] > '`')
isstate =true;
//繪制障礙
if (isthing)
{
picnum = (map[i][j]-'A');
DDBuf->BltFast(j*40+35, i*40+10, DDPla[2],
CRect(picnum%4*50, picnum/4*80, picnum%4*50+50, picnum/4*80+80),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
//繪制效果
if (isstate)
{
//道具浮動效果
if (map[i][j]>='m' && map[i][j]<='x')
map[i][j] = (map[i][j]-'m')/4*4 + 'm' + gametime/20%4;
picnum = (map[i][j]-'a');
DDBuf->BltFast(j*40+35, i*40+10, DDPla[3],
CRect(picnum%4*50, picnum/4*80, picnum%4*50+50, picnum/4*80+80),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
}
//Player1
if (i*40<=(int)player[0].posY && i*40+40>(int)player[0].posY)
{
//繪制人物
if (player[0].alive)
{
DDBuf->BltFast((int)player[0].posX, (int)player[0].posY, DDPla[1],
CRect(player[0].offsetX, player[0].offsetY, player[0].offsetX+60, player[0].offsetY+60),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
//繪制被炸效果
else
{
if ((int)player[0].posY < 50)
{
DDBuf->BltFast((int)player[0].posX-10, 0, DDPla[4],
CRect(player[0].offsetX, player[0].offsetY+50-(int)player[0].posY, player[0].offsetX+80, player[0].offsetY+120),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
DDBuf->BltFast((int)player[0].posX-10, (int)player[0].posY-50, DDPla[4],
CRect(player[0].offsetX, player[0].offsetY, player[0].offsetX+80, player[0].offsetY+120),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
}
//Player2
if (gamestate == VERSUS
&& i*40<=(int)player[1].posY && i*40+40>(int)player[1].posY)
{
//繪制人物
if (player[1].alive)
{
DDBuf->BltFast((int)player[1].posX, (int)player[1].posY, DDPla[1],
CRect(player[1].offsetX+180, player[1].offsetY, player[1].offsetX+240, player[1].offsetY+60),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
//繪制被炸效果
else
{
if ((int)player[1].posY < 50)
{
DDBuf->BltFast((int)player[1].posX-10, 0, DDPla[4],
CRect(player[1].offsetX+320, player[1].offsetY+50-(int)player[1].posY, player[1].offsetX+400, player[1].offsetY+120),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
DDBuf->BltFast((int)player[1].posX-10, (int)player[1].posY-50, DDPla[4],
CRect(player[1].offsetX+320, player[1].offsetY, player[1].offsetX+400, player[1].offsetY+120),
DDBLTFAST_WAIT | DDBLTFAST_SRCCOLORKEY);
}
}
}
}
}
void BMFrame::playSingle()
{
//炸彈進(jìn)行
for (i=0; i<bombtotal; i++)
{
if (bomb[i].doBlast())
delBomb();
}
player[0].doPlay();
}
void BMFrame::playVersus()
{
//炸彈進(jìn)行
for (i=0; i<bombtotal; i++)
{
if (bomb[i].doBlast())
delBomb();
}
player[0].doPlay();
player[1].doPlay();
//判斷被碰
if (abs((int)player[0].posX-(int)player[1].posX)<30
&& abs((int)player[0].posY-(int)player[1].posY)<30)
{
if(!player[0].alive && player[1].alive && player[0].traptime<420)
player[0].traptime = 420;
if(!player[1].alive && player[0].alive && player[1].traptime<420)
player[1].traptime = 420;
}
}
void BMFrame::delBomb()
{
if (bombtotal-1 != i)
bomb[i] = bomb[bombtotal-1];
bombtotal --;
}
void BMFrame::ColorKey(int num)
{
key.dwColorSpaceHighValue = 0;
key.dwColorSpaceLowValue = 0;
DDPla[num]->SetColorKey(DDCKEY_SRCBLT,&key);
}
void BMFrame::CreateDDPla(int width,int height,char* filename,int num)
{
//設(shè)定幕后內(nèi)存區(qū)大小
DDde.dwWidth = width;
DDde.dwHeight = height;
//建立幕后內(nèi)存區(qū)
result = DD->CreateSurface(&DDde, &DDPla[num], NULL);
if(result !=DD_OK)
{
MessageBox("建立幕后內(nèi)存區(qū)失敗!");
return;
}
//載入位圖
bitmap = (HBITMAP)::LoadImage(NULL,filename,IMAGE_BITMAP,width,height,LR_LOADFROMFILE);
if(bitmap==NULL)
{
MessageBox(filename);
return;
}
//復(fù)制位圖到繪圖中
::SelectObject(hdc,bitmap);
DDPla[num]->GetDC( &dhdc );
::BitBlt( dhdc , 0 , 0 ,width,height, hdc , 0 , 0 , SRCCOPY );
DDPla[num]->ReleaseDC(dhdc);
}
void BMFrame::CreateDSBuf(char* filename,int num)
{
//開啟與檢查文件格式
hmmio = mmioOpen(filename, NULL, MMIO_ALLOCBUF|MMIO_READ );
if(hmmio == NULL)
{
MessageBox("文件不存在!");
return;
}
ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E');
mmresult = mmioDescend(hmmio,&ckRiff,NULL,MMIO_FINDRIFF);
if(mmresult != MMSYSERR_NOERROR)
{
MessageBox("文件格式錯誤!");
return;
}
ckInfo.ckid = mmioFOURCC('f','m','t',' ');
mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
if(mmresult != MMSYSERR_NOERROR)
{
MessageBox("文件格式錯誤!");
return;
}
//讀取文件格式
mmresult = mmioRead(hmmio,(HPSTR)&wfmt,sizeof(wfmt));
if(mmresult == -1)
{
MessageBox("讀取格式失敗!");
return;
}
mmresult = mmioAscend(hmmio,&ckInfo,0);
ckInfo.ckid = mmioFOURCC('d','a','t','a');
mmresult = mmioDescend(hmmio,&ckInfo,&ckRiff,MMIO_FINDCHUNK);
if(mmresult != MMSYSERR_NOERROR)
{
MessageBox("文件格式錯誤!");
return;
}
wsize = ckInfo.cksize;
//設(shè)定次緩沖區(qū)特性
memset( &DSde,0,sizeof(DSde));
DSde.dwSize = sizeof(DSde);
DSde.dwFlags = DSBCAPS_STATIC | DSBCAPS_GLOBALFOCUS;
DSde.dwBufferBytes = wsize;
DSde.lpwfxFormat = &wfmt;
//建立次緩沖區(qū)
result = DS->CreateSoundBuffer( &DSde, &DSBuf[num], NULL );
if(result != DS_OK)
{
MessageBox("建立次緩沖區(qū)失敗!");
return;
}
//鎖定次緩沖區(qū)并載入音文件
result = DSBuf[num]->Lock(0,wsize,&pAudio,&bytesAudio,NULL,NULL,NULL);
if(result != DS_OK)
{
MessageBox("鎖定緩沖區(qū)失敗!");
return;
}
mmresult = mmioRead(hmmio,(HPSTR)pAudio,bytesAudio);
if(mmresult == -1)
{
MessageBox("讀取音文件數(shù)據(jù)失敗!");
return;
}
result = DSBuf[num]->Unlock(pAudio,bytesAudio,NULL,NULL);
if(result != DS_OK)
{
MessageBox("解除鎖定緩沖區(qū)失敗!");
return;
}
mmioClose(hmmio,0);
}
void BMFrame::InitDD()
{
//建立 DirectDraw 對象
result = DirectDrawCreateEx(NULL, (VOID**)&DD, IID_IDirectDraw7, NULL);
if (result != DD_OK)
{
MessageBox("建立DirectDraw對象失敗!");
return;
}
//設(shè)定協(xié)調(diào)層級
result = DD->SetCooperativeLevel(m_hWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN
|DDSCL_ALLOWREBOOT);
if(result !=DD_OK)
{
MessageBox("設(shè)定程序協(xié)調(diào)層級失敗!");
return;
}
//設(shè)定顯示模式
result = DD->SetDisplayMode(800,600,16,0,DDSDM_STANDARDVGAMODE);
if(result !=DD_OK)
{
MessageBox("設(shè)定屏幕顯示模式失敗!");
return;
}
//建立主繪圖頁
memset(&DDde,0,sizeof(DDde));
DDde.dwSize = sizeof(DDde);
DDde.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
DDde.dwBackBufferCount = 1;
DDde.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
result = DD->CreateSurface(&DDde,&DDSur,NULL);
if(result !=DD_OK)
{
MessageBox("建立主繪圖頁失敗!");
return;
}
//連結(jié)后緩沖區(qū)
DDcaps.dwCaps = DDSCAPS_BACKBUFFER;
result = DDSur->GetAttachedSurface(&DDcaps,&DDBuf);
if(result !=DD_OK)
{
MessageBox("連接后緩沖區(qū)失敗!");
return;
}
//聲明幕后內(nèi)存區(qū)的共同特性
memset(&DDde,0,sizeof(DDde));
DDde.dwSize = sizeof(DDde);
DDde.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
DDde.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ;
}
void BMFrame::InitDS()
{
//建立 DirectSound 對象
result = DirectSoundCreate( NULL, &DS, NULL );
if(result != DS_OK)
{
MessageBox("建立 DirectSound 對象失敗!");
return;
}
//設(shè)定協(xié)調(diào)層級
result = DS->SetCooperativeLevel( m_hWnd, DSSCL_PRIORITY );
if(result != DS_OK)
{
MessageBox("設(shè)定協(xié)調(diào)層級失敗!");
return;
}
//建立主緩沖區(qū)
memset( &DSde,0, sizeof(DSde) );
DSde.dwSize = sizeof(DSde);
DSde.dwFlags = DSBCAPS_PRIMARYBUFFER;
DSde.dwBufferBytes = 0;
DSde.lpwfxFormat = NULL;
result = DS->CreateSoundBuffer( &DSde, &DSPri, NULL );
if(result != DS_OK)
{
MessageBox("建立主緩沖區(qū)失敗!");
return;
}
//設(shè)定聲音播放格式
memset( &DSfmt,0, sizeof(DSfmt) );
DSfmt.wFormatTag = WAVE_FORMAT_PCM;
DSfmt.nChannels = 2;
DSfmt.nSamplesPerSec = 44100;
DSfmt.wBitsPerSample = 16;
DSfmt.nBlockAlign = DSfmt.wBitsPerSample / 8 * DSfmt.nChannels;
DSfmt.nAvgBytesPerSec = DSfmt.nSamplesPerSec * DSfmt.nBlockAlign;
result = DSPri->SetFormat(&DSfmt);
if(result != DS_OK)
{
MessageBox("設(shè)定播放格式失敗!");
return;
}
}
void BMFrame::InitDI()
{
//建立 DirectInput 對象
HINSTANCE hinst = AfxGetInstanceHandle();
result = DirectInputCreateEx(hinst, DIRECTINPUT_VERSION,IID_IDirectInput7,
(void**)&DI, NULL);
if(result != DI_OK)
{
MessageBox("建立 DirectInput 對象失敗!");
return;
}
//建立輸入裝置對象
result = DI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
(void**)&DIkb, NULL);
if(result != DI_OK)
{
MessageBox("建立鼠標(biāo)輸入裝置失敗!");
return;
}
//設(shè)定數(shù)據(jù)格式
result = DIkb->SetDataFormat(&c_dfDIKeyboard);
if(result != DI_OK)
{
MessageBox("設(shè)定數(shù)據(jù)格式失敗!");
return;
}
//設(shè)定協(xié)調(diào)層級
result = DIkb->SetCooperativeLevel(m_hWnd,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
if(result != DI_OK)
{
MessageBox("設(shè)定程序協(xié)調(diào)層級失敗!");
return;
}
//取用輸入裝置
result = DIkb->Acquire();
if(result != DI_OK)
{
MessageBox("取用輸入裝置失敗!");
return;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -