m_intState[i][j]=0;
m_intLevel=NORMAL;//初始化难度
srand((unsigned)time(NULL));//初始化随机数
m_intScore=0;
m_CurrentCom.intComID=-1;
m_CurrentCom.intDimension=0;
m_CurrentCom.pintArray=NULL;
//初始化7个部件
//0:
方块
m_Componets[0].intComID=0;
m_Componets[0].intDimension=2;
m_Componets[0].pintArray=newint[4];
for(i=0;i<4;i++)
m_Componets[0].pintArray[i]=1;//11
//11
//1:
-|
m_Componets[1].intComID=1;
m_Componets[1].intDimension=3;
m_Componets[1].pintArray=newint[9];
m_Componets[1].pintArray[0]=0;
m_Componets[1].pintArray[1]=1;
m_Componets[1].pintArray[2]=0;//010
m_Componets[1].pintArray[3]=1;//111
m_Componets[1].pintArray[4]=1;//000
m_Componets[1].pintArray[5]=1;
m_Componets[1].pintArray[6]=0;
m_Componets[1].pintArray[7]=0;
m_Componets[1].pintArray[8]=0;
//2
m_Componets[2].intComID=2;
m_Componets[2].intDimension=3;
m_Componets[2].pintArray=newint[9];
m_Componets[2].pintArray[0]=1;
m_Componets[2].pintArray[1]=0;
m_Componets[2].pintArray[2]=0;//100
m_Componets[2].pintArray[3]=1;//110
m_Componets[2].pintArray[4]=1;//010
m_Componets[2].pintArray[5]=0;
m_Componets[2].pintArray[6]=0;
m_Componets[2].pintArray[7]=1;
m_Componets[2].pintArray[8]=0;
//3
m_Componets[3].intComID=3;
m_Componets[3].intDimension=3;
m_Componets[3].pintArray=newint[9];
m_Componets[3].pintArray[0]=0;
m_Componets[3].pintArray[1]=0;
m_Componets[3].pintArray[2]=1;//001
m_Componets[3].pintArray[3]=0;//011
m_Componets[3].pintArray[4]=1;//010
m_Componets[3].pintArray[5]=1;
m_Componets[3].pintArray[6]=0;
m_Componets[3].pintArray[7]=1;
m_Componets[3].pintArray[8]=0;
//4
m_Componets[4].intComID=4;
m_Componets[4].intDimension=3;
m_Componets[4].pintArray=newint[9];
m_Componets[4].pintArray[0]=1;
m_Componets[4].pintArray[1]=0;
m_Componets[4].pintArray[2]=0;//100
m_Componets[4].pintArray[3]=1;//111
m_Componets[4].pintArray[4]=1;//000
m_Componets[4].pintArray[5]=1;
m_Componets[4].pintArray[6]=0;
m_Componets[4].pintArray[7]=0;
m_Componets[4].pintArray[8]=0;
//5
m_Componets[5].intComID=5;
m_Componets[5].intDimension=3;
m_Componets[5].pintArray=newint[9];
m_Componets[5].pintArray[0]=0;
m_Componets[5].pintArray[1]=0;
m_Componets[5].pintArray[2]=1;//001
m_Componets[5].pintArray[3]=1;//111
m_Componets[5].pintArray[4]=1;//000
m_Componets[5].pintArray[5]=1;
m_Componets[5].pintArray[6]=0;
m_Componets[5].pintArray[7]=0;
m_Componets[5].pintArray[8]=0;
//6
m_Componets[6].intComID=6;
m_Componets[6].intDimension=4;
m_Componets[6].pintArray=newint[16];
m_Componets[6].pintArray[0]=0;
m_Componets[6].pintArray[1]=1;
m_Componets[6].pintArray[2]=0;//0100
m_Componets[6].pintArray[3]=0;//0100
m_Componets[6].pintArray[4]=0;//0100
m_Componets[6].pintArray[5]=1;//0100
m_Componets[6].pintArray[6]=0;
m_Componets[6].pintArray[7]=0;
m_Componets[6].pintArray[8]=0;
m_Componets[6].pintArray[9]=1;
m_Componets[6].pintArray[10]=0;
m_Componets[6].pintArray[11]=0;
m_Componets[6].pintArray[12]=0;
m_Componets[6].pintArray[13]=1;
m_Componets[6].pintArray[14]=0;
m_Componets[6].pintArray[15]=0;
}
CRusBlockView:
:
~CRusBlockView()
{
//释放内存
for(inti=0;idelete[]m_Componets[i].pintArray;
delete[]m_CurrentCom.pintArray;
}
voidCRusBlockView:
:
OnDraw(CDC*pDC)
{
CRusBlockDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:
adddrawcodefornativedatahere
//画游戏区域
CBrushbrushBK(RGB(135,197,255));
CBrush*pbrushOld=pDC->SelectObject(&brushBK);
pDC->Rectangle(LEFT-1,TOP-1,LEFT+WIDE*SIZE+1,
TOP+HIGH*SIZE+1);
//画不能移动的方块
CBrushbrushStick(RGB(127,127,127));
pDC->SelectObject(&brushStick);
for(inti=0;ifor(intj=0;jif(m_intState[i][j]==1)
pDC->Rectangle(LEFT+SIZE*j,TOP+SIZE*i,LEFT+SIZE*(j+1),
TOP+SIZE*(i+1));
//画下落的部件
if(m_CurrentCom.intComID>=0)
{
CBrushbrushCom(RGB(0,255,0));
pDC->SelectObject(&brushCom);
intintDimension=m_CurrentCom.intDimension;
for(inti=0;i{
if(m_CurrentCom.pintArray[i]==1)
{
intm=ptIndex.x+i/intDimension;//找出部件对应整体数组中的位置
intn=ptIndex.y+(i%intDimension);
pDC->Rectangle(LEFT+SIZE*n,TOP+SIZE*m,LEFT+SIZE*(n+1),
TOP+SIZE*(m+1));
}
}
}
//显示得分
CStringstrOut;
strOut.Format("得分%d",m_intScore);
pDC->TextOut(LEFT+WIDE*SIZE+50,TOP+100,strOut);
pDC->SelectObject(&pbrushOld);
}
voidCRusBlockView:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
intintDimension=m_CurrentCom.intDimension;
if(CanDown())//可以下落
{
//擦除
MyInvalidateRect(ptIndex,intDimension);
//下落
ptIndex.x++;
//显示新位置上的部件
MyInvalidateRect(ptIndex,intDimension);
}
else
{
for(inti=0;i{
if(m_CurrentCom.pintArray[i]==1)
{
intm=ptIndex.x+i/intDimension;//找出部件对应整体数组中的位置
intn=ptIndex.y+(i%intDimension);
m_intState[m][n]=1;
}
}
MyInvalidateRect(ptIndex,intDimension);
Disappear();//消去行
if(CheckFail())//游戏结束
{
m_CurrentCom.intComID=-1;
KillTimer
(1);
MessageBox("GameOver!
");
}
else
NewComponet();//新部件
}
CView:
:
OnTimer(nIDEvent);
}
voidCRusBlockView:
:
OnKeyDown(UINTnChar,UINTnRepCnt,UINTnFlags)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
intintDimension=m_CurrentCom.intDimension;
switch(nChar)//left37,right39,up38
{
case37:
if(CanLeft())
{
//擦除
MyInvalidateRect(ptIndex,intDimension);
//左移
ptIndex.y--;
//显示新位置上的部件
MyInvalidateRect(ptIndex,intDimension);
}
break;
case39:
if(CanRight())
{
//擦除
MyInvalidateRect(ptIndex,intDimension);
//右移
ptIndex.y++;
//显示新位置上的部件
MyInvalidateRect(ptIndex,intDimension);
}
break;
case38:
if(CanRotate())
{
//擦除
MyInvalidateRect(ptIndex,intDimension);
//转动
int*pintNewCom=newint[intDimension*intDimension];
for(inti=0;i{
intintR=intDimension*(intDimension-(i%intDimension)-1)+(i/intDimension);
pintNewCom[i]=m_CurrentCom.pintArray[intR];
}
for(i=0;i{
m_CurrentCom.pintArray[i]=pintNewCom[i];
}
delete[]pintNewCom;
//显示新位置上的部件
MyInvalidateRect(ptIndex,intDimension);
}
break;
}
CView:
:
OnKeyDown(nChar,nRepCnt,nFlags);
}
voidCRusBlockView:
:
NewComponet(void)
{
intintComID=rand()%7;//产生随机数
m_CurrentCom.intComID=intComID;
intintDimension=m_Componets[intComID].intDimension;
m_CurrentCom.intDimension=intDimension;
delete[]m_CurrentCom.pintArray;//删除上一个部件的内存分配
m_CurrentCom.pintArray=newint[intDimension*intDimension];
//拷贝部件
for(inti=0;im_CurrentCom.pintArray[i]=m_Componets[intComID].pintArray[i];
ptIndex.x=0;//行
ptIndex.y=5;//列
//检查是否有足够的空位显示新的部件,否则游戏结束
if(CanNew())
{
//显示该部件
MyInvalidateRect(ptIndex,intDimension);
}
else
{
m_CurrentCom.intComID=-1;
KillTimer
(1);
MessageBox("GameOver!
");
}
}
boolCRusBlockView:
:
CanDown(void)
{
boolboolDown=true;
POINTintNewIndex=ptIndex;//假设可以下落
intNewIndex.x++;
intintDimension=m_CurrentCom.intDimension;
for(inti=0;i{
if(m_CurrentCom.pintArray[i]==1)
{
intm=intNewIndex.x+i/intDimension;//找出部件对应整体数组中的位置
intn=intNewIndex.y+(i%intDimension);
if(m>=HIGH||m_intState[m][n]==1)//被挡住或出游戏区域
boolDown=false;
}
}
returnboolDown;
}
//可以左移
boolCRusBlockView:
:
CanLeft(void)
{
boolboolLeft=true;
intintDimension=m_CurrentCom.intDimension;
POINTptNewIndex=ptIndex;//假设可以左移
ptNewIndex.y--;
for(inti=0;i{
if(m_CurrentCom.pintArray[i]==1)
{
intm=ptNewIndex.x+i/intDimension;//找出部件对应整体数组中的位置
intn=ptNewIndex.y+(i%intDimension);
if(n<0||m_intState[m][n]==1)//被挡住或出游戏区域
boolLeft=false;
}
}
returnboolLeft;
}
//可以右移
boolCRusBlockView:
:
CanRight(void)
{
boolboolRight=true;
intintDimension=m_CurrentCom.intDimension;
POINTptNewIndex=ptIndex;//假设可以右移
ptNewIndex.y++;
for(inti=0;i{
if(m_CurrentCom.pintArray[i]==1)
{
intm=ptNewIndex.x+i/intDimension;//找出部件对应整体数组中的位置
intn=ptNewIndex.y+(i%intDimension);
if(n>=WIDE||m_intState[m][n]==1)//被挡住或出游戏区域
boolRight=false;
}
}
returnboolRight;
}
//可以旋转
boolCRusBlockView:
:
CanRotate(void)
{
boolboolRotate=true;
intintDimension=m_CurrentCom.intDimension;
POINTptNewIndex=ptIndex;
//假设可以转动
//新的矩阵存储转动后的部件
int*pintNewCom=newint[intDimension*intDimension