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

📄 square.c

📁 手机8032的完整代码。吐血推荐。小做改动就可以生产。
💻 C
📖 第 1 页 / 共 2 页
字号:

#define _SQUARE_C_

#pragma NOAREGS

#include "general.h"
#include "osdshow.h"

#ifdef  OSD_GAME_TETRIS
///////////////////////////////////////////////////////////////////////////////

extern void DRS232LogS(char *pcStr);
extern void DRS232LogB(BYTE bV1, BYTE bV2, BYTE bV3, BYTE bV4);

///////////////////////////////////////////////////////////////////////////////

static void vGameSQVarInit(void) large
{
  _bGameState = 0;
  _bGameSubState = 0;
  _bGameItem = 0;
  _bGameGrade = 0;
#ifdef OSD_GAME_SOUND
  _bGameSndTimer = 0;
  _bGameSndId = 0xFF;
#endif
}


static void vGameSQNewTurnInit(void) large
{
  BYTE i, j;

  _fgGameStart = FALSE;
  _fgGmTmEnb = FALSE;
  _fgSquareInit = FALSE;
  _fgDownKey = FALSE;
  _bRotate = 0;
  _bNowShape = 0;
  _bNextShape = SHP_LONG_BAR;
  _bCurLine = MAX_HEIGHT;

  _bGameSqTimer = 0 ;
  _wSqScore = 0;
  _wSqLine  = 0;

  //clear all flags
  for ( i = 0; i < MAX_WIDTH; i++)
  {
    for ( j = 0; j < MAX_HEIGHT; j++)
    {
      vSetSQMap(i, j, SHP_EMPTY);
    }
  }
}


static void vGameSQShowInit(void) large
{
  vOsdSetSt(OSD_ST_BURST);
  
  vCreateWB(WB_OSD_GAME, TRUE);

  vSetWBTcColor(WB_OSD_GAME);

  vSetTransEn(WB_OSD_GAME, 0);

  vSquareShowInfo(SQ_INFO_ALL1);

  vHiliSquareMenuItem(_bGameItem, ITEM_HILI);

  vEnableWB(WB_OSD_GAME);
}


// Get the new square type
static void vInitSquare(void) large
{
  _rNowSquarePos.y = 0;  // now square pos initial
  _rNowSquarePos.x = 7;

  _bNowShape = _bNextShape;
  _bNextShape = (_bNowShape + 1 + TL0 + TL1 + TL2) % 7;  // Random

  _bRotate = 0;
  _bSquareType = bGetSquareType(_bRotate, _bNowShape);
  _fgSquareInit = TRUE;   // indicate the next sqaure
}


static void vGameSQOsdRecover(void) large
{
  BYTE i;

  // show square map
  for ( i = 0; i < MAX_HEIGHT; i++)
  {
    vOsdDrawLine(i);
  }

  // the information show
  vSquareShowInfo(SQ_INFO_ALL1);

  // show indicate map
  if (_fgGameStart)
  {
    _fgSquareInit = TRUE;
    vOsdDrawSquare(_bSquareType);
  }
}


static BOOL fgGetValue(BYTE bGetType, BYTE bWidth) large
{
  BOOL fgGetValueEnable;
  BYTE bEmptyCount;
  BYTE bOkCount;
  BYTE i, j;

  _bMin_x = rSquareData[_bSquareType][0].x + _rNowSquarePos.x; // minx
  _bMax_x = rSquareData[_bSquareType][1].x + _rNowSquarePos.x; // maxx
  _bMin_y = rSquareData[_bSquareType][2].y + _rNowSquarePos.y; // miny
  _bMax_y = rSquareData[_bSquareType][3].y + _rNowSquarePos.y; // maxy

  for ( i = _bMin_x; i <= _bMax_x; i++)
  {
    for ( j = _bMin_y; j <= _bMax_y; j++)
    {
      if (bReadSQMap(i, j) != SHP_EMPTY)
      {
        fgGetValueEnable = FALSE;
        return fgGetValueEnable;
      }
    }
  }

  switch (bGetType)
  {
    case GET_Y_VALUE:
      bEmptyCount = 0;
      bOkCount = 0;
      if (((BYTE)(_bMin_y - (bWidth - 1))) < 0Xf0 && ((_bMin_y - (bWidth - 1) != 0)))
      {
        for (i = _bMin_y - 1; i >= _bMin_y - (bWidth - 1); i--)   // get the min_y
        {
          for (j = _bMin_x; j <= _bMax_x; j++)
          {
            if (bReadSQMap(j, i) == SHP_EMPTY)
            {
              bOkCount++;
            }
          }
          if ( (bOkCount >= rSquareSize[_bSquareType].Width)
            && (bOkCount >= rSquareSize[_bSquareType].Height) )
          {
            bEmptyCount++;
            bOkCount = 0;
          }
          else
          {
            break;
          }
        }
        _bMin_y = _bMin_y - bEmptyCount;
        if (_bMin_y > 0xf0)
        {
          _bMin_y = 0;
        }
      }

      bEmptyCount = 0;
      bOkCount = 0;                          //GET THE MAX Y
      if (_bMax_y + (bWidth - 1) <= MAX_HEIGHT - 1)
      {
        for (i = _bMax_y + 1; i <= _bMax_y + (bWidth - 1); i++)
        {
          for (j = _bMin_x; j <= _bMax_x; j++)
          {

            if (bReadSQMap(j, i) == SHP_EMPTY)
            {
              bOkCount++;
            }
          }
          if ( (bOkCount >= rSquareSize[_bSquareType].Width)
            && (bOkCount >= rSquareSize[_bSquareType].Height) )
          {
            bEmptyCount++;
            bOkCount = 0;
          }
          else
          {
            break;
          }
        }
        _bMax_y = _bMax_y + bEmptyCount;
        if (_bMax_y + 1 > MAX_HEIGHT)
        {
          _bMax_y = MAX_HEIGHT - 1;
        }
      }
      break;

    case GET_X_VALUE:
      bEmptyCount = 0;
      bOkCount = 0;
      if (((BYTE)(_bMin_x - (bWidth - 1)) < 0xf0)&&((_bMin_x - (bWidth - 1)) != 0))
      {
        for (i = _bMin_x - 1; i >= _bMin_x - (bWidth - 1); i--)
        {
          for (j = _bMin_y; j <= _bMax_y; j++)
          {

            if (bReadSQMap(i, j) == SHP_EMPTY)
            {
              bOkCount++;
            }
          }
          if ( (bOkCount >= rSquareSize[_bSquareType].Width)
            && (bOkCount >= rSquareSize[_bSquareType].Height) )
          {
            bEmptyCount++;
            bOkCount = 0;
          }
          else
          {
            break;
          }
        }
        _bMin_x = _bMin_x - bEmptyCount;
        if (_bMin_x > 0xf0)
        {
          _bMin_x = 0;
        }
      }
      bEmptyCount = 0;
      bOkCount = 0;
      if (_bMax_x + (bWidth - 1) <= MAX_WIDTH - 1)
      {
        for (i = _bMax_x + 1; i <= _bMax_x + (bWidth - 1); i++)
        {
          for (j = _bMin_y; j <= _bMax_y; j++)
          {
            if (bReadSQMap(i, j) == SHP_EMPTY)
            {
              bOkCount++;
            }
          }
          if ( (bOkCount >= rSquareSize[_bSquareType].Width)
            && (bOkCount >= rSquareSize[_bSquareType].Height) )
          {
            bEmptyCount++;
            bOkCount = 0;
          }
          else
          {
            break;
          }
        }
        _bMax_x = _bMax_x + bEmptyCount;
        if (_bMax_x + 1 > MAX_WIDTH)
        {
          _bMax_x = MAX_WIDTH - 1;
        }
      }
      else
      {
        _bMax_x = MAX_WIDTH - 1;
      }
      break;
  }

  fgGetValueEnable = TRUE;
  return fgGetValueEnable;
}


static BOOL fgGetyValue(void) large
{
  BOOL fgGetyValueEnable = FALSE;

  if (fgGetValue(GET_Y_VALUE, 2))
  {
    if (_bMax_y - _bMin_y >= 2)
    {
      if (_bMax_y - _rNowSquarePos.y <= 1)
      {
        _rNowSquarePos.y = _bMax_y - 2;
      }
      else
      {
        //rNowSquarePos.y = bMin_y;
      }
      fgGetyValueEnable = TRUE;
    }
  }
  else
  {
    fgGetyValueEnable = FALSE;
  }

  return   fgGetyValueEnable;
}


static BOOL fgGetxValue(void) large
{
  BOOL fgGetxValudeEnble;

  if (fgGetValue(GET_X_VALUE, 2))
  {
    if (_bMax_x - _bMin_x >= 2)
    {
      if ((_bMax_x - _rNowSquarePos.x) <= 1)
      {
        _rNowSquarePos.x = _bMax_x - 2;
      }
      else
      {
        //rNowSquarePos.x = bMin_x; //??
      }
      fgGetxValudeEnble = TRUE;
    }
  }
  else
  {
    fgGetxValudeEnble = FALSE;

  }

  return  fgGetxValudeEnble;
}


static BOOL fgJudgeRotate(void) large
{
  BOOL fgRotateEnable = FALSE;

  switch (_bNowShape)
  {
    case SHP_LONG_BAR:
      switch (_bRotate % 2)
      {
        case 0://from"--"to''|''
          if (fgGetValue(GET_Y_VALUE, 4))
          {
            if (_bMax_y - _bMin_y >= 3)
            {
              if (_bMax_y - _rNowSquarePos.y <= 3)
              {
                _rNowSquarePos.y = _bMax_y - 3;
              }
              else
              {
                _rNowSquarePos.y = _bMin_y;
              }
              _rNowSquarePos.x += 2;
              fgRotateEnable = TRUE;
            }
          }
          break;
        case 1:
          if (fgGetValue(GET_X_VALUE, 4))
          {
            if (_bMax_x - _bMin_x >= 3)
            {
              if ((_bMax_x == MAX_WIDTH - 1) && (_rNowSquarePos.x <= MAX_WIDTH - 1 - 3))
              {
                _rNowSquarePos.x -= 2;
                fgRotateEnable = TRUE;
                return fgRotateEnable;
              }
              if (_bMax_x - _rNowSquarePos.x <= 3)
              {
                _rNowSquarePos.x = _bMax_x - 3;
              }
              else
              {
                _rNowSquarePos.x = _bMin_x;
              }
              if (!(_bMax_x == MAX_WIDTH - 1))
              {
                _rNowSquarePos.x -= 2;
                if (_rNowSquarePos.x > 0xf0)
                {
                  _rNowSquarePos.x = 0;
                }
              }

              fgRotateEnable = TRUE;
            }
          }
          break;
      }
      break;

    case SHP_TWO:
    case SHP_RE_TWO:
      switch (_bRotate % 2)
      {
        case 0:
          fgRotateEnable = fgGetyValue();
          break;
        case 1:
          fgRotateEnable = fgGetxValue();
          break;
      }
      break;

    case SHP_SEVEN:
    case SHP_RE_SEVEN:
      switch (_bRotate % 4)
      {
        case 0:
        case 2:
          fgRotateEnable = fgGetxValue();
          break;
        case 1:
        case 3:
          fgRotateEnable = fgGetyValue();
          break;
      }
      break;

    case SHP_T:
      switch (_bRotate % 4)
      {
        case 0:
        case 2:
          fgRotateEnable = fgGetyValue();
          break;
        case 1:
        case 3:
          fgRotateEnable = fgGetxValue();
          break;
      }
      break;
  }

  return  fgRotateEnable;
}


static BOOL fgCheckLine(BYTE bLine) large
{
  BYTE i;
  
  for (i = 0; i < MAX_WIDTH; i++)
  {
    if (bReadSQMap(i, bLine) == SHP_EMPTY)
    {
      return FALSE;
    }
  }

  return TRUE;
}


static void vPicDrop(BYTE bLineNo) large
{
  BYTE i, j;

  for (i = bLineNo; i > 0; i--)
  {
    for (j = 0; j < MAX_WIDTH; j++)
    {
      vSetSQMap(j, i, bReadSQMap(j, i - 1));
    }
    vOsdDrawLine(i);
  }
  vOsdClearLine(0);
}


static void vJudgeFull(void) large
{
  BOOL fgFull = FALSE;
  BOOL fgClearLine = FALSE;
  WORD wScoreDataBuff = 0;
  BYTE bLinesDataBuff = 0;
  BYTE bClearLine = 0;
  BYTE i;

  for (i = rSquareSize[_bSquareType].Height; i > 0; i--)
  {
    if (fgCheckLine(rSquareData[_bSquareType][3].y + _rNowSquarePos.y - bClearLine))
    {
      fgClearLine = TRUE;
      //caculate the score as the grade of the cleared line
      bLinesDataBuff++;
      //the above picture will drop down for one grid
      vPicDrop(rSquareData[_bSquareType][3].y + _rNowSquarePos.y - bClearLine);
    }
    else
    {
      fgClearLine = FALSE;
    }
    if (!fgClearLine)
    {
      bClearLine++;
    }
  }

  if(bLinesDataBuff != 0)
  {
    // the more lines deleted the heigher the score increase
    wScoreDataBuff = bScoreLine[bLinesDataBuff];
    wScoreDataBuff *= (_bGameGrade + 1);
    _wSqScore += wScoreDataBuff;  
    _wSqLine += bLinesDataBuff;
    //show the osd information
    vSquareShowInfo(SQ_INFO_SCORE | SQ_INFO_LINE);
  #ifdef OSD_GAME_SOUND
    if (_bGameSndTimer == 0)
    {
      vGameDelayMs(10);
      vSendUopCmd(UOP_AUDIO_CLIP, SV_ACLIP_FLASH, CLIP_DISAPPEAR, 0);
      _bGameSndId = 0xFF;
      _bGameSndTimer = 15;
    }
  #endif
  }

  vOsdClrIndiSquare();
  vInitSquare(); // initle next picture
}


static void vJudgeY(void) large
{
  BYTE i, j;

  for (i = 0; i < 4; i++)
  {
    if (rSquareData[_bSquareType][i].fgJudge)
    {
      if ( (_fgDownKey)
        && ( (bReadSQMap(rSquareData[_bSquareType][i].x + _rNowSquarePos.x,
                         rSquareData[_bSquareType][i].y + _rNowSquarePos.y + 2) != SHP_EMPTY)
          || (rSquareData[_bSquareType][i].y + _rNowSquarePos.y + 2 == MAX_HEIGHT) ) )
      {
        _fgDownKey = FALSE;
        vOsdDrawSquare(_bSquareType);
      }
      if ( (bReadSQMap(rSquareData[_bSquareType][i].x + _rNowSquarePos.x,
                       rSquareData[_bSquareType][i].y + _rNowSquarePos.y + 1) != SHP_EMPTY)
        || (rSquareData[_bSquareType][i].y + _rNowSquarePos.y + 1 == MAX_HEIGHT) )
      {
        for ( j = 0; j < 4; j++)
        {
          vSetSQMap(rSquareData[_bSquareType][j].x + _rNowSquarePos.x,
                    rSquareData[_bSquareType][j].y + _rNowSquarePos.y, _bNowShape);

          if ((rSquareData[_bSquareType][j].y + _rNowSquarePos.y) < _bCurLine  )
          {
            _bCurLine = rSquareData[_bSquareType][j].y + _rNowSquarePos.y;
          }

⌨️ 快捷键说明

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