Advertisement
Monika__

Tetris_do_oddania

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