C 언어 소스 코드 는 러시아 블록 을 실현 한다.

소개 하 다.
러시아 블록(Tetris,러시아어:Тетрис)텔레비전 게임 기와 핸드 게임 기 게임 으로 러시아인 알 렉 스 파 키 트 노 프 가 발명 하여 이 이름 을 얻 었 다.러시아 사각형 의 기본 규칙 은 게임 이 자동 으로 출력 하 는 각종 사각형 을 이동,회전,배치 하여 완전한 한 줄 또는 여러 줄 로 배열 하고 점 수 를 없 애 는 것 이다.손 이 간단 하고 노소 가 모두 적합 하기 때문에 누구나 다 알 고 세 계 를 풍미 했다.
소스 코드

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>

#ifdef _MSC_VER // M$           
 #if _MSC_VER <= 1200 // VC6     
 #error        VC6?!
 #else // VC6    
 #if _MSC_VER >= 1600 //   VC10      stdint.h 
  #include <stdint.h>
 #else // VC10    ,    int8_t uint16_t
  typedef signed char int8_t;
  typedef unsigned short uint16_t;
 #endif
 #ifndef __cplusplus //   VC   stdbool.h,  C++  ,    bool
  typedef int bool;
  #define true 1
  #define false 0
 #endif
 #endif
#else //          
 #include <stdint.h>
 #ifndef __cplusplus //   C++  ,  stdbool.h  bool
 #include <stdbool.h>
 #endif
#endif

// =============================================================================
// 7    4    (4    )
static const uint16_t gs_uTetrisTable[7][4] =
{
 { 0x00F0U, 0x2222U, 0x00F0U, 0x2222U }, // I 
 { 0x0072U, 0x0262U, 0x0270U, 0x0232U }, // T 
 { 0x0223U, 0x0074U, 0x0622U, 0x0170U }, // L 
 { 0x0226U, 0x0470U, 0x0322U, 0x0071U }, // J 
 { 0x0063U, 0x0264U, 0x0063U, 0x0264U }, // Z 
 { 0x006CU, 0x0462U, 0x006CU, 0x0462U }, // S 
 { 0x0660U, 0x0660U, 0x0660U, 0x0660U } // O 
};

// =============================================================================
//         
//             ,          
//     2 1,  2   1,        
//            12 
//        10 ,      1  (0xE007),              
//       0xFFFFU ,        
//   4      ,     
//      2 ,           22 
static const uint16_t gs_uInitialTetrisPool[28] =
{
 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xFFFFU, 0xFFFFU
};

#define COL_BEGIN 2
#define COL_END 14
#define ROW_BEGIN 4
#define ROW_END 26

// =============================================================================
typedef struct TetrisManager //              
{
 uint16_t pool[28]; //    
 int8_t x; //     x  ,            
 int8_t y; //     y  
 int8_t type[3]; //   、            
 int8_t orientation[3]; //   、              
 unsigned score; //   
 unsigned erasedCount[4]; //    
 unsigned erasedTotal; //     
 unsigned tetrisCount[7]; //     
 unsigned tetrisTotal; //     
 bool dead; //  
} TetrisManager;

// =============================================================================
typedef struct TetrisControl //              
{
 bool pause; //   
 bool clockwise; //     :    true
 int8_t direction; //     :0     1    
 //          
 //          ,               
 //   ,         ,          
 int8_t color[28][16];
} TetrisControl;

HANDLE g_hConsoleOutput; //        

// =============================================================================
//     
//             ,       
void initGame(TetrisManager *manager, TetrisControl *control); //      
void restartGame(TetrisManager *manager, TetrisControl *control); //       
void giveTetris(TetrisManager *manager); //      
bool checkCollision(const TetrisManager *manager); //     
void insertTetris(TetrisManager *manager); //     
void removeTetris(TetrisManager *manager); //     
void horzMoveTetris(TetrisManager *manager, TetrisControl *control); //       
void moveDownTetris(TetrisManager *manager, TetrisControl *control); //       
void rotateTetris(TetrisManager *manager, TetrisControl *control); //     
void dropDownTetris(TetrisManager *manager, TetrisControl *control); //       
bool checkErasing(TetrisManager *manager, TetrisControl *control); //     
void keydownControl(TetrisManager *manager, TetrisControl *control, int key); //    
void setPoolColor(const TetrisManager *manager, TetrisControl *control); //     
void gotoxyWithFullwidth(short x, short y); //      
void printPoolBorder(); //        
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control); //      
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control); //       
void printNextTetris(const TetrisManager *manager); //             
void printScore(const TetrisManager *manager); //       
void runGame(TetrisManager *manager, TetrisControl *control); //     
void printPrompting(); //       
bool ifPlayAgain(); //     

// =============================================================================
//    
int main()
{
 TetrisManager tetrisManager;
 TetrisControl tetrisControl;

 initGame(&tetrisManager, &tetrisControl); //      
 do
 {
 printPrompting(); //       
 printPoolBorder(); //        
 runGame(&tetrisManager, &tetrisControl); //     
 if (ifPlayAgain()) //     
 {
  SetConsoleTextAttribute(g_hConsoleOutput, 0x7);
  system("cls"); //   
  restartGame(&tetrisManager, &tetrisControl); //       
 }
 else
 {
  break;
 }
 } while (1);
 gotoxyWithFullwidth(0, 0);
 CloseHandle(g_hConsoleOutput);
 return 0;
}

// =============================================================================
//      
void initGame(TetrisManager *manager, TetrisControl *control)
{
 CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE }; //     

 g_hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); //          
 SetConsoleCursorInfo(g_hConsoleOutput, &cursorInfo); //       
 SetConsoleTitleA("         ――By: NEWPLAN");

 restartGame(manager, control);
}

// =============================================================================
//       
void restartGame(TetrisManager *manager, TetrisControl *control)
{
 memset(manager, 0, sizeof(TetrisManager)); //    0

 //       
 memcpy(manager->pool, gs_uInitialTetrisPool, sizeof(uint16_t [28]));
 srand((unsigned)time(NULL)); //       

 manager->type[1] = rand() % 7; //    
 manager->orientation[1] = rand() & 3;

 manager->type[2] = rand() % 7; //     
 manager->orientation[2] = rand() & 3;

 memset(control, 0, sizeof(TetrisControl)); //    0

 giveTetris(manager); //       
 setPoolColor(manager, control); //     
}

// =============================================================================
//      
void giveTetris(TetrisManager *manager)
{
 uint16_t tetris;

 manager->type[0] = manager->type[1]; //          
 manager->orientation[0] = manager->orientation[1];

 manager->type[1] = manager->type[2];//            
 manager->orientation[1] = manager->orientation[2];

 manager->type[2] = rand() % 7;//           
 manager->orientation[2] = rand() & 3;

 tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]]; //     

 //       y  ,                
 //                              4  
 if (tetris & 0xF000)
 {
 manager->y = 0;
 }
 else
 {
 manager->y = (tetris & 0xFF00) ? 1 : 2;
 }
 manager->x = 6; //       x  

 if (checkCollision(manager)) //      
 {
 manager->dead = true; //       
 }
 else //       
 {
 insertTetris(manager); //           
 }

 ++manager->tetrisTotal; //     
 ++manager->tetrisCount[manager->type[0]]; //      

 printNextTetris(manager); //        
 printScore(manager); //       
}

// =============================================================================
//     
bool checkCollision(const TetrisManager *manager)
{
 //     
 uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
 uint16_t dest = 0;

 //               :
 //      x y      ,       16      
 dest |= (((manager->pool[manager->y + 0] >> manager->x) << 0x0) & 0x000F);
 dest |= (((manager->pool[manager->y + 1] >> manager->x) << 0x4) & 0x00F0);
 dest |= (((manager->pool[manager->y + 2] >> manager->x) << 0x8) & 0x0F00);
 dest |= (((manager->pool[manager->y + 3] >> manager->x) << 0xC) & 0xF000);

 //               (  ),        0
 return ((dest & tetris) != 0);
}

// =============================================================================
//     
void insertTetris(TetrisManager *manager)
{
 //     
 uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];

 //      4   ,          ,       
 manager->pool[manager->y + 0] |= (((tetris >> 0x0) & 0x000F) << manager->x);
 manager->pool[manager->y + 1] |= (((tetris >> 0x4) & 0x000F) << manager->x);
 manager->pool[manager->y + 2] |= (((tetris >> 0x8) & 0x000F) << manager->x);
 manager->pool[manager->y + 3] |= (((tetris >> 0xC) & 0x000F) << manager->x);
}

// =============================================================================
//     
void removeTetris(TetrisManager *manager)
{
 //     
 uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];

 //      4   ,               ,       
 manager->pool[manager->y + 0] &= ~(((tetris >> 0x0) & 0x000F) << manager->x);
 manager->pool[manager->y + 1] &= ~(((tetris >> 0x4) & 0x000F) << manager->x);
 manager->pool[manager->y + 2] &= ~(((tetris >> 0x8) & 0x000F) << manager->x);
 manager->pool[manager->y + 3] &= ~(((tetris >> 0xC) & 0x000F) << manager->x);
}

// =============================================================================
//     
void setPoolColor(const TetrisManager *manager, TetrisControl *control)
{
 //         ,                          
 //         ,     
 //               

 int8_t i, x, y;

 //     
 uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];

 for (i = 0; i < 16; ++i)
 {
 y = (i >> 2) + manager->y; //      
 if (y > ROW_END) //       
 {
  break;
 }
 x = (i & 3) + manager->x; //      
 if ((tetris >> i) & 1) //                
 {
  control->color[y][x] = (manager->type[0] | 8); //     
 }
 }
}

// =============================================================================
//     
void rotateTetris(TetrisManager *manager, TetrisControl *control)
{
 int8_t ori = manager->orientation[0]; //        

 removeTetris(manager); //       

 //  /     
 manager->orientation[0] = (control->clockwise) ? ((ori + 1) & 3) : ((ori + 3) & 3);

 if (checkCollision(manager)) //      
 {
 manager->orientation[0] = ori; //         
 insertTetris(manager); //       。       ,       
 }
 else
 {
 insertTetris(manager); //       
 setPoolColor(manager, control); //     
 printCurrentTetris(manager, control); //       
 }
}

// =============================================================================
//       
void horzMoveTetris(TetrisManager *manager, TetrisControl *control)
{
 int x = manager->x; //       

 removeTetris(manager); //       
 control->direction == 0 ? (--manager->x) : (++manager->x); //  /   

 if (checkCollision(manager)) //      
 {
 manager->x = x; //        
 insertTetris(manager); //       。       ,       
 }
 else
 {
 insertTetris(manager); //       
 setPoolColor(manager, control); //     
 printCurrentTetris(manager, control); //       
 }
}

// =============================================================================
//       
void moveDownTetris(TetrisManager *manager, TetrisControl *control)
{
 int8_t y = manager->y; //       

 removeTetris(manager); //       
 ++manager->y; //     

 if (checkCollision(manager)) //      
 {
 manager->y = y; //        
 insertTetris(manager); //       。       ,       
 if (checkErasing(manager, control)) //      
 {
  printTetrisPool(manager, control); //      
 }
 }
 else
 {
 insertTetris(manager); //       
 setPoolColor(manager, control); //     
 printCurrentTetris(manager, control); //       
 }
}

// =============================================================================
//       
void dropDownTetris(TetrisManager *manager, TetrisControl *control)
{
 removeTetris(manager); //       
 for (; manager->y < ROW_END; ++manager->y) //     
 {
 if (checkCollision(manager)) //      
 {
  break;
 }
 }
 --manager->y; //           

 insertTetris(manager); //       
 setPoolColor(manager, control); //     

 checkErasing(manager, control); //     
 printTetrisPool(manager, control); //      
}

// =============================================================================
//     
bool checkErasing(TetrisManager *manager, TetrisControl *control)
{
 static const unsigned scores[5] = { 0, 10, 30, 90, 150 }; //     
 int8_t count = 0;
 int8_t k = 0, y = manager->y + 3; //       

 do
 {
 if (y < ROW_END && manager->pool[y] == 0xFFFFU) //            
 {
  ++count;
  //       
  memmove(manager->pool + 1, manager->pool, sizeof(uint16_t) * y);
  //            
  memmove(control->color[1], control->color[0], sizeof(int8_t [16]) * y);
 }
 else
 {
  --y;
  ++k;
 }
 } while (y >= manager->y && k < 4);

 manager->erasedTotal += count; //     
 manager->score += scores[count]; //   

 if (count > 0)
 {
 ++manager->erasedCount[count - 1]; //   
 }

 giveTetris(manager); //       
 setPoolColor(manager, control); //     

 return (count > 0);
}

// =============================================================================
//    
void keydownControl(TetrisManager *manager, TetrisControl *control, int key)
{
 if (key == 13) //   /    
 {
 control->pause = !control->pause;
 }

 if (control->pause) //     ,    
 {
 return;
 }

 switch (key)
 {
 case 'w': case 'W': case '8': case 72: //  
 control->clockwise = true; //      
 rotateTetris(manager, control); //     
 break;
 case 'a': case 'A': case '4': case 75: //  
 control->direction = 0; //     
 horzMoveTetris(manager, control); //       
 break;
 case 'd': case 'D': case '6': case 77: //  
 control->direction = 1; //     
 horzMoveTetris(manager, control); //       
 break;
 case 's': case 'S': case '2': case 80: //  
 moveDownTetris(manager, control); //       
 break;
 case ' ': //     
 dropDownTetris(manager, control);
 break;
 case '0': //   
 control->clockwise = false; //      
 rotateTetris(manager, control); //     
 break;
 default:
 break;
 }
}

// =============================================================================
//      
void gotoxyWithFullwidth(short x, short y)
{
 static COORD cd;

 cd.X = (short)(x << 1);
 cd.Y = y;
 SetConsoleCursorPosition(g_hConsoleOutput, cd);
}

// =============================================================================
//        
void printPoolBorder()
{
 int8_t y;

 SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
 for (y = ROW_BEGIN; y < ROW_END; ++y) //      4    2 
 {
 gotoxyWithFullwidth(10, y - 3);
 printf("%2s", "");
 gotoxyWithFullwidth(23, y - 3);
 printf("%2s", "");
 }

 gotoxyWithFullwidth(10, y - 3); //     
 printf("%28s", "");
}

//           
#define gotoxyInPool(x, y) gotoxyWithFullwidth(x + 9, y - 3)

// =============================================================================
//      
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control)
{
 int8_t x, y;

 for (y = ROW_BEGIN; y < ROW_END; ++y) //      4    2 
 {
 gotoxyInPool(2, y); //           
 for (x = COL_BEGIN; x < COL_END; ++x) //        
 {
  if ((manager->pool[y] >> x) & 1) //          
  {
  //      ,        
  SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
  printf("■");
  }
  else //     ,    
  {
  SetConsoleTextAttribute(g_hConsoleOutput, 0);
  printf("%2s", "");
  }
 }
 }
}

// =============================================================================
//       
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control)
{
 int8_t x, y;

 //               ,         ,        
 //          ,        
 y = (manager->y > ROW_BEGIN) ? (manager->y - 1) : ROW_BEGIN; //       
 for (; y < ROW_END && y < manager->y + 4; ++y)
 {
 x = (manager->x > COL_BEGIN) ? (manager->x - 1) : COL_BEGIN; //       
 for (; x < COL_END && x < manager->x + 5; ++x) //       
 {
  gotoxyInPool(x, y); //           
  if ((manager->pool[y] >> x) & 1) //          
  {
  //      ,        
  SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
  printf("■");
  }
  else //     ,    
  {
  SetConsoleTextAttribute(g_hConsoleOutput, 0);
  printf("%2s", "");
  }
 }
 }
}

// =============================================================================
//             
void printNextTetris(const TetrisManager *manager)
{
 int8_t i;
 uint16_t tetris;

 //   
 SetConsoleTextAttribute(g_hConsoleOutput, 0xF);
 gotoxyWithFullwidth(26, 1);
 printf("┏━━━━┳━━━━┓");
 gotoxyWithFullwidth(26, 2);
 printf("┃%8s┃%8s┃", "", "");
 gotoxyWithFullwidth(26, 3);
 printf("┃%8s┃%8s┃", "", "");
 gotoxyWithFullwidth(26, 4);
 printf("┃%8s┃%8s┃", "", "");
 gotoxyWithFullwidth(26, 5);
 printf("┃%8s┃%8s┃", "", "");
 gotoxyWithFullwidth(26, 6);
 printf("┗━━━━┻━━━━┛");

 //    ,       
 tetris = gs_uTetrisTable[manager->type[1]][manager->orientation[1]];
 SetConsoleTextAttribute(g_hConsoleOutput, manager->type[1] | 8);
 for (i = 0; i < 16; ++i)
 {
 gotoxyWithFullwidth((i & 3) + 27, (i >> 2) + 2);
 ((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
 }

 //     ,     
 tetris = gs_uTetrisTable[manager->type[2]][manager->orientation[2]];
 SetConsoleTextAttribute(g_hConsoleOutput, 8);
 for (i = 0; i < 16; ++i)
 {
 gotoxyWithFullwidth((i & 3) + 32, (i >> 2) + 2);
 ((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
 }
}

// =============================================================================
//       
void printScore(const TetrisManager *manager)
{
 static const char *tetrisName = "ITLJZSO";
 int8_t i;

 SetConsoleTextAttribute(g_hConsoleOutput, 0xE);

 gotoxyWithFullwidth(2, 2);
 printf("■  :%u", manager->score);

 gotoxyWithFullwidth(1, 6);
 printf("■    :%u", manager->erasedTotal);
 for (i = 0; i < 4; ++i)
 {
 gotoxyWithFullwidth(2, 8 + i);
 printf("□ %d:%u", i + 1, manager->erasedCount[i]);
 }

 gotoxyWithFullwidth(1, 15);
 printf("■    :%u", manager->tetrisTotal);

 for (i = 0; i < 7; ++i)
 {
 gotoxyWithFullwidth(2, 17 + i);
 printf("□%c :%u", tetrisName[i], manager->tetrisCount[i]);
 }
}

// =============================================================================
//       
void printPrompting()
{
 SetConsoleTextAttribute(g_hConsoleOutput, 0xB);
 gotoxyWithFullwidth(26, 10);
 printf("■  :");
 gotoxyWithFullwidth(27, 12);
 printf("□    :← A 4");
 gotoxyWithFullwidth(27, 13);
 printf("□    :→ D 6");
 gotoxyWithFullwidth(27, 14);
 printf("□    :↓ S 2");
 gotoxyWithFullwidth(27, 15);
 printf("□    :↑ W 8");
 gotoxyWithFullwidth(27, 16);
 printf("□    :0");
 gotoxyWithFullwidth(27, 17);
 printf("□    :  ");
 gotoxyWithFullwidth(27, 18);
 printf("□    :  ");
 gotoxyWithFullwidth(25, 23);
 printf("■By: NEWPLAN @ UESTC");
}

// =============================================================================
//     
void runGame(TetrisManager *manager, TetrisControl *control)
{
 clock_t clockLast, clockNow;

 clockLast = clock(); //   
 printTetrisPool(manager, control); //      

 while (!manager->dead) //   
 {
 while (_kbhit()) //     
 {
  keydownControl(manager, control, _getch()); //     
 }

 if (!control->pause) //    
 {
  clockNow = clock(); //   
  //          0.45 
  if (clockNow - clockLast > 0.45F * CLOCKS_PER_SEC)
  {
  clockLast = clockNow;
  keydownControl(manager, control, 80); //      
  }
 }
 }
}

// =============================================================================
//     
bool ifPlayAgain()
{
 int ch;

 SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
 gotoxyWithFullwidth(15, 10);
 printf("    ");
 gotoxyWithFullwidth(13, 11);
 printf(" Y  , N  ");

 do
 {
 ch = _getch();
 if (ch == 'Y' || ch == 'y')
 {
  return true;
 }
 else if (ch == 'N' || ch == 'n')
 {
  return false;
 }
 } while (1);
}

더 많은 러시아 블록 하 이 라이트 글 은 주 제 를 클릭 하 십시오러 스 블록 게임 집합학습 을 진행 합 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기