⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 自编小俄罗斯方块
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      //如果得分,则销掉此行
      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;
      //刷新游戏区域
      TRect rect1(m_iStartY, m_iStartX, m_iStartY+300, m_iStartX+360);
      InvalidateRect(Handle,&rect1,false);

      //刷新分数区域
      TRect rect2(m_iStartY+320, m_iStartX+180, m_iStartY+440, m_iStartX+200);
      InvalidateRect(Handle,&rect2,false);
    }
  }
}
//---------------------------------------------------------------------------
/* * * * * * * * * * * * * * * * * * * * * *
* 内部函数:方块是否还可以左移
* * * * * * * * * * * * * * * * * * * * * */
BOOL TForm1::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;
}
//---------------------------------------------------------------------------
/* * * * * * * * * * * * * * * * * * * * * *
* 内部函数:方块是否还可以右移
* * * * * * * * * * * * * * * * * * * * * */
BOOL TForm1::RightIsLimit()
{

  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[x4][y4+1])
            return FALSE;
    break;
  case 2:
    if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 3:
    if (GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 31:
    if (GameStatus[x1][y1+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 32:
    if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 33:
    if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 4:
    if (GameStatus[x1][y1+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 41:
    if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 5:
    if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 51:
    if (GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 6:
    if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 61:
    if (GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 62:
    if (GameStatus[x1][y1+1] || GameStatus[x2][y2+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 63:
    if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 7:
    if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 71:
    if (GameStatus[x1][y1+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 72:
    if (GameStatus[x2][y2+1] || GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  case 73:
    if (GameStatus[x3][y3+1] || GameStatus[x4][y4+1])
            return FALSE;
    break;
  }

  return TRUE;
}
//---------------------------------------------------------------------------
/* * * * * * * * * * * * * * * * * * * * * *
* 内部函数:方块的变形
* * * * * * * * * * * * * * * * * * * * * */
void TForm1::RectChange()
{
  //先预先变形,然后判断变形后的方块是否有空间,
  //如有足够空间,则进行实际变形,否则不变
  int xx1,xx2,xx3,xx4,yy1,yy2,yy3,yy4;
  int m_lscurrentRect;

  AnsiString lsStr;

  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];

  //变形后位置在数组中的存放顺序仍需遵循先左后右,在同一列中先上后下
  xx1=x1; xx2=x2; xx3=x3; xx4=x4; yy1=y1; yy2=y2; yy3=y3; yy4=y4;
  switch(m_currentRect)
  {
  case 1:
    xx1=x1+1; yy1=y1-1; xx3=x3-1; yy3=y3+1; xx4=x4-2; yy4=y4+2;
    m_lscurrentRect = 11;
    break;
  case 11:
    xx1=x1-1; yy1=y1+1; xx3=x3+1; yy3=y3-1; xx4=x4+2; yy4=y4-2;
    m_lscurrentRect = 1;
    break;
  case 2:
      m_lscurrentRect=2;
      break;
  case 3:
    xx1=x1-2; yy1=y1+1; xx4=x4-1; yy4=y4;
    m_lscurrentRect = 31;
    break;
  case 31:
    xx1=x1+1; yy1=y1-1;
    m_lscurrentRect = 32;
    break;
  case 32:
    xx1=x1+1; yy1=y1; xx4=x4+2; yy4=y4-1;
    m_lscurrentRect=33;
    break;
  case 33:
    xx4=x4-1; yy4=y4+1;
    m_lscurrentRect=3;
    break;
  case 4:
    xx1=x1+2; yy1=y1-1; xx3=x3+1; yy3=y3-1; xx4=x4-1;
    m_lscurrentRect = 41;
    break;
  case 41:
    xx1=x1-2; yy1=y1+1; xx3=x3-1; yy3=y3+1; xx4=x4+1;
    m_lscurrentRect = 4;
    break;
  case 5:
    xx1=x1-1; xx2=x2-2; yy2=y2+1; xx3=x3+1; yy4=y4+1;
    m_lscurrentRect = 51;
    break;
  case 51:
    xx1=x1+1; xx2=x2+2; yy2=y2-1; xx3=x3-1; yy4=y4-1;
    m_lscurrentRect = 5;
    break;
  case 6:
    xx2=x1+1; yy2=y2-1; xx3=x3-1; xx4=x4-2; yy4 = yy4+1;
    m_lscurrentRect = 61;
    break;
  case 61:
    xx3=x3+2; yy3=y3-1; xx4=x4+2; yy4=y4-1;
    m_lscurrentRect = 62;
    break;
  case 62:
    xx1=x1+1; yy1=y1-1; xx3=x3-2; yy3=y3+1; xx4=x4-1;
    m_lscurrentRect = 63;
    break;
  case 63:
    xx1=x1-2; yy1=y1+1; xx2=x2-2; yy2=y2+1;
    m_lscurrentRect = 6;
    break;
  case 7:
    xx3=x3-1; yy3=y3+1; xx4=x4+1; yy4=y4+1;
    m_lscurrentRect = 71;
    break;
  case 71:
    xx1=x1+2; xx2=x2-1; yy2=y2+1; xx4=x4+1; yy4=y4-1;
    m_lscurrentRect = 72;
    break;
  case 72:
    xx1=x1-2; xx3=x3-1; yy3=y3+1; xx4=x4-1; yy4=y4+1;
    m_lscurrentRect = 73;
    break;
  case 73:
    xx2=x2+1; yy2=y2-1; xx3=x3+2; yy3=y3-2; xx4=x4-1; yy4=y4-1;
    m_lscurrentRect = 7;
    break;
  }

  //如果变形后所在的区域内无其他方块,则表示有足够空间,可以变形
  //且不能超越边界
  GameStatus[x1][y1] = 0;
  GameStatus[x2][y2] = 0;
  GameStatus[x3][y3] = 0;
  GameStatus[x4][y4] = 0;
  if (GameStatus[xx1][yy1]==0 && GameStatus[xx2][yy2]==0 && GameStatus[xx3][yy3]==0 && GameStatus[xx4][yy4]==0
          && yy1>=0 && yy4<=m_iCol-1
          && !(xx1<0 || xx2<0 || xx3<0 || xx4<0)
          && !(xx1>m_iRow-1 || xx2>m_iRow-1 || xx3>m_iRow-1 || xx4>m_iRow-1) )
  {
    InvalidateCurrent();

    ActiveStatus[0][0]=xx1;
    ActiveStatus[1][0]=xx2;
    ActiveStatus[2][0]=xx3;
    ActiveStatus[3][0]=xx4;
    ActiveStatus[0][1]=yy1;
    ActiveStatus[1][1]=yy2;
    ActiveStatus[2][1]=yy3;
    ActiveStatus[3][1]=yy4;

    GameStatus[xx1][yy1] = 1;
    GameStatus[xx2][yy2] = 1;
    GameStatus[xx3][yy3] = 1;
    GameStatus[xx4][yy4] = 1;

    InvalidateCurrent();

    //改变形状代码
    m_currentRect = m_lscurrentRect;
  }
  else
  {
    GameStatus[x1][y1] = 1;
    GameStatus[x2][y2] = 1;
    GameStatus[x3][y3] = 1;
    GameStatus[x4][y4] = 1;
  }

  //判断是否已到底
  ActiveIsBottom();
}
//---------------------------------------------------------------------------
/* * * * * * * * * * * * * * * * * * * * * *
* 内部函数:刷新当前的区域
*
* 只刷新需要刷新的四个小方块区域,防止屏幕抖动情况发生
* * * * * * * * * * * * * * * * * * * * * */
void TForm1::InvalidateCurrent()
{
  int i;

  for (i=0;i<4;i++)
  {
    TRect rect(m_iStartX+ActiveStatus[i][1]*m_iLarge,  m_iStartY+ActiveStatus[i][0]*m_iLarge,
    m_iStartX+(ActiveStatus[i][1]+1)*m_iLarge+5,  m_iStartY+(ActiveStatus[i][0]+1)*m_iLarge);
    InvalidateRect(Handle,&rect,false);
  }
}
//---------------------------------------------------------------------------
//用于生成当前区域大小与级别所对应的汉字描述
void TForm1::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 TForm1::PlayMid()
{
  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, Handle);
    if (mciError != 0)
      mciSendString("close myseq",NULL,0,NULL);
  }
}
//---------------------------------------------------------------------------
//终止插放背景音乐
void TForm1::StopMid()
{
  mciSendString("close myseq",NULL,0,NULL);
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -