📄 game.cpp
字号:
m_pMemDC->DrawText(str,a,NULL);
//m_pMemDC->TextOut(rect4.left+15,rect4.top+37,str);
str.Format(L"%d",m_Speed);
a.left=rect4.right-40;
m_pMemDC->DrawText(str,a,NULL);
//m_pMemDC->TextOut(rect4.right-40,rect4.top+37,str);
a.left=rect4.left+10;a.top=rect4.top+32;
m_pMemDC->DrawText(L"分数: 0",a,NULL);
//m_pMemDC->TextOut(rect4.left+10,rect4.top+52,"SCORE: 0");
DeclareRect=rect4; //保存说明区域
m_pMemDC->SelectObject(pOldPen);
//把当前的内存DC复制到m_pInitialMemDC和图片m_pBitmap;
CBitmap* pOldBitmap2=m_pInitialMemDC->SelectObject(m_pInitialBitmap);
m_pInitialMemDC->BitBlt(Left,Top,1024,768,m_pMemDC,Left,Top,SRCCOPY);
m_pMemDC->SelectObject(pOldBitmap);
m_pInitialMemDC->SelectObject(pOldBitmap2);
//把刚才的状况恢复
// DrawDeclareNode(CubeX,true); //下一要运动的方块
for(i=0;i<m_RowCount;i++)
for(int j=0;j<m_ColCount;j++)
if(DiamondsA[i][j]==1)
DrawNode(i,j,false);
}
//画/擦除运动的方块
void CGame::DrawNode(int i, int j,bool IsErase)
{
CRect rect;
CBrush brush;
CBitmap* pOldBitmap=m_pMemDC->SelectObject(m_pBitmap);
if(!IsErase)
{
brush.CreateSolidBrush(GetRunCubeColor());
rect.left=GameRect.left+j*dx+1;
rect.top=GameRect.top+i*dy+1;
rect.right=rect.left+dx-1;
rect.bottom=rect.top+dy-1;
m_pMemDC->FillRect(rect,&brush);
}
else
{
CBitmap* pOldInitBitmap=m_pInitialMemDC->SelectObject(m_pInitialBitmap);
rect.left=GameRect.left+j*dx+1;
rect.top=GameRect.top+i*dy+1;
rect.right=rect.left+dx-1;
rect.bottom=rect.top+dy-1;
m_pMemDC->BitBlt(rect.left,rect.top,dx,dy,m_pInitialMemDC,rect.left,rect.top,SRCCOPY);
m_pInitialMemDC->SelectObject(pOldInitBitmap);
}
m_pMemDC->SelectObject(pOldBitmap);
}
void CGame::DrawNode(int a[][4], bool IsErase,CPoint position)
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(a[i][j]==0)
continue;
else
DrawNode(position.x+i,position.y+j,IsErase);
}
//是否遇到了边界或者有其他方块档住了
bool CGame::MeetBorder(int a[][4],int direction,CPoint p)
{
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j]==1)
DiamondsA[p.x+i][p.y+j]=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j]==1)
{
switch(direction)
{
case 1: //左移
if((p.y+j-1)<0) goto exit;
if(DiamondsA[p.x+i][p.y+j-1]==1) goto exit;
break;
case 2://右移
if((p.y+j+1)>=m_ColCount) goto exit;
if(DiamondsA[p.x+i][p.y+j+1]==1) goto exit;
break;
case 3://下移
if((p.x+i+1)>=m_RowCount) goto exit;
if(DiamondsA[p.x+i+1][p.y+j]==1) goto exit;
break;
case 4://变换
if(!Rotatable(a,p,DiamondsA)) goto exit;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
Cube[i][j]=CubeR[i][j];
a[i][j]=Cube[i][j];
if(Cube[i][j]==1) DiamondsA[p.x+i][p.y+j]=0;
}
return false;
break;
}
}
int x,y;
x=p.x;
y=p.y;
switch(direction)
{
case 1:
y--;break;
case 2:
y++;break;
case 3:
x++;break;
case 4:
break;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j]==1)
DiamondsA[x+i][y+j]=1;
return false;
exit:
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j]==1)
DiamondsA[p.x+i][p.y+j]=1;
return true;
}
//消去行
void CGame::LineDelete()
{
int m=0; //本次共消去的行数
bool flag=0;
for(int i=0;i<m_RowCount;i++)
{
flag=true;
for(int j=0;j<m_ColCount;j++)
if(DiamondsA[i][j]==0)
flag=false;
if(flag==true)
{
m++;
for(int k=i;k>0;k--)
{
for(int l=0;l<m_ColCount;l++)
{
DiamondsA[k][l]=DiamondsA[k-1][l];
if(DiamondsA[k][l]==0)
DrawNode(k,l,true);
else
DrawNode(k,l,false);
}
}
for(int l=0;l<m_ColCount;l++)
{
DiamondsA[0][l]=0;
DrawNode(0,l,true);
}
}
}
//CubeAssign(CubeX,Cube);
//DrawDeclareNode(Cube,true);
GenerateDiamond();
if(m>0)
{
sndPlaySound(NULL,SND_ASYNC);
CString strFilePath;
CString strFolderPath;
//Get program file path
TCHAR lpFileName[MAX_PATH+1];
GetModuleFileName(NULL, lpFileName, MAX_PATH);
strFilePath = lpFileName;
//Get program folder
int nLastIndex = strFilePath.ReverseFind('\\');
if (nLastIndex!=-1)
{
strFolderPath = strFilePath.Left(nLastIndex);
}
else
{
strFolderPath = _T("");
}
strFolderPath+=L"\\LineDel.wav";
sndPlaySound(strFolderPath,SND_SYNC);
PlayMusic(m_Music);
}
switch(m)
{
case 1:
m_Score++;
break;
case 2:
m_Score+=3;
break;
case 3:
m_Score+=6;
break;
case 4:
m_Score+=10;
break;
default:
break;
}
if(m>0)
{
int temp=m_StepScore;
m_StepScore=m_Score%50;
if(m_StepScore<temp) SetSpeed(m_Speed+1);
}
DrawDeclaration(m_Score,m_Speed,m_Level);
for(i=0;i<4;i++)
for(int j=0;j<4;j++)
if(Cube[i][j]==1)
if(DiamondsA[i+CubePosition.x][j+CubePosition.y]==1)
{
EndFlag=1;
CBitmap* pOldBitmap=m_pMemDC->SelectObject(m_pBitmap);
m_pMemDC->SetTextColor(m_clrGameOverFont);
m_pMemDC->SetBkColor(m_clrDeclareRect);
CRect a(DeclareRect.right-70,DeclareRect.top+55,DeclareRect.right,DeclareRect.bottom);
m_pMemDC->DrawText(L"游戏结束",a,NULL);
//m_pMemDC->TextOut(DeclareRect.right-70,DeclareRect.top+55,"游戏结束");
m_pMemDC->SetTextColor(m_clrDeclareFont);
m_pMemDC->SelectObject(pOldBitmap);
//Get program file path
CString strFilePath;
CString strFolderPath;
TCHAR lpFileName[MAX_PATH+1];
GetModuleFileName(NULL, lpFileName, MAX_PATH);
strFilePath = lpFileName;
//Get program folder
int nLastIndex = strFilePath.ReverseFind('\\');
if (nLastIndex!=-1)
{
strFolderPath = strFilePath.Left(nLastIndex);
}
else
{
strFolderPath = _T("");
}
strFolderPath+=L"\\GameOver.wav";
sndPlaySound(strFolderPath,SND_SYNC);
//sndPlaySound(L"GameOver.wav",SND_ASYNC);
}
}
bool CGame::Rotatable(int a[][4], CPoint p,int b[][100])
{
int tmp[4][4];
int i,j;
int k=4,l=4;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
tmp[i][j]=a[j][3-i];
CubeR[i][j]=0; //存放变换后的方块矩阵
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(tmp[i][j]==1)
{
if(k>i) k=i;
if(l>j) l=j;
}
for(i=k;i<4;i++)
for(j=l;j<4;j++)
{
CubeR[i-k][j-l]=tmp[i][j];
} //把变换后的矩阵移到左上角
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(CubeR[i][j]==0) continue;
if(((p.x+i)>=m_RowCount)||((p.y+j)<0)||((p.y+j)>=m_ColCount)) return false;
if(b[p.x+i][p.y+j]==1)
return false;
}
return true;
}
void CGame::DrawDeclareNode(int a[][4],bool IsErase)
{
CRect rect;
CBrush brush;
CBitmap* pOldBitmap=m_pMemDC->SelectObject(m_pBitmap);
if(!IsErase) //画出要出现的图形
{
brush.CreateSolidBrush(m_clrDeclareCube);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(a[i][j]==0) continue;
rect.left=DeclareRect.left+DeclareRect.Width()/2-2*dx+j*dx+1;
rect.top=DeclareRect.top+DeclareRect.Height()/2-2*dy+i*dy+1;
rect.right=rect.left+dx-1;
rect.bottom=rect.top+dy-1;
m_pMemDC->FillRect(rect,&brush);
}
}
else
{
//擦除上一图形
CBrush brush2;
brush2.CreateSolidBrush(m_clrDeclareRect); //以声明区域的颜色擦除
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
rect.left=DeclareRect.left+DeclareRect.Width()/2-2*dx+j*dx+1;
rect.top=DeclareRect.top+DeclareRect.Height()/2-2*dy+i*dy+1;
rect.right=rect.left+dx-1;
rect.bottom=rect.top+dy-1;
m_pMemDC->FillRect(rect,&brush2);
}
}
m_pMemDC->SelectObject(pOldBitmap);
}
//把叔祖a赋给数组b
void CGame::CubeAssign(int a[][4], int b[][4])
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
b[i][j]=a[i][j];
}
void CGame::SaveGame(CString filename)
{
/*
CString str;
int i,j;
EndFlag=true;
CStdioFile file;
if(file.Open(filename,CFile::modeCreate|CFile::modeWrite|CFile::typeText)==0)
{
AfxMessageBox("save error!");
return;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(Cube[i][j]==0)
file.WriteString("0\n");
else
file.WriteString("1\n");
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(CubeX[i][j]==0)
file.WriteString(L"0\n");
else
file.WriteString(L"1\n");
}
str.Format("%d,%d,%d,%d,%d\n",CubePosition.x,CubePosition.y,m_Score,m_Level,m_Speed);
file.WriteString(str);
for(i=0;i<m_RowCount;i++)
for(j=0;j<m_ColCount;j++)
{
if(DiamondsA[i][j]==0)
file.WriteString("0\n");
else
file.WriteString("1\n");
}
file.Close();
EndFlag=false;
*/
}
void CGame::ReadGame(CString filename)
{
/*
CString str;
int i,j,m;
EndFlag=true;
CStdioFile file;
if(file.Open(filename,CFile::modeRead)==0)
{
AfxMessageBox("save error!");
return;
}
CArchive ar(&file,CArchive::load);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
ar.ReadString(str);
sscanf(str,L"%d",&m);
if(m==0)
Cube[i][j]=0;
else
Cube[i][j]=1;
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
ar.ReadString(str);
sscanf(str,L"%d",&m);
if(m==0)
CubeX[i][j]=0;
else
CubeX[i][j]=1;
}
ar.ReadString(str);
sscanf(str,"%d,%d,%d,%d,%d",&CubePosition.x,&CubePosition.y,&m_Score,&m_Level,&m_Speed);
for(i=0;i<m_RowCount;i++)
for(j=0;j<m_ColCount;j++)
{
ar.ReadString(str);
sscanf(str,L"%d",&m);
if(m==0)
DiamondsA[i][j]=0;
else
DiamondsA[i][j]=1;
}
file.Close();
ar.Close();
DrawDeclaration(m_Score,m_Speed,m_Level);
SetPosition(Left,Top);
EndFlag=false;
//GenerateDiamond();
//CubeAssign(CubeX,Cube);
//GenerateDiamond();
DrawDeclareNode(CubeX,true);
DrawNode(Cube,false,CubePosition);
for(i=0;i<m_RowCount;i++)
for(j=0;j<m_ColCount;j++)
if(DiamondsA[i][j]==1)
DrawNode(i,j,false);
*/
}
void CGame::PlayMusic(BOOL flag)
{
m_Music=flag;
//Get program file path
CString strFilePath;
CString strFolderPath;
TCHAR lpFileName[MAX_PATH+1];
GetModuleFileName(NULL, lpFileName, MAX_PATH);
strFilePath = lpFileName;
//Get program folder
int nLastIndex = strFilePath.ReverseFind('\\');
if (nLastIndex!=-1)
{
strFolderPath = strFilePath.Left(nLastIndex);
}
else
{
strFolderPath = _T("");
}
strFolderPath+=L"\\Russia.wav";
if(flag==TRUE)
{
sndPlaySound(strFolderPath,SND_LOOP|SND_ASYNC);
}
else
{
sndPlaySound(NULL,SND_ASYNC);
}
}
//得到运动方块的颜色
COLORREF CGame::GetRunCubeColor()
{ //运动方块颜色随即产生
if(m_bRandomCubeColor==FALSE)
return(m_clrCube);
CTime t;
time_t starttime;
t=CTime::GetCurrentTime();
starttime=t.GetTime();
srand((unsigned)starttime);
//srand((unsigned)time(NULL));
int nTemp=rand()%8;
switch(nTemp)
{
case 0:
m_clrCube=RGB(0,0,0);
break;
case 1:
m_clrCube=RGB(255,0,0);
break;
case 2:
m_clrCube=RGB(0,255,0);
break;
case 3:
m_clrCube=RGB(0,0,255);
break;
case 4:
m_clrCube=RGB(255,0,255);
break;
case 5:
m_clrCube=RGB(255,0,128);
break;
case 6:
m_clrCube=RGB(255,255,0);
break;
case 7:
m_clrCube=RGB(0,255,255);
break;
}
return(m_clrCube);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -