monoteen

Tetris Game

Nov 5th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <conio.h>
  6.  
  7. #define BOARD_HEIGHT 20
  8. #define BOARD_WIDTH 10
  9. #define BOARD_X 4
  10. #define BOARD_Y 2
  11.  
  12. #define CBLOCK_X 13
  13. #define CBLOCK_Y 2
  14. #define DELAY 100
  15. #define RAND 4
  16.  
  17. #define LEFT 75
  18. #define RIGHT 77
  19. #define UP 72
  20. #define DOWN 80
  21. #define SPACE 32
  22.  
  23. int static score = 0;
  24. int static level = 1;
  25. int static speed = 180;
  26. int curX, curY;
  27. int board[BOARD_HEIGHT + 1][BOARD_WIDTH + 2] = { 0, };
  28. int block[][4][4] = {
  29.     {
  30.         { 0, 0, 0, 0 },
  31.         { 1, 0, 0, 0 },
  32.         { 1, 1, 1, 0 },
  33.         { 0, 0, 0, 0 } },
  34.         {
  35.             { 0, 0, 1, 0 },
  36.             { 0, 0, 1, 0 },
  37.             { 0, 1, 1, 0 },
  38.             { 0, 0, 0, 0 } },
  39.             {
  40.                 { 1, 1, 1, 0 },
  41.                 { 0, 0, 1, 0 },
  42.                 { 0, 0, 0, 0 },
  43.                 { 0, 0, 0, 0 } },
  44.                 {
  45.                     { 1, 1, 0, 0 },
  46.                     { 1, 0, 0, 0 },
  47.                     { 1, 0, 0, 0 },
  48.                     { 0, 0, 0, 0 } },
  49.                     {
  50.                         { 0, 0, 0, 0 },
  51.                         { 0, 0, 1, 0 },
  52.                         { 1, 1, 1, 0 },
  53.                         { 0, 0, 0, 0 } },
  54.                         {
  55.                             { 0, 1, 1, 0 },
  56.                             { 0, 0, 1, 0 },
  57.                             { 0, 0, 1, 0 },
  58.                             { 0, 0, 0, 0 } },
  59.                             {
  60.                                 { 1, 1, 1, 0 },
  61.                                 { 1, 0, 0, 0 },
  62.                                 { 0, 0, 0, 0 },
  63.                                 { 0, 0, 0, 0 } },
  64.                                 {
  65.                                     { 1, 0, 0, 0 },
  66.                                     { 1, 0, 0, 0 },
  67.                                     { 1, 1, 0, 0 },
  68.                                     { 0, 0, 0, 0 } },
  69.  
  70.                                     {
  71.                                         { 0, 0, 0, 0 },
  72.                                         { 0, 1, 0, 0 },
  73.                                         { 1, 1, 1, 0 },
  74.                                         { 0, 0, 0, 0 } },
  75.                                         {
  76.                                             { 0, 0, 0, 0 },
  77.                                             { 0, 1, 0, 0 },
  78.                                             { 1, 1, 0, 0 },
  79.                                             { 0, 1, 0, 0 } },
  80.                                             {
  81.                                                 { 0, 0, 0, 0 },
  82.                                                 { 0, 0, 0, 0 },
  83.                                                 { 1, 1, 1, 0 },
  84.                                                 { 0, 1, 0, 0 } },
  85.                                                 {
  86.                                                     { 0, 0, 0, 0 },
  87.                                                     { 0, 1, 0, 0 },
  88.                                                     { 0, 1, 1, 0 },
  89.                                                     { 0, 1, 0, 0 } },
  90.  
  91.                                                     {
  92.                                                         { 0, 1, 0, 0 },
  93.                                                         { 0, 1, 0, 0 },
  94.                                                         { 0, 1, 0, 0 },
  95.                                                         { 0, 1, 0, 0 } },
  96.                                                         {
  97.                                                             { 0, 0, 0, 0 },
  98.                                                             { 0, 0, 0, 0 },
  99.                                                             { 1, 1, 1, 1 },
  100.                                                             { 0, 0, 0, 0 } },
  101.                                                             {
  102.                                                                 { 0, 1, 0, 0 },
  103.                                                                 { 0, 1, 0, 0 },
  104.                                                                 { 0, 1, 0, 0 },
  105.                                                                 { 0, 1, 0, 0 } },
  106.                                                                 {
  107.                                                                     { 0, 0, 0, 0 },
  108.                                                                     { 0, 0, 0, 0 },
  109.                                                                     { 1, 1, 1, 1 },
  110.                                                                     { 0, 0, 0, 0 } },
  111.  
  112.                                                                     {
  113.                                                                         { 1, 1, 0, 0 },
  114.                                                                         { 1, 1, 0, 0 },
  115.                                                                         { 0, 0, 0, 0 },
  116.                                                                         { 0, 0, 0, 0 } },
  117.                                                                         {
  118.                                                                             { 1, 1, 0, 0 },
  119.                                                                             { 1, 1, 0, 0 },
  120.                                                                             { 0, 0, 0, 0 },
  121.                                                                             { 0, 0, 0, 0 } },
  122.                                                                             {
  123.                                                                                 { 1, 1, 0, 0 },
  124.                                                                                 { 1, 1, 0, 0 },
  125.                                                                                 { 0, 0, 0, 0 },
  126.                                                                                 { 0, 0, 0, 0 } },
  127.                                                                                 {
  128.                                                                                     { 1, 1, 0, 0 },
  129.                                                                                     { 1, 1, 0, 0 },
  130.                                                                                     { 0, 0, 0, 0 },
  131.                                                                                     { 0, 0, 0, 0 } },
  132.  
  133.                                                                                     {
  134.                                                                                         { 0, 0, 0, 0 },
  135.                                                                                         { 0, 1, 1, 0 },
  136.                                                                                         { 1, 1, 0, 0 },
  137.                                                                                         { 0, 0, 0, 0 } },
  138.                                                                                         {
  139.                                                                                             { 0, 1, 0, 0 },
  140.                                                                                             { 0, 1, 1, 0 },
  141.                                                                                             { 0, 0, 1, 0 },
  142.                                                                                             { 0, 0, 0, 0 } },
  143.                                                                                             {
  144.                                                                                                 { 0, 0, 0, 0 },
  145.                                                                                                 { 0, 1, 1, 0 },
  146.                                                                                                 { 1, 1, 0, 0 },
  147.                                                                                                 { 0, 0, 0, 0 } },
  148.                                                                                                 {
  149.                                                                                                     { 0, 1, 0, 0 },
  150.                                                                                                     { 0, 1, 1, 0 },
  151.                                                                                                     { 0, 0, 1, 0 },
  152.                                                                                                     { 0, 0, 0, 0 } },
  153.  
  154.                                                                                                     {
  155.                                                                                                         { 0, 0, 0, 0 },
  156.                                                                                                         { 1, 1, 0, 0 },
  157.                                                                                                         { 0, 1, 1, 0 },
  158.                                                                                                         { 0, 0, 0, 0 } },
  159.                                                                                                         {
  160.                                                                                                             { 0, 0, 1, 0 },
  161.                                                                                                             { 0, 1, 1, 0 },
  162.                                                                                                             { 0, 1, 0, 0 },
  163.                                                                                                             { 0, 0, 0, 0 } },
  164.                                                                                                             {
  165.                                                                                                                 { 0, 0, 0, 0 },
  166.                                                                                                                 { 1, 1, 0, 0 },
  167.                                                                                                                 { 0, 1, 1, 0 },
  168.                                                                                                                 { 0, 0, 0, 0 } },
  169.                                                                                                                 {
  170.                                                                                                                     { 0, 0, 1, 0 },
  171.                                                                                                                     { 0, 1, 1, 0 },
  172.                                                                                                                     { 0, 1, 0, 0 },
  173.                                                                                                                     { 0, 0, 0, 0 } },
  174.  
  175. };
  176. void removeCursor(void)
  177. {
  178.     CONSOLE_CURSOR_INFO curInfo;
  179.     GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
  180.     curInfo.bVisible = 0;
  181.     SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
  182. }
  183.  
  184. void setCursor(int x, int y)
  185. {
  186.     COORD pos = { x, y };
  187.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
  188. }
  189. COORD getCursor(void)
  190. {
  191.     COORD cur;
  192.     CONSOLE_SCREEN_BUFFER_INFO curInfo;
  193.  
  194.     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
  195.     cur.X = curInfo.dwCursorPosition.X;
  196.     cur.Y = curInfo.dwCursorPosition.Y;
  197.     return cur;
  198. }
  199.  
  200. void showBoard(void)
  201. {
  202.     int x, y;
  203.  
  204.     for (x = 1; x <= BOARD_WIDTH + 1; x++)
  205.     {
  206.         board[BOARD_HEIGHT][x] = 1;
  207.         setCursor((BOARD_X)+x * 2, BOARD_Y + BOARD_HEIGHT);
  208.         printf("━");
  209.     }
  210.     for (y = 0; y<BOARD_HEIGHT + 1; y++)
  211.     {
  212.         board[y][0] = 1;
  213.         setCursor(BOARD_X, BOARD_Y + y);
  214.         if (y == BOARD_HEIGHT)
  215.             printf("┗");
  216.         else
  217.             printf("┃");
  218.     }
  219.     for (y = 0; y<BOARD_HEIGHT + 1; y++)
  220.     {
  221.         board[y][BOARD_WIDTH + 1] = 1;
  222.         setCursor(BOARD_X + (BOARD_WIDTH + 2) * 2, BOARD_Y + y);
  223.         if (y == BOARD_HEIGHT)
  224.             printf("┛");
  225.         else
  226.             printf("┃");
  227.     }
  228.  
  229.     board[20][0] = 1;
  230.     board[20][11] = 1;
  231.  
  232.     puts(" ");
  233. }
  234.  
  235. void initial(int x, int y)
  236. {
  237.     curX = x;
  238.     curY = y;
  239.     setCursor(x, y);
  240. }
  241.  
  242. int detect(int rotation, int move1, int move2)
  243. {
  244.     int x, y;
  245.     int arrX, arrY;
  246.     COORD pos = getCursor();
  247.  
  248.     arrX = pos.X + move1;
  249.     arrY = pos.Y + move2;
  250.  
  251.     arrX = (arrX / 2) - 2;
  252.     arrY = arrY - BOARD_Y;
  253.  
  254.  
  255.     for (y = 0; y<4; y++)
  256.     {
  257.         for (x = 0; x<4; x++)
  258.         {
  259.             if ((block[rotation][y][x] == 1) && board[arrY + y][arrX + x] == 1)
  260.                 return 1;
  261.         }
  262.     }
  263.     return 0;
  264.  
  265. }
  266.  
  267. void showBlock(int rotation)
  268. {
  269.     int x, y;
  270.     COORD cursor = getCursor();
  271.     int prove;
  272.     prove = detect(rotation, 0, 0);
  273.     if (prove == 0)
  274.     {
  275.         for (y = 0; y<4; y++)
  276.         {
  277.             for (x = 0; x<4; x++)
  278.             {
  279.                 setCursor(cursor.X + (x * 2), cursor.Y + y);
  280.                 if (block[rotation][y][x] == 1)
  281.                     printf("■");
  282.             }
  283.         }
  284.         setCursor(cursor.X, cursor.Y);
  285.     }
  286. }
  287.  
  288.  
  289. void removeBlock(int rotation, int move1, int move2)
  290. {
  291.     int pr;
  292.     int x, y;
  293.  
  294.     COORD cursor = getCursor();
  295.  
  296.     pr = detect(rotation, move1, move2);
  297.  
  298.     if (pr == 0)
  299.     {
  300.         for (y = 0; y<4; y++)
  301.         {
  302.             for (x = 0; x<4; x++)
  303.             {
  304.                 setCursor(cursor.X + (x * 2), cursor.Y + y);
  305.                 if (block[rotation][y][x] == 1)
  306.                     printf(" ");
  307.             }
  308.         }
  309.         setCursor(cursor.X + move1, cursor.Y + move2);
  310.     }
  311. }
  312.  
  313.  
  314. void boardConginition(int n, int move1, int move2)
  315. {
  316.     int x1, y1;
  317.     COORD pos = getCursor();
  318.  
  319.     int arrX = pos.X + move1;
  320.     int arrY = pos.Y + move2;
  321.     int x, y;
  322.  
  323.     arrX = arrX / 2 - 2;
  324.     arrY = arrY - 2;
  325.  
  326.     for (y = 0; y<4; y++)
  327.     {
  328.         for (x = 0; x<4; x++)
  329.         {
  330.             if (block[n][y][x] == 1)
  331.             {
  332.                 board[arrY + y][arrX + x] = 1;
  333.             }
  334.         }
  335.     }
  336.     return;
  337.  
  338. }
  339.  
  340. void array_down(int column)
  341. {
  342.     int y, x;
  343.  
  344.     for (y = column; y >= 0; y--)
  345.     {
  346.         for (x = 1; x <= 10; x++)
  347.         {
  348.             board[y][x] = board[y - 1][x];
  349.         }
  350.     }
  351.     for (x = 1; x <= 10; x++)
  352.         board[0][x] = 0;
  353.  
  354.     for (y = 1; y <= 19; y++)
  355.     {
  356.         for (x = 1; x <= 10; x++)
  357.         {
  358.             setCursor((BOARD_X)+x * 2 + 1, y + BOARD_Y);
  359.             if (board[y][x] == 1)
  360.                 printf("■");
  361.             else
  362.                 printf("  ");
  363.         }
  364.     }
  365.  
  366. }
  367. void scoreLevel(void)
  368. {
  369.  
  370.     setCursor(40, 3);
  371.     printf("Misson : Level 10 Game Clear");
  372.     setCursor(40, 5);
  373.     printf("Level:%d\n", level);
  374.     setCursor(40, 7);
  375.     printf("Score:%d\n", score);
  376. }
  377. void countScore(void)
  378. {
  379.     score += 10;
  380.     if (score % 30 == 0)
  381.     {
  382.         level += 1;
  383.         speed -= 30;
  384.     }
  385.     scoreLevel();
  386. }
  387. void control(void)
  388. {
  389.     int i;
  390.     int x, y;
  391.     int z = 0;
  392.  
  393.     for (y = 19; y >= 1; y--)
  394.     {
  395.         for (z = 0; z<4; z++)
  396.         {
  397.             i = 0;
  398.             for (x = 1; x<11; x++)
  399.             {
  400.                 if (board[y][x] == 1)
  401.                 {
  402.                     i++;
  403.                     if (i == 10)
  404.                     {
  405.                         for (x = 1; x<11; x++)
  406.                         {
  407.                             setCursor((x + 2) * 2, y + 2);
  408.                             printf("  ");
  409.                         }
  410.                         countScore();
  411.                         array_down(y);
  412.                     }
  413.  
  414.                 }
  415.             }
  416.         }
  417.     }
  418.  
  419.  
  420. }
  421.  
  422. int gameOver(int rotation)
  423. {
  424.     if (detect(rotation, 0, 0))
  425.         return 5;
  426.     else
  427.         return 0;
  428. }
  429.  
  430. int gameWin(void)
  431. {
  432.     if (level == 10)
  433.         return 1;
  434.     else
  435.         return 0;
  436. }
  437.  
  438. void moveBlock(void)
  439. {
  440.  
  441.     int n;
  442.     int kb;
  443.     int prove;
  444.     int winOver;
  445.     srand(time(NULL));
  446.     int c = 2;
  447.  
  448.     while (1)
  449.     {
  450.  
  451.         initial(CBLOCK_X, CBLOCK_Y);
  452.  
  453.         n = rand() % 7;
  454.         n = n * 4;
  455.  
  456.         if (gameWin())
  457.         {
  458.             setCursor(35, 20);
  459.             printf("GAME WIN");
  460.             getchar();
  461.             exit(1);
  462.         }
  463.         if (gameOver(n))
  464.             break;
  465.  
  466.  
  467.         while (1)
  468.         {
  469.             int ww = 0;
  470.             int var;
  471.             int k = 0;
  472.             int tmp;
  473.  
  474.             while (!_kbhit())
  475.             {
  476.                 showBlock(n);
  477.                 Sleep(DELAY + speed);
  478.                 if (detect(n, 0, 1) == 1)
  479.                 {
  480.                     ww = 1;
  481.                     boardConginition(n, 0, 0);
  482.                     control();
  483.                     break;
  484.                 }
  485.                 removeBlock(n, 0, 1);
  486.             }
  487.             if (ww == 1)
  488.                 break;
  489.  
  490.             kb = _getch();
  491.             switch (kb)
  492.             {
  493.             case LEFT:
  494.                 removeBlock(n, -2, 0);
  495.                 showBlock(n);
  496.                 break;
  497.             case RIGHT:
  498.                 removeBlock(n, 2, 0);
  499.                 showBlock(n);
  500.                 break;
  501.             case UP:
  502.  
  503.                 k = n / 4;
  504.                 k *= 4;
  505.  
  506.                 if ((n + 1) <= (k + 3))
  507.                 {
  508.                     k = n + 1;
  509.                 }
  510.  
  511.                 prove = detect(k, 0, 0);
  512.                 if (prove == 0)
  513.                 {
  514.                     removeBlock(n, 0, 0);
  515.                     n = k;
  516.                     showBlock(n);
  517.                     break;
  518.                 }
  519.                 break;
  520.             case DOWN:
  521.                 removeBlock(n, 0, 2);
  522.                 break;
  523.             case SPACE:
  524.                 while (1)
  525.                 {
  526.                     removeBlock(n, 0, 1);
  527.                     if (detect(n, 0, 1) == 1)
  528.                     {
  529.                         showBlock(n);
  530.                         boardConginition(n, 0, 0);
  531.                         break;
  532.                     }
  533.  
  534.                 }
  535.  
  536.  
  537.             }
  538.  
  539.         }
  540.  
  541.     }
  542.  
  543.     setCursor(35, 20);
  544.     printf("GAME OVER");
  545. }
  546.  
  547.  
  548.  
  549.  
  550. int main()
  551. {
  552.     removeCursor();
  553.  
  554.     setCursor(2, 1);
  555.     showBoard();
  556.     scoreLevel();
  557.  
  558.     moveBlock();
  559.  
  560.     getchar();
  561.  
  562. }
Advertisement
Add Comment
Please, Sign In to add comment