Advertisement
Monika__

Tetris

Apr 9th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.60 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5.  
  6. #include "primlib.h"
  7. #include "pieces.inl"
  8.  
  9. #define SQUARE_SIZE 20
  10. #define GAME_WIDTH 15
  11. #define GAME_HEIGHT 20
  12. #define MOVING_PIECE_COLOR YELLOW
  13. #define STATIC_PIECE_COLOR BLUE
  14. #define TIME 100000
  15. #if (GAME_WIDTH < 10 || GAME_WIDTH > 20 || GAME_HEIGHT < 10 || GAME_HEIGHT > 20)
  16.     #error: "the wrong dimension of the game's area"
  17. #endif
  18.  
  19.  
  20. void updateState (char nextPiece[4][4]);
  21.  
  22. bool isPossible(int x, int y, char piece[4][4], char move);
  23.  
  24. int play(int x,int y, char whichPiece[4][4][4]);
  25.  
  26. void deleteRows();
  27.  
  28. void putPiece(int x,int y, char thatPiece[4][4], enum color colour);
  29.  
  30. void end();
  31.  
  32. int area[GAME_WIDTH][GAME_HEIGHT][3];
  33. int leftCorner_x = 100;
  34. int leftCorner_y = 70;
  35.  
  36.  
  37. int main()
  38. {
  39.   int column, row;
  40.   int whichPiece, nextPiece;
  41.  
  42.   initGraph();
  43.  
  44.   for(column = 0; column < GAME_WIDTH; column++)
  45.   {
  46.     for(row = 0;row < GAME_HEIGHT; row++)
  47.     {
  48.       area[column][row][0] = leftCorner_x + column * SQUARE_SIZE;
  49.       area[column][row][1] = leftCorner_y + row * SQUARE_SIZE;
  50.       area[column][row][2] = 0;
  51.     }
  52.   }
  53.  
  54.   whichPiece = (int) rand() % 7;
  55.  
  56.   while(1)
  57.   {
  58.     nextPiece = (int) rand() % 7;
  59.  
  60.     updateState(pieces[nextPiece][0]);
  61.     updateScreen();
  62.  
  63.     if(isPossible((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == false)
  64.     {
  65.       end();
  66.       break;
  67.     }
  68.  
  69.     putPiece((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR);
  70.     updateScreen();
  71.  
  72.     if(play((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
  73.     {
  74.       end();
  75.       break;
  76.     }
  77.  
  78.     deleteRows();
  79.  
  80.     whichPiece = nextPiece;
  81.   }
  82.  
  83.   updateScreen();
  84.   return 0;
  85. }
  86.  
  87. void updateState(char nextPiece [4][4])
  88. {
  89.     int column, row;
  90.  
  91.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  92.  
  93.     filledRect(leftCorner_x - 5, leftCorner_y,
  94.              leftCorner_x - 1, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
  95.     filledRect(leftCorner_x, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE),
  96.              leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) - 1, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
  97.     filledRect(leftCorner_x + (GAME_WIDTH * SQUARE_SIZE), leftCorner_y,
  98.              leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) + 4, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
  99.  
  100.  
  101.     for(column = 0; column < GAME_WIDTH; column++)
  102.   {
  103.     for(row = 0; row < GAME_HEIGHT; row++)
  104.     {
  105.       filledRect(area[column][row][0] + 1, area[column][row][1] + 1,
  106.                  area[column][row][0] + SQUARE_SIZE - 2, area[column][row][1] + SQUARE_SIZE - 2, area[column][row][2]);
  107.     }
  108.   }
  109.  
  110.     textout( leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) + 11, leftCorner_y, "next piece: ", WHITE);
  111.  
  112.     for(column = 0; column < 4; column++)
  113.   {
  114.     for(row = 0; row < 4; row++)
  115.         {
  116.       if (nextPiece[column][row] != 0)
  117.             {
  118.         filledRect(area[column][row][0] + (GAME_WIDTH * SQUARE_SIZE) + 11, area[column][row][1] + 11,
  119.                    area[column][row][0] + (GAME_WIDTH * SQUARE_SIZE) + 28, area[column][row][1] + 28,  MOVING_PIECE_COLOR);
  120.       }
  121.     }
  122.   }
  123. }
  124. bool isPossible(int x, int y, char piece[4][4], char move)
  125. {
  126.     int column, row;
  127.  
  128.     switch(move)
  129.     {
  130.         case('l'):
  131.                 x--;
  132.                 break;
  133.         case('r'):
  134.                 x++;
  135.                 break;
  136.         case('d'):
  137.                 y++;
  138.                 break;
  139.         case('0'):
  140.           break;
  141.     }
  142.  
  143.     if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
  144.   {
  145.     return false;
  146.   }
  147.  
  148.     for(column = 0; column < 4; column++)
  149.   {
  150.     for(row = 0; row < 4; row++)
  151.         {
  152.             if(piece[column][row] != 0 && (x + column >= GAME_WIDTH || y + row >= GAME_HEIGHT))
  153.       {
  154.         return false;
  155.       }
  156.       if (piece[column][row] != 0 && area[x + column][y + row][2] != 0)
  157.       {
  158.         return false;
  159.       }
  160.         }
  161.   }
  162.  
  163.     return true;
  164. }
  165.  
  166. void putPiece(int x, int y, char piece[4][4], enum color colour)
  167. {
  168.   int column,row;
  169.     for(column = 0; column < 4; column++)
  170.   {
  171.     for(row = 0; row < 4; row++)
  172.         {
  173.       if(piece[column][row] != 0)
  174.             {
  175.         filledRect(area[x + column][y + row][0] + 1, area[x + column][y + row][1] + 1,
  176.                    area[x + column][y + row][0] + SQUARE_SIZE - 2, area[x + column][y + row][1] + SQUARE_SIZE - 2, colour);
  177.       }
  178.     }
  179.   }
  180. }
  181.  
  182. int play(int x, int y, char whichPiece[4][4][4])
  183. {
  184.   int column, row, turn;
  185.     int rotation = 0;
  186.  
  187.     while(1)
  188.     {
  189.         for(turn = 0; turn < 10; turn++)
  190.         {
  191.             if(isKeyDown('q') == 1)
  192.       {
  193.         return 0;
  194.       }
  195.  
  196.             switch(pollkey())
  197.             {
  198.                 case(SDLK_DOWN):
  199.                     while(isPossible(x, y, whichPiece[rotation], 'd') == true)
  200.                     {
  201.                         putPiece(x, y, whichPiece[rotation], BLACK);
  202.                         y++;
  203.                         putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  204.                         updateScreen();
  205.                     }
  206.                     goto saving;
  207.  
  208.                 case(SDLK_RIGHT):
  209.                     if(isPossible(x, y, whichPiece[rotation], 'r') == true)
  210.                     {
  211.                         putPiece(x,y,whichPiece[rotation],BLACK);
  212.                         x++;
  213.                         putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  214.                         updateScreen();
  215.                     }
  216.             break;
  217.  
  218.                 case(SDLK_LEFT):
  219.                     if(isPossible(x, y, whichPiece[rotation], 'l') == true)
  220.                     {
  221.                         putPiece(x, y, whichPiece[rotation], BLACK);
  222.                         x--;
  223.                         putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  224.                         updateScreen();
  225.                     }
  226.             break;
  227.  
  228.                 case(SDLK_SPACE):
  229.                     if(isPossible(x, y, whichPiece[(rotation + 1) % 4], '0'))
  230.                     {
  231.                         putPiece(x, y, whichPiece[rotation], BLACK);
  232.                         ++rotation;
  233.               rotation %= 4;
  234.                         putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  235.                         updateScreen();
  236.                     }
  237.                     else if (isPossible(x - 1, y, whichPiece[(rotation+1)%4], '0'))
  238.                     {
  239.                         putPiece(x, y, whichPiece[rotation], BLACK);
  240.                         ++rotation;
  241.               rotation%=4;
  242.               x--;
  243.                         putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  244.                         updateScreen();
  245.                     }
  246.                     else if (isPossible(x + 1, y, whichPiece[(rotation + 1) % 4], '0'))
  247.                     {
  248.                         putPiece(x, y, whichPiece[rotation], BLACK);
  249.                         ++rotation;
  250.               rotation %= 4;
  251.               x++;
  252.                         putPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  253.                         updateScreen();
  254.                     }
  255.                     break;
  256.             }
  257.             usleep(TIME);
  258.         }
  259.         if(isPossible(x, y, whichPiece[rotation], 'd') == true)
  260.         {
  261.             putPiece(x,y,whichPiece[rotation],BLACK);
  262.             y++;
  263.             putPiece(x,y,whichPiece[rotation], MOVING_PIECE_COLOR);
  264.             updateScreen();
  265.         }
  266.         else
  267.     {
  268.       break;
  269.     }
  270.     }
  271. saving:
  272.     for(column = 0; column < 4; column++)
  273.   {
  274.     for(row = 0; row < 4; row++)
  275.     {
  276.       if(whichPiece[rotation][column][row] != 0)
  277.       {
  278.         area[x + column][y + row][2] = STATIC_PIECE_COLOR;
  279.       }
  280.     }
  281.   }
  282.     return 1;
  283. }
  284.  
  285. void deleteRows()
  286. {
  287.   int column, row;
  288.     int deletedRow;
  289.     int alfa = 1;
  290.  
  291.     for(row = 0; row < GAME_HEIGHT; row++)
  292.     {
  293.         for(column = 0; column < GAME_WIDTH; column++)
  294.         {
  295.             alfa *= area[column][row][2];
  296.             if (alfa == 0)
  297.       {
  298.         break;
  299.       }
  300.         }
  301.         if(alfa != 0)
  302.         {
  303.             for(deletedRow = row; deletedRow > 0; deletedRow--)
  304.       {
  305.         for(column = 0; column < GAME_WIDTH; column++)
  306.         {
  307.           area[column][deletedRow][2] = area[column][deletedRow - 1][2];
  308.         }
  309.       }
  310.  
  311.             for(column = 0; column < GAME_WIDTH; column++)
  312.       {
  313.         area[column][0][2] = 0;
  314.       }
  315.         }
  316.         alfa = 1;
  317.     }
  318. }
  319.  
  320. void end()
  321. {
  322.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  323.     textout((screenWidth() - 1) / 2 - 100, (screenHeight() - 1) / 2, "GAME OVER", WHITE);
  324.     updateScreen();
  325.     sleep(3);
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement