C++编写俄罗斯方块游戏(四)

2014-11-24 10:37:43 · 作者: · 浏览: 4
ectObject(penSquare);
dcTemp.SelectObject(GridBrush);
dcTemp.SelectObject(SquareBrush);


dcTemp.DeleteDC();
}


void CWndMain:: DrawSquBmp(CDC* pDC)
{
if (m_Start == FALSE )
{
return;
}
//方块位图
CBitmap SquareBmp;


//加载方块位图
SquareBmp.LoadBitmap(IDB_BITMAP1);

CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);


int i, j,
k=0;


// for (i=0; i<4; i++)
// {
// nx = m_nIndex[i][0];
// ny = m_nIndex[i][1];
// Russian[nx][ny] = 0;
// }
// //将小数组赋值给大数组
// for (i=0; i<4; i++)
// {
// nx = NowPosition.x + i;
// for (j=0; j<4; j++)
// {
// ny = NowPosition.y + j;
// Russian[nx][ny] = GameSquWill[i][j];
//
// if (GameSquWill[i][j] == 1)
// {
// m_nIndex[k][0] = nx;
// m_nIndex[k][1] = ny;
// k++;
// }
// }
// }


//如果有方块则显示方块,否则不显示
for ( i=0; i {
for ( j=0; j {

if (Russian[i][j] == 1 )
{
dcCompatible.SelectObject(&SquareBmp);

CRect rect;
GetClientRect(&rect);
pDC->BitBlt(j* 30+ 1, i*30 + 1, 30, 30, &dcCompatible, 0, 0, SRCCOPY);
TRACE("i = %d, j = %d\n", i, j);
}
}
}

//预先图形方块
for (int n=0; n<4; n++)
{
for (int m=0; m<4; m++)
{
if (GameSquWill[n][m] == 1)
{
dcCompatible.SelectObject(&SquareBmp);
pDC->BitBlt(450 + m*30, 50 +n*30, 30, 30, &dcCompatible, 0, 0, SRCCOPY);
TRACE("n = %d, m = %d\n", m, n);
}
}
}

}


void CWndMain::Start()
{
int i, j;


m_End = FALSE;


SetTimer(1, 500, NULL);


//清空背景数组
for (i=0; i {
for (j=0; j {
Russian[i][j] = 0;
}
}


//清空方块数组
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
GameSquare[i][j] = 0;
GameSquWill[i][j] = 0;
}
}


DrawWill();
}


void CWndMain::DrawWill()
{
int i, j, k, l;
k = 4;
l = 4;


//把将要出现的方块给当前数组,并把将要出现数组赋值为零
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
GameSquare[i][j] = GameSquWill[i][j];
GameSquWill[i][j] = 0;
}
}




// //初始化随机数种子
srand (GetTickCount());
int nTemp = rand() % Count;


//各种图形
switch(nTemp)
{
case 0:
GameSquWill[0][0] = 1;
GameSquWill[0][1] = 1;
GameSquWill[1][0] = 1;
GameSquWill[1][1] = 1;
break;
case 1:
GameSquWill[0][0] = 1;
GameSquWill[0][1] = 1;
GameSquWill[1][0] = 1;
GameSquWill[2][0] = 1;
break;
case 2:
GameSquWill[0][0] = 1;
GameSquWill[0][1] = 1;
GameSquWill[1][1] = 1;
GameSquWill[2][1] = 1;
break;
case 3:
GameSquWill[0][1] = 1;
GameSquWill[1][0] = 1;
GameSquWill[1][1] = 1;
GameSquWill[2][0] = 1;
break;
case 4:
GameSquWill[0][0] = 1;
GameSquWill[1][0] = 1;
GameSquWill[1][1] = 1;
GameSquWill[2][1] = 1;
break;
case 5:
GameSquWill[0][0] = 1;
GameSquWill[1][0] = 1;
GameSquWill[1][1] = 1;
GameSquWill[2][0] = 1;
break;
case 6:
GameSquWill[0][0] = 1;
GameSquWill[1][0] = 1;
GameSquWill[2][0] = 1;
GameSquWill[3][0] = 1;
break;


default:
ASSERT(0);
break;
}


//方块一开始的位置
NowPosition.x = 0;
NowPosition.y = m_col/2;
}


void CWndMain::OnTimer(UINT nIDEvent)
{
MoveDir(3);

this->Invalidate();

CWnd::OnTimer(nIDEvent);
}




bool CWndMain::MoveDir(int direction)
{
if (m_End)
{
return false;
}
switch(direction)
{
//左
case 1:
if (Meet(GameSquare, 1, NowPosition)) break;
NowPosition.y--;
break;
//右
case 2:
if (Meet(GameSquare, 2, NowPosition)) break;
NowPosition.y++;
break;
//下
case 3:
if (Meet(GameSquare, 3, NowPosition))
{
LineDelete();
break;
}
NowPosition.x++;
break;
//上
case 4:
Meet(GameSquare, 4, NowPosition);
break;

default:
ASSERT(0);
break;
}
return true;
}


bool CWndMain::Meet(int a[][4], int direction, CPoint p)
{
int i, j;
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
a[i][j] = GameSquWill[i][j];
}
}
//先把原位置清0
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
if (a[i