📄 canvasframe.cpp
字号:
result = DSBuf[num]->Lock(0,wsize,&pAudio,&bytesAudio,NULL,NULL,NULL);
if(result != DS_OK)
{
MessageBox("锁定缓冲区失败!");
return;
}
mmresult = mmioRead(hmmio,(HPSTR)pAudio,bytesAudio);
if(mmresult == -1)
{
MessageBox("读取音文件数据失败!");
return;
}
result = DSBuf[num]->Unlock(pAudio,bytesAudio,NULL,NULL);
if(result != DS_OK)
{
MessageBox("解除锁定缓冲区失败!");
return;
}
mmioClose(hmmio,0);
}
void canvasFrame::InitDD()
{
//建立 DirectDraw 对象
result = DirectDrawCreateEx(NULL, (VOID**)&DD, IID_IDirectDraw7, NULL);
if (result != DD_OK)
{
MessageBox("建立DirectDraw对象失败!");
return;
}
//设定协调层级
result = DD->SetCooperativeLevel(m_hWnd,DDSCL_EXCLUSIVE |
DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT );
if(result !=DD_OK)
{
MessageBox("设定程序协调层级失败!");
return;
}
//设定显示模式
result = DD->SetDisplayMode(640,480,16,0,DDSDM_STANDARDVGAMODE);
if(result !=DD_OK)
{
MessageBox("设定屏幕显示模式失败!");
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;
}
//连结后缓冲区
DDcaps.dwCaps = DDSCAPS_BACKBUFFER;
result = DDSur->GetAttachedSurface(&DDcaps,&DDBuf);
if(result !=DD_OK)
{
MessageBox("连接后缓冲区失败!");
return;
}
//声明幕后幕存区的共同特性
memset(&DDde,0,sizeof(DDde));
DDde.dwSize = sizeof(DDde);
DDde.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
DDde.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN ;
}
void canvasFrame::InitDS()
{
//建立 DirectSound 对象
result = DirectSoundCreate( NULL, &DS, NULL );
if(result != DS_OK)
{
MessageBox("建立 DirectSound 对象失败!");
return;
}
//设定协调层级
result = DS->SetCooperativeLevel( m_hWnd, DSSCL_PRIORITY );
if(result != DS_OK)
{
MessageBox("设定协调层级失败!");
return;
}
//建立主缓冲区
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("建立主缓冲区失败!");
return;
}
//设定声音播放格式
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("设定播放格式失败!");
return;
}
}
void canvasFrame::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_SysMouse, IID_IDirectInputDevice7,(void**)&DIms, NULL);
if(result != DI_OK)
{
MessageBox("建立鼠标输入装置失败!");
return;
}
//设定数据格式
result = DIms->SetDataFormat(&c_dfDIMouse2);
if(result != DI_OK)
{
MessageBox("设定数据格式失败!");
return;
}
//设定协调层级
result = DIms->SetCooperativeLevel(m_hWnd,DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
if(result != DI_OK)
{
MessageBox("设定程序协调层级失败!");
return;
}
//取用输入装置
result = DIms->Acquire();
if(result != DI_OK)
{
MessageBox("取用输入装置失败!");
return;
}
}
void canvasFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if( nChar== VK_ESCAPE ) //按下 Esc 键
PostMessage(WM_CLOSE ); //结束程序
if( nChar == VK_F1) //按下 F1 键
{
over = false;
score = 0;
ship.x = 540;
ship.y = 200;
ship.r[0].left = ship.x + 16;
ship.r[0].top = ship.y + 3;
ship.r[0].right = ship.x + 50;
ship.r[0].bottom = ship.y + 53;
ship.r[1].left = ship.x + 8;
ship.r[1].top = ship.y + 16;
ship.r[1].right = ship.x + 78;
ship.r[1].bottom = ship.y + 50;
ship.r[2].left = ship.x + 35;
ship.r[2].top = ship.y + 35;
ship.r[2].right = ship.x + 81;
ship.r[2].bottom = ship.y + 71;
DSBuf[0]->Play(0,0,1); //回放背景音乐
}
CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
void canvasFrame::MonBombShip()
{
if(mcount != 0)
for(i=0;i<50;i++) //取出所有怪物
if(mon[i].exist)
for(j=0;j<=3;j++) //取出所有怪物的碰撞点
for(m=0;m<3;m++) //取出飞机各个区块
if(mon[i].p[j].x >= ship.r[m].left && mon[i].p[j].x <= ship.r[m].right && mon[i].p[j].y >= ship.r[m].top && mon[i].p[j].y <= ship.r[m].bottom)
{
DDBuf->BltFast(mon[i].p[j].x-40,mon[i].p[j].y-40,DDPla[7],CRect(0,0,80,80),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
DDBuf->BltFast(0,150,DDPla[8],CRect(0,0,640,159),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
DSBuf[0]->Stop(); //停止背景音乐
DSBuf[3]->Play(0,0,0); //播放碰撞声音
for(i=0;i<50;i++) //清空怪物数组
if(mon[i].exist)
mon[i].exist = false;
for(i=0;i<100;i++) //清空怪物子弹数组
if(c[i].exist)
c[i].exist = false;
for(i=0;i<100;i++) //清空飞机子弹数组
if(b[i].exist)
b[i].exist = false;
bcount = 0;
ccount = 0;
mcount = 0;
over = true; //游戏结束
break;
}
}
void canvasFrame::HitShip()
{
if(ccount !=0)
for(i=0;i<100;i++) //取出所有怪物子弹
if(c[i].exist)
{
c[i].hitx = c[i].x + 10;
c[i].hity = c[i].y + 5;
hit = false;
for(j=0;j<=2;j++) //取出飞机各个区块
{
if(c[i].hitx >= ship.r[j].left && c[i].hitx <= ship.r[j].right && c[i].hity >= ship.r[j].top && c[i].hity <= ship.r[j].bottom)
{
hit = true;
DDBuf->BltFast(ship.x,ship.y,DDPla[7],CRect(0,0,80,80),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
break;
}
}
if(hit)
{
DDBuf->BltFast(0,150,DDPla[8],CRect(0,0,640,159),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
DSBuf[0]->Stop(); //停止背景音乐
DSBuf[3]->Play(0,0,0); //播放碰撞声音
for(i=0;i<50;i++) //清空怪物数组
if(mon[i].exist)
mon[i].exist = false;
for(i=0;i<100;i++) //清空怪物子弹数组
if(c[i].exist)
c[i].exist = false;
for(i=0;i<100;i++) //清空飞机子弹数组
if(b[i].exist)
b[i].exist = false;
over = true; //游戏结束
bcount = 0;
ccount = 0;
mcount = 0;
break;
}
else
{
c[i].x +=5; //计算下次 X 坐标
if(c[i].x>=640) //子弹移动超出屏幕
{
c[i].exist = false;
ccount--;
}
}
}
}
void canvasFrame::HitMon()
{
if(bcount != 0)
for(i=0;i<100;i++) //取出所有飞机子弹
{
if(b[i].exist)
{
b[i].hitx = b[i].x + 3;
b[i].hity = b[i].y + 5;
hit = false;
for(m=0;m<50;m++) //取出所有怪物
{
if(mon[m].exist)
{
for(j=0;j<=2;j++) //取出怪物各个区块
{
if(b[i].hitx >= mon[m].r[j].left && b[i].hitx <= mon[m].r[j].right && b[i].hity >= mon[m].r[j].top && b[i].hity <= mon[m].r[j].bottom)
{
hit = true;
DDBuf->BltFast(mon[m].x,mon[m].y,DDPla[7],CRect(0,0,80,80),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
switch(mon[m].type)
{
case 0: //打中蜗牛
score+=100;
break;
case 1: //打中小鸟
score+=50;
break;
case 2: //打中鱼
score+=150;
}
break;
}
}
if(hit)
{
mon[m].exist = false;
b[i].exist = false;
DSBuf[2]->Play(0,0,0);
mcount--;
bcount--;
break;
}
}
}
if(hit)
continue;
b[i].x -=20; //计算下次 X 坐标
if(b[i].x<-10) //子弹移动超出屏幕
{
b[i].exist = false;
bcount--;
}
}
}
}
void canvasFrame::PasteMon()
{
if(mcount != 0)
for(i=0;i<50;i++) //怪物坐标与贴图
if(mon[i].exist)
{
switch(mon[i].type)
{
case 0:
if(mon[i].draw%10 == 0)
{
mon[i].draw = 0;
mon[i].x += 20;
if(mon[i].x >= 640)
{
mon[i].exist = false;
break;
}
if(rand()%2 == 0) //以随机数决定往上或往下移动
{
mon[i].y -= 20;
if(mon[i].y<0) //移动到最上方
mon[i].y =0;
}
else
{
mon[i].y +=20;
if(mon[i].y>420) //移动到最下方
mon[i].y = 420;
}
if(mon[i].x <100) //判断是否发射子弹
for(j=0;j<100;j++)
{
if(!c[j].exist)
{
c[j].x = mon[i].x + 61;
c[j].y = mon[i].y+30;
c[j].exist = true;
ccount++;
break;
}
}
}
mon[i].r[0].left = mon[i].x + 28;
mon[i].r[0].top = mon[i].y + 7;
mon[i].r[0].right = mon[i].x + 72;
mon[i].r[0].bottom = mon[i].y + 34;
mon[i].r[1].left = mon[i].x;
mon[i].r[1].top = mon[i].y + 23;
mon[i].r[1].right = mon[i].x + 49;
mon[i].r[1].bottom = mon[i].y + 59;
mon[i].r[2].left = mon[i].x + 24;
mon[i].r[2].top = mon[i].y + 50;
mon[i].r[2].right = mon[i].x + 48;
mon[i].r[2].bottom = mon[i].y + 59;
mon[i].p[0].x = mon[i].x +46;
mon[i].p[0].y = mon[i].y + 8;
mon[i].p[1].x = mon[i].x + 71;
mon[i].p[1].y = mon[i].y + 22;
mon[i].p[2].x = mon[i].x + 36;
mon[i].p[2].y = mon[i].y + 57;
mon[i].p[3].x = mon[i].x + 1;
mon[i].p[3].y = mon[i].y + 31;
mon[i].draw++;
DDBuf->BltFast(mon[i].x,mon[i].y,DDPla[3],CRect(0,0,80,59),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
break;
case 1:
if(mon[i].draw%5 == 0)
{
mon[i].draw = 0;
if(mon[i].x > ship.x)
mon[i].x-=30;
else
mon[i].x+=30;
if(mon[i].y > ship.y)
mon[i].y-=30;
else
mon[i].y+=30;
}
mon[i].r[0].left = mon[i].x + 14;
mon[i].r[0].top = mon[i].y + 4;
mon[i].r[0].right = mon[i].x + 57;
mon[i].r[0].bottom = mon[i].y + 26;
mon[i].r[1].left = mon[i].x+1;
mon[i].r[1].top = mon[i].y + 14;
mon[i].r[1].right = mon[i].x + 43;
mon[i].r[1].bottom = mon[i].y + 41;
mon[i].r[2].left = mon[i].x + 5;
mon[i].r[2].top = mon[i].y + 34;
mon[i].r[2].right = mon[i].x + 22;
mon[i].r[2].bottom = mon[i].y + 55;
mon[i].p[0].x = mon[i].x +52;
mon[i].p[0].y = mon[i].y + 18;
mon[i].p[1].x = mon[i].x + 20;
mon[i].p[1].y = mon[i].y + 4;
mon[i].p[2].x = mon[i].x + 3;
mon[i].p[2].y = mon[i].y + 22;
mon[i].p[3].x = mon[i].x + 14;
mon[i].p[3].y = mon[i].y + 50;
mon[i].draw++;
DDBuf->BltFast(mon[i].x,mon[i].y,DDPla[4],CRect(0,0,60,56),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
break;
case 2:
if(mon[i].draw%10 == 0)
{
mon[i].draw = 0;
mon[i].x -= 20;
if(mon[i].x < 0)
{
mon[i].exist = false;
break;
}
}
mon[i].r[0].left = mon[i].x + 1;
mon[i].r[0].top = mon[i].y + 8;
mon[i].r[0].right = mon[i].x + 78;
mon[i].r[0].bottom = mon[i].y + 37;
mon[i].r[1].left = mon[i].x+8;
mon[i].r[1].top = mon[i].y + 36;
mon[i].r[1].right = mon[i].x + 52;
mon[i].r[1].bottom = mon[i].y + 50;
mon[i].r[2].left = mon[i].x + 20;
mon[i].r[2].top = mon[i].y + 0;
mon[i].r[2].right = mon[i].x + 37;
mon[i].r[2].bottom = mon[i].y + 8;
mon[i].p[0].x = mon[i].x +4;
mon[i].p[0].y = mon[i].y + 21;
mon[i].p[1].x = mon[i].x + 31;
mon[i].p[1].y = mon[i].y + 3;
mon[i].p[2].x = mon[i].x + 72;
mon[i].p[2].y = mon[i].y + 24;
mon[i].p[3].x = mon[i].x + 36;
mon[i].p[3].y = mon[i].y + 45;
mon[i].draw++;
DDBuf->BltFast(mon[i].x,mon[i].y,DDPla[5],CRect(0,0,79,51),DDBLTFAST_WAIT|DDBLTFAST_SRCCOLORKEY);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -