📄 hwrectview.cpp
字号:
BOOL CHWRECTView::LeftIsLimit()
{
int x1,x2,x3,x4,y1,y2,y3,y4;
x1 = ActiveStatus[0][0];
x2 = ActiveStatus[1][0];
x3 = ActiveStatus[2][0];
x4 = ActiveStatus[3][0];
y1 = ActiveStatus[0][1];
y2 = ActiveStatus[1][1];
y3 = ActiveStatus[2][1];
y4 = ActiveStatus[3][1];
switch(m_currentRect)
{
case 1:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x3][y3-1] || GameStatus[x4][y4-1])
return FALSE;
break;
case 11:
if (GameStatus[x1][y1-1])
return FALSE;
break;
case 2:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1])
return FALSE;
break;
case 3:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1])
return FALSE;
break;
case 31:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 32:
if (GameStatus[x1][y1-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 33:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x4][y4-1])
return FALSE;
break;
case 4:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x4][y4-1])
return FALSE;
break;
case 41:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1])
return FALSE;
break;
case 5:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 51:
if (GameStatus[x1][y1-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 6:
if (GameStatus[x1][y1-1] || GameStatus[x3][y3-1] || GameStatus[x4][y4-1])
return FALSE;
break;
case 61:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1])
return FALSE;
break;
case 62:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 63:
if (GameStatus[x1][y1-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 7:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 71:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1])
return FALSE;
break;
case 72:
if (GameStatus[x1][y1-1] || GameStatus[x2][y2-1] || GameStatus[x3][y3-1])
return FALSE;
break;
case 73:
if (GameStatus[x1][y1-1] || GameStatus[x4][y4-1])
return FALSE;
break;
}
return TRUE;
}
void CHWRECTView::ActiveIsBottom()
{
//到底有两种概念:1是已到底部,2是下面碰到了另外的方块
int x1,x2,x3,x4;
int x,xx,yy,i;
x1 = ActiveStatus[0][0];
x2 = ActiveStatus[1][0];
x3 = ActiveStatus[2][0];
x4 = ActiveStatus[3][0];
//已到底
//如果下面碰到了另外的方块
//一个方块分为四个小方块,只有这样的小方块下面有另外的方块才算到底,这样的小方块满足条件如下:
//它的下面不再有本方块的另外的小方块
//用算法描述为:y坐标相等的一组小方块中的最下面的哪个小方块, 我称之为接触面
if (x1>=m_iRow-1 || x2>=m_iRow-1 || x3>=m_iRow-1 || x4>=m_iRow-1)
m_isBottom = TRUE;
else
{
for (i=0;i<4;i++)
{
if (InterFace[m_currentRect][i] > -1)
{
x=InterFace[m_currentRect][i];
xx=ActiveStatus[x][0]+1;
yy=ActiveStatus[x][1];
if (GameStatus[xx][yy]==1)
m_isBottom = TRUE;
}
}
}
BOOL m_bIsSucced;
int k,j;
int m_iMuch=0; //本次销掉的行数
//计分规则:一次销掉一行,加100分,一次销掉两行,加400分,三行,900分
//例如销掉x行,则分数为:x*(x*100)
if (m_isBottom)
{
//判断是否已得分
for (i=0;i<m_iRow;i++)
{
m_bIsSucced = TRUE;
for (j=0;j<m_iCol;j++)
if (GameStatus[i][j]==0)
m_bIsSucced = FALSE;
//如果得分,则销掉此行
if (m_bIsSucced)
{
for (k=i;k>0;k--)
for (j=0;j<m_iCol;j++)
GameStatus[k][j] = GameStatus[k-1][j];
//第1行清零
for (j=0;j<m_iCol;j++)
GameStatus[0][j]=0;
m_iMuch += 1;
}
}
if (m_iMuch>0)
{
m_iPerformance += m_iMuch * m_iMuch * 100;
//刷新游戏区域
CRect rect1(m_iStartY, m_iStartX, m_iStartY+300, m_iStartX+360);
InvalidateRect(&rect1);
//刷新分数区域
CRect rect2(m_iStartY+320, m_iStartX+180, m_iStartY+440, m_iStartX+200);
InvalidateRect(&rect2);
}
}
}
void CHWRECTView::RectDown()
{
ActiveIsBottom();
if (!m_isBottom)
{
//清除以前的方块
int x1,x2,x3,x4,y1,y2,y3,y4;
x1 = ActiveStatus[0][0];
x2 = ActiveStatus[1][0];
x3 = ActiveStatus[2][0];
x4 = ActiveStatus[3][0];
y1 = ActiveStatus[0][1];
y2 = ActiveStatus[1][1];
y3 = ActiveStatus[2][1];
y4 = ActiveStatus[3][1];
GameStatus[x1][y1]=0;
GameStatus[x2][y2]=0;
GameStatus[x3][y3]=0;
GameStatus[x4][y4]=0;
InvalidateCurrent();
//方块下落
ActiveStatus[0][0] += 1;
ActiveStatus[1][0] += 1;
ActiveStatus[2][0] += 1;
ActiveStatus[3][0] += 1;
GameStatus[x1+1][y1]=1;
GameStatus[x2+1][y2]=1;
GameStatus[x3+1][y3]=1;
GameStatus[x4+1][y4]=1;
InvalidateCurrent();
}
}
void CHWRECTView::RectArrow(int m_Type)
{
//清除以前的方块
int x1,x2,x3,x4,y1,y2,y3,y4;
x1 = ActiveStatus[0][0];
x2 = ActiveStatus[1][0];
x3 = ActiveStatus[2][0];
x4 = ActiveStatus[3][0];
y1 = ActiveStatus[0][1];
y2 = ActiveStatus[1][1];
y3 = ActiveStatus[2][1];
y4 = ActiveStatus[3][1];
//如果方块已到底则不能再移动
switch(m_Type)
{
case LEFT:
if ( (ActiveStatus[0][1]>0) && LeftIsLimit() && !m_isBottom)
{
//清原来的方块
GameStatus[x1][y1]=0;
GameStatus[x2][y2]=0;
GameStatus[x3][y3]=0;
GameStatus[x4][y4]=0;
InvalidateCurrent();
ActiveStatus[0][1] -= 1;
ActiveStatus[1][1] -= 1;
ActiveStatus[2][1] -= 1;
ActiveStatus[3][1] -= 1;
GameStatus[x1][y1-1]=1;
GameStatus[x2][y2-1]=1;
GameStatus[x3][y3-1]=1;
GameStatus[x4][y4-1]=1;
InvalidateCurrent();
}
break;
case RIGHT:
if ( (ActiveStatus[3][1]< m_iCol-1) && RightIsLimit() && !m_isBottom)
{
//清原来的方块
GameStatus[x1][y1]=0;
GameStatus[x2][y2]=0;
GameStatus[x3][y3]=0;
GameStatus[x4][y4]=0;
InvalidateCurrent();
ActiveStatus[0][1] += 1;
ActiveStatus[1][1] += 1;
ActiveStatus[2][1] += 1;
ActiveStatus[3][1] += 1;
GameStatus[x1][y1+1]=1;
GameStatus[x2][y2+1]=1;
GameStatus[x3][y3+1]=1;
GameStatus[x4][y4+1]=1;
InvalidateCurrent();
}
break;
case DOWN:
//顺序下降,每降一级判断是否已到底
RectDown();
break;
}
}
int CHWRECTView::Random(int MaxNumber)
{
int currentClock = (int)clock();
srand(currentClock);
return (rand() % MaxNumber);
}
void CHWRECTView::RectStatusToActiveStatus(int m_which)
{
switch(m_which)
{
case 1:
ActiveStatus[0][0] = 0; ActiveStatus[0][1] = 5; ActiveStatus[1][0] = 1; ActiveStatus[1][1] = 5;
ActiveStatus[2][0] = 2; ActiveStatus[2][1] = 5; ActiveStatus[3][0] = 3; ActiveStatus[3][1] = 5;
break;
case 2:
ActiveStatus[0][0] = 0; ActiveStatus[0][1] = 5; ActiveStatus[1][0] = 1; ActiveStatus[1][1] = 5;
ActiveStatus[2][0] = 0; ActiveStatus[2][1] = 6; ActiveStatus[3][0] = 1; ActiveStatus[3][1] = 6;
break;
case 3:
ActiveStatus[0][0] = 1; ActiveStatus[0][1] = 4; ActiveStatus[1][0] = 0; ActiveStatus[1][1] = 5;
ActiveStatus[2][0] = 1; ActiveStatus[2][1] = 5; ActiveStatus[3][0] = 1; ActiveStatus[3][1] = 6;
break;
case 4:
ActiveStatus[0][0] = 0; ActiveStatus[0][1] = 5; ActiveStatus[1][0] = 1; ActiveStatus[1][1] = 5;
ActiveStatus[2][0] = 1; ActiveStatus[2][1] = 6; ActiveStatus[3][0] = 2; ActiveStatus[3][1] = 6;
break;
case 5:
ActiveStatus[0][0] = 1; ActiveStatus[0][1] = 5; ActiveStatus[1][0] = 2; ActiveStatus[1][1] = 5;
ActiveStatus[2][0] = 0; ActiveStatus[2][1] = 6; ActiveStatus[3][0] = 1; ActiveStatus[3][1] = 6;
break;
case 6:
ActiveStatus[0][0] = 0; ActiveStatus[0][1] = 5; ActiveStatus[1][0] = 0; ActiveStatus[1][1] = 6;
ActiveStatus[2][0] = 1; ActiveStatus[2][1] = 6; ActiveStatus[3][0] = 2; ActiveStatus[3][1] = 6;
break;
case 7:
ActiveStatus[0][0] = 0; ActiveStatus[0][1] = 5; ActiveStatus[1][0] = 1; ActiveStatus[1][1] = 5;
ActiveStatus[2][0] = 2; ActiveStatus[2][1] = 5; ActiveStatus[3][0] = 0; ActiveStatus[3][1] = 6;
break;
}
}
void CHWRECTView::RectStatusToNextStatus(int m_which)
{
switch(m_which)
{
case 1:
NextStatus[0][0] = 0; NextStatus[0][1] = 1; NextStatus[1][0] = 1; NextStatus[1][1] = 1;
NextStatus[2][0] = 2; NextStatus[2][1] = 1; NextStatus[3][0] = 3; NextStatus[3][1] = 1;
break;
case 2:
NextStatus[0][0] = 0; NextStatus[0][1] = 1; NextStatus[1][0] = 1; NextStatus[1][1] = 1;
NextStatus[2][0] = 0; NextStatus[2][1] = 2; NextStatus[3][0] = 1; NextStatus[3][1] = 2;
break;
case 3:
NextStatus[0][0] = 1; NextStatus[0][1] = 0; NextStatus[1][0] = 0; NextStatus[1][1] = 1;
NextStatus[2][0] = 1; NextStatus[2][1] = 1; NextStatus[3][0] = 1; NextStatus[3][1] = 2;
break;
case 4:
NextStatus[0][0] = 0; NextStatus[0][1] = 1; NextStatus[1][0] = 1; NextStatus[1][1] = 1;
NextStatus[2][0] = 1; NextStatus[2][1] = 2; NextStatus[3][0] = 2; NextStatus[3][1] = 2;
break;
case 5:
NextStatus[0][0] = 1; NextStatus[0][1] = 1; NextStatus[1][0] = 2; NextStatus[1][1] = 1;
NextStatus[2][0] = 0; NextStatus[2][1] = 2; NextStatus[3][0] = 1; NextStatus[3][1] = 2;
break;
case 6:
NextStatus[0][0] = 0; NextStatus[0][1] = 1; NextStatus[1][0] = 0; NextStatus[1][1] = 2;
NextStatus[2][0] = 1; NextStatus[2][1] = 2; NextStatus[3][0] = 2; NextStatus[3][1] = 2;
break;
case 7:
NextStatus[0][0] = 0; NextStatus[0][1] = 1; NextStatus[1][0] = 1; NextStatus[1][1] = 1;
NextStatus[2][0] = 2; NextStatus[2][1] = 1; NextStatus[3][0] = 0; NextStatus[3][1] = 2;
break;
}
}
void CHWRECTView::ActiveStatusToGameStatus()
{
int x1,x2,x3,x4,y1,y2,y3,y4;
x1 = ActiveStatus[0][0];
x2 = ActiveStatus[1][0];
x3 = ActiveStatus[2][0];
x4 = ActiveStatus[3][0];
y1 = ActiveStatus[0][1];
y2 = ActiveStatus[1][1];
y3 = ActiveStatus[2][1];
y4 = ActiveStatus[3][1];
GameStatus[x1][y1]=1;
GameStatus[x2][y2]=1;
GameStatus[x3][y3]=1;
GameStatus[x4][y4]=1;
}
void CHWRECTView::StopMid()
{
mciSendString("close myseq",NULL,0,NULL);
}
void CHWRECTView::PlayMid()
{
HWND hWnd;
hWnd = GetSafeHwnd();
char inBuf[300],outBuf[60],fileName[255];
MCIERROR mciError;
strcpy(fileName,"hwrect.mid");
wsprintf( inBuf,"open %s type sequencer alias myseq",fileName);
mciError = mciSendString( inBuf, outBuf, sizeof(outBuf), NULL);
if (mciError == 0)
{
mciError = mciSendString("play myseq notify",NULL,0, hWnd);
if (mciError != 0)
mciSendString("close myseq",NULL,0,NULL);
}
}
void CHWRECTView::CurrentAreaAndLevel()
{
switch(m_iRow)
{
case 12:
m_strArea = "12行10列"; break;
case 18:
m_strArea = "18行15列"; break;
case 24:
m_strArea = "24行20列"; break;
case 30:
m_strArea = "30行25列"; break;
}
switch(m_iLevel)
{
case 0:
m_strLevel = "第一级: 蜗牛级"; break;
case 1:
m_strLevel = "第二级: 笨鸭级"; break;
case 2:
m_strLevel = "第三级: 雄鸡级"; break;
case 3:
m_strLevel = "第四级: 猎狗级"; break;
case 4:
m_strLevel = "第五级: 云豹级"; break;
case 5:
m_strLevel = "第六级: 飞鹰级"; break;
}
}
void CHWRECTView::OnGameStart()
{
// TODO: Add your command handler code here
if (!m_bGamePaush) //如果不是游戏暂停状态,则必须作些初始工作
{
m_bGameEnd = FALSE;
//总分值清零, 并显示总分记分牌
m_iPerformance = 0;
//显示当前的区域及游戏级别的汉字描述
CurrentAreaAndLevel();
CRect rect(m_iStartY, m_iStartX, m_iStartY+440, m_iStartX+370);
InvalidateRect(&rect);
}
m_bGamePaush = FALSE;
SetTimer(1,1500-250*m_iLevel,NULL);
}
void CHWRECTView::OnGamePaush()
{
// TODO: Add your command handler code here
m_bGamePaush = TRUE;
KillTimer(1);
}
void CHWRECTView::OnGameEnd()
{
// TODO: Add your command handler code here
m_bGameEnd = TRUE;
int i,j;
for (i=0;i<m_iRow;i++)
for (j=0;j<m_iCol;j++)
GameStatus[i][j]=0;
CRect rect(m_iStartY, m_iStartX, m_iStartY+440, m_iStartX+370);
InvalidateRect(&rect);
m_bGamePaush = FALSE; //清除游戏暂停状态
KillTimer(1);
}
void CHWRECTView::OnGameOption()
{
// TODO: Add your command handler code here
//参数顺序: 区域大小代码:0-3,分别为:12X10,18X15,24X20,m_iLargeX25
//级别:0-5,分别为:1500,1200,1000,800,600,400
//背景音乐:TRUE 或者 FALSE
int m_lsArea;
switch(m_iRow)
{
case 12:
m_lsArea = 0;
break;
case 18:
m_lsArea = 1;
break;
case 24:
m_lsArea = 2;
break;
case 30:
m_lsArea = 3;
break;
}
COptionDlg dlg(m_lsArea,m_iLevel,m_bMusic,m_bDrawGrid);
if (dlg.DoModal()==IDOK)
{
//确定区域的大小
switch(dlg.m_iArea)
{
case 0:
m_iRow = 12;
m_iCol = 10;
m_iLarge = 30;
break;
case 1:
m_iRow = 18;
m_iCol = 15;
m_iLarge = 20;
break;
case 2:
m_iRow = 24;
m_iCol = 20;
m_iLarge = 15;
break;
case 3:
m_iRow = 30;
m_iCol = 25;
m_iLarge = 12;
break;
}
//确定级别
m_iLevel = dlg.m_iLevel;
//确定是否绘网格背景
m_bDrawGrid = dlg.m_bDrawGrid;
//检查是否插放音乐
m_bMusic = dlg.m_bMusic;
if (m_bMusic)
PlayMid();
else
StopMid();
Invalidate();
}
}
void CHWRECTView::OnGameExit()
{
// TODO: Add your command handler code here
//if (m_bGameEnd)
// pCmdUI->Enable(TRUE);
// else
// pCmdUI->Enable(FALSE);
exit(1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -