Advertisement
Monika__

tetris_work in progress

Apr 23rd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.11 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdbool.h>
  6.  
  7. #include "primlib.h"
  8. #include "pieces.inl"
  9.  
  10. #define SQUARE_SIZE 20
  11. #define GAME_WIDTH 15
  12. #define GAME_HEIGHT 20
  13. #define MOVING_PIECE_COLOR GREEN
  14. #define STATIC_PIECE_COLOR YELLOW
  15. #define GAME_OVER_TIME 2
  16. #define TIME 100000
  17. #if (GAME_WIDTH < 10 || GAME_WIDTH > 20 || GAME_HEIGHT < 10 || GAME_HEIGHT > 20)
  18.     #error: "the wrong dimension of the game's area"
  19. #endif
  20.  
  21.  
  22. void updateState (char nextPiece[4][4]);
  23. bool isPossible(int x, int y, char piece[4][4], char move);
  24. bool play(int x,int y, char whichPiece[4][4][4]);
  25. void deleteRows();
  26. void setPiece(int x,int y, char thatPiece[4][4], enum color colour);
  27. void gameOver();
  28. void drawBoundaries();
  29. void showComingPiece(char nextPiece[4][4]);
  30. void clearPiece(int x, int y, char piece[4][4]);
  31. //void findCenter(int rotation, char whichPiece[4][4][4]);
  32.  
  33. int gameArray[GAME_WIDTH][GAME_HEIGHT][3];
  34. int coordinatesOfCenter[2];
  35. int leftCorner_x = 100;
  36. int leftCorner_y = 70;
  37.  
  38.  
  39. int main()
  40. {
  41.   int column, row;
  42.   int whichPiece, nextPiece;
  43. //  srand(time(NULL));
  44.  
  45.   initGraph();
  46.  
  47.   for(column = 0; column < GAME_WIDTH; column++)
  48.   {
  49.     for(row = 0;row < GAME_HEIGHT; row++)
  50.     {
  51.       gameArray[column][row][0] = leftCorner_x + column * SQUARE_SIZE;
  52.       gameArray[column][row][1] = leftCorner_y + row * SQUARE_SIZE;
  53.       gameArray[column][row][2] = 0;
  54.     }
  55.   }
  56.  
  57.   whichPiece = (int) rand() % 7;
  58.  
  59.   while(1)
  60.   {
  61.     nextPiece = (int) rand() % 7;
  62.  
  63.     updateState(pieces[nextPiece][0]);
  64.     updateScreen();
  65.  
  66.     if(isPossible((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == false)
  67.     {
  68.       gameOver();
  69.       break;
  70.     }
  71.  
  72.     setPiece((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR);
  73.     updateScreen();
  74.  
  75.     if(play((GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == false)
  76.     {
  77.       gameOver();
  78.       break;
  79.     }
  80.  
  81.     deleteRows();
  82.  
  83.     whichPiece = nextPiece;
  84.   }
  85.  
  86.   updateScreen();
  87.   return 0;
  88. }
  89.  
  90. void updateState(char nextPiece [4][4])
  91. {
  92.     int column, row;
  93.  
  94.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  95.   drawBoundaries();
  96.  
  97.     for(column = 0; column < GAME_WIDTH; column++)
  98.   {
  99.     for(row = 0; row < GAME_HEIGHT; row++)
  100.     {
  101.             if(gameArray[column][row][2] == 2)
  102.             {
  103.                 gameArray[column][row][2] = RED;
  104.             }
  105.  
  106.             filledRect(gameArray[column][row][0] + 1, gameArray[column][row][1] + 1,
  107.                  gameArray[column][row][0] + SQUARE_SIZE - 2, gameArray[column][row][1] + SQUARE_SIZE - 2, gameArray[column][row][2]);
  108.     }
  109.   }
  110.   showComingPiece(nextPiece);
  111. }
  112.  
  113. void drawBoundaries()
  114. {
  115.   filledRect(leftCorner_x - 5, leftCorner_y,
  116.              leftCorner_x - 1, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
  117.   filledRect(leftCorner_x, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE),
  118.              leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) - 1, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
  119.   filledRect(leftCorner_x + (GAME_WIDTH * SQUARE_SIZE), leftCorner_y,
  120.              leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) + 4, leftCorner_y + (GAME_HEIGHT * SQUARE_SIZE) + 4, WHITE);
  121.  
  122. }
  123.  
  124. void showComingPiece(char nextPiece[4][4])
  125. {
  126.     textout( leftCorner_x + (GAME_WIDTH * SQUARE_SIZE) + 11, leftCorner_y - 11, "next: ", BLUE);
  127.     int column, row, pieceColor;
  128.         pieceColor = MOVING_PIECE_COLOR;
  129.  
  130.     for(column = 0; column < 4; column++)
  131.     {
  132.       for(row = 0; row < 4; row++)
  133.         {
  134.         if (nextPiece[column][row] != 0)
  135.             {
  136.                     pieceColor = GREEN;
  137.           filledRect(gameArray[column][row][0] + (GAME_WIDTH * SQUARE_SIZE) + 11, gameArray[column][row][1] + 11,
  138.                      gameArray[column][row][0] + (GAME_WIDTH * SQUARE_SIZE) + 28, gameArray[column][row][1] + 28,  pieceColor);
  139.         }
  140.       }
  141.     }
  142. }
  143.  
  144. bool isPossible(int x, int y, char piece[4][4], char move)
  145. {
  146.     int column, row;
  147.  
  148.     switch(move)
  149.     {
  150.         case('l'):
  151.                 x--;
  152.                 break;
  153.         case('r'):
  154.                 x++;
  155.                 break;
  156.         case('d'):
  157.                 y++;
  158.                 break;
  159.         case('0'):
  160.           break;
  161.     }
  162.  
  163.     if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
  164.   {
  165.     return false;
  166.   }
  167.  
  168.     for(column = 0; column < 4; column++)
  169.   {
  170.     for(row = 0; row < 4; row++)
  171.         {
  172.             if(piece[column][row] != 0 && (x + column >= GAME_WIDTH || y + row >= GAME_HEIGHT))
  173.       {
  174.         return false;
  175.       }
  176.       if (piece[column][row] != 0 && gameArray[x + column][y + row][2] != 0)
  177.       {
  178.         return false;
  179.       }
  180.         }
  181.   }
  182.  
  183.     return true;
  184. }
  185.  
  186. void setPiece(int x, int y, char piece[4][4], enum color colour)
  187. {
  188.   int column, row, hue;
  189.     hue = colour;
  190.     for(column = 0; column < 4; column++)
  191.   {
  192.     for(row = 0; row < 4; row++)
  193.         {
  194.             hue = colour;
  195.       if(piece[column][row] != 0)
  196.             {
  197.                 if(piece[column][row] == 2)
  198.                 {
  199.                     hue = WHITE;
  200.                 }
  201.         filledRect(gameArray[x + column][y + row][0] + 1, gameArray[x + column][y + row][1] + 1,
  202.                    gameArray[x + column][y + row][0] + SQUARE_SIZE - 2, gameArray[x + column][y + row][1] + SQUARE_SIZE - 2, hue);
  203.       }
  204.     }
  205.   }
  206. }
  207.  
  208. void clearPiece(int x, int y, char piece[4][4])
  209. {
  210.   int column, row;
  211.     for(column = 0; column < 4; column++)
  212.   {
  213.     for(row = 0; row < 4; row++)
  214.         {
  215.       if(piece[column][row] != 0)
  216.             {
  217.         filledRect(gameArray[x + column][y + row][0] + 1, gameArray[x + column][y + row][1] + 1,
  218.                    gameArray[x + column][y + row][0] + SQUARE_SIZE - 2, gameArray[x + column][y + row][1] + SQUARE_SIZE - 2, BLACK);
  219.       }
  220.     }
  221.   }
  222. }
  223.  
  224. bool play(int x, int y, char whichPiece[4][4][4])
  225. {
  226.   int column, row, counter;
  227.     int rotation = 0;
  228.     int oldCenter_x, oldCenter_y, newCenter_x, newCenter_y;
  229.     int checker = 0;
  230.  
  231.     while(1)
  232.     {
  233.         for(counter = 0; counter < 10; counter++)
  234.         {
  235.             if(isKeyDown(SDLK_ESCAPE) == 1)
  236.       {
  237.         return false;
  238.       }
  239.  
  240.             switch(pollkey())
  241.             {
  242.                 case(SDLK_DOWN):
  243.                     while(isPossible(x, y, whichPiece[rotation], 'd') == true)
  244.                     {
  245.                         clearPiece(x, y, whichPiece[rotation]);
  246.                         y++;
  247.                         setPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  248.                         updateScreen();
  249.                     }
  250.                     goto saving;
  251.  
  252.                 case(SDLK_RIGHT):
  253.                     if(isPossible(x, y, whichPiece[rotation], 'r') == true)
  254.                     {
  255.                         clearPiece(x, y, whichPiece[rotation]);
  256.                         x++;
  257.                         setPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  258.                         updateScreen();
  259.                     }
  260.             break;
  261.  
  262.                 case(SDLK_LEFT):
  263.                     if(isPossible(x, y, whichPiece[rotation], 'l') == true)
  264.                     {
  265.                         clearPiece(x, y, whichPiece[rotation]);
  266.                         x--;
  267.                         setPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  268.                         updateScreen();
  269.                     }
  270.             break;
  271.  
  272.                 case(SDLK_SPACE):
  273.                     if(isPossible(x, y, whichPiece[(rotation + 1) % 4], '0'))
  274.                     {
  275.  
  276.                             counter = 0;
  277.                             for(column = 0; column < 4; column++)
  278.                             {
  279.                                 for(row = 0; row < 4; row++)
  280.                                 {
  281.                                     if(whichPiece[rotation][column][row] == 2)
  282.                                     {
  283.                                         oldCenter_x = column;
  284.                                         oldCenter_y = row;
  285.                                         counter += 1;
  286.                                         break;
  287.                                     }
  288.                                 }
  289.                                 if(counter != 0)
  290.                                 {
  291.                                     break;
  292.                                 }
  293.                             }
  294. printf("oldcenters %d %d\n", oldCenter_x, oldCenter_y );
  295.                         //  findOldCenter(rotation, whichPiece[rotation]);
  296.                             clearPiece(x, y, whichPiece[rotation]);
  297.                             rotation++;
  298.               rotation %= 4;
  299.                     //      findNewCenter(rotation, whichPiece[(rotation + 2) % 4]);
  300.  
  301.                             counter = 0;
  302.                             for(column = 0; column < 4; column++)
  303.                             {
  304.                                 for(row = 0; row < 4; row++)
  305.                                 {
  306.                                     if(whichPiece[rotation][column][row] == 2)
  307.                                     {
  308.                                         newCenter_x = column;
  309.                                         newCenter_y = row;
  310.                                         counter += 1;
  311.                                         break;
  312.                                     }
  313.                                 }
  314.                                 if(counter != 0)
  315.                                 {
  316.                                     break;
  317.                                 }
  318.                             }
  319.                             printf("newcenters: %d %d\n", newCenter_x, newCenter_y);
  320.  
  321.                             coordinatesOfCenter[0] = oldCenter_x - newCenter_x;
  322.                             coordinatesOfCenter[1] = oldCenter_y - newCenter_y;
  323.                             printf("roznica stare-nowe: %d, y coordinate: %d\n", coordinatesOfCenter[0], coordinatesOfCenter[1]);
  324.  
  325.                             x = x + coordinatesOfCenter[0];
  326.                             y = y + coordinatesOfCenter[1];
  327.  
  328.  
  329.                         setPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  330.                         updateScreen();
  331.                     }
  332.                     else if (isPossible(x - 1, y, whichPiece[(rotation + 1) % 4], '0'))
  333.                     {
  334.                             counter = 0;
  335.                             for(column = 0; column < 4; column++)
  336.                             {
  337.                                 for(row = 0; row < 4; row++)
  338.                                 {
  339.                                     if(whichPiece[rotation][column][row] == 2)
  340.                                     {
  341.                                         oldCenter_x = column;
  342.                                         oldCenter_y = row;
  343.                                         counter += 1;
  344.                                         break;
  345.                                     }
  346.                                 }
  347.                                 if(counter != 0)
  348.                                 {
  349.                                     break;
  350.                                 }
  351.                             }
  352.  
  353.                             clearPiece(x, y, whichPiece[rotation]);
  354.                         ++rotation;
  355.               rotation %= 4;
  356.                             //findCenter(rotation, whichPiece[4][4][4]);
  357.                             //x = coordinatesOfCenter[0];
  358.                             //y = coordinatesOfCenter[1];
  359.               x--;
  360.  
  361.                             counter = 0;
  362.                             for(column = 0; column < 4; column++)
  363.                             {
  364.                                 for(row = 0; row < 4; row++)
  365.                                 {
  366.                                     if(whichPiece[rotation][column][row] == 2)
  367.                                     {
  368.                                         newCenter_x = column;
  369.                                         newCenter_y = row;
  370.                                         counter += 1;
  371.                                         break;
  372.                                     }
  373.                                 }
  374.                                 if(counter != 0)
  375.                                 {
  376.                                     break;
  377.                                 }
  378.                             }
  379.                             coordinatesOfCenter[0] = oldCenter_x - newCenter_x;
  380.                             coordinatesOfCenter[1] = oldCenter_y - newCenter_y;
  381.                             printf("\nAFTER x coordinate: %d, y coordinate: %d\n", coordinatesOfCenter[0], coordinatesOfCenter[1]);
  382.  
  383.                             x = x + coordinatesOfCenter[0];
  384.                             y = y + coordinatesOfCenter[1];
  385.  
  386.                         setPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  387.                         updateScreen();
  388.                     }
  389.                     else if (isPossible(x + 1, y, whichPiece[(rotation + 1) % 4], '0'))
  390.                     {
  391.  
  392.                             counter = 0;
  393.                             for(column = 0; column < 4; column++)
  394.                             {
  395.                                 for(row = 0; row < 4; row++)
  396.                                 {
  397.                                     if(whichPiece[rotation][column][row] == 2)
  398.                                     {
  399.                                         oldCenter_x = column;
  400.                                         oldCenter_y = row;
  401.                                         counter += 1;
  402.                                         break;
  403.                                     }
  404.                                 }
  405.                                 if(counter != 0)
  406.                                 {
  407.                                     break;
  408.                                 }
  409.                             }
  410.  
  411.  
  412.                         clearPiece(x, y, whichPiece[rotation]);
  413.                         ++rotation;
  414.               rotation %= 4;
  415.                             //findCenter(rotation, whichPiece[4][4][4]);
  416.                             //x = coordinatesOfCenter[0];
  417.                             //y = coordinatesOfCenter[1];
  418.               x++;
  419.  
  420.                             counter = 0;
  421.                             for(column = 0; column < 4; column++)
  422.                             {
  423.                                 for(row = 0; row < 4; row++)
  424.                                 {
  425.                                     if(whichPiece[rotation][column][row] == 2)
  426.                                     {
  427.                                         newCenter_x = column;
  428.                                         newCenter_y = row;
  429.                                         counter += 1;
  430.                                         break;
  431.                                     }
  432.                                 }
  433.                                 if(counter != 0)
  434.                                 {
  435.                                     break;
  436.                                 }
  437.                             }
  438.                             coordinatesOfCenter[0] = oldCenter_x - newCenter_x;
  439.                             coordinatesOfCenter[1] = oldCenter_y - newCenter_y;
  440.                             printf("\nAFTER x coordinate: %d, y coordinate: %d\n", coordinatesOfCenter[0], coordinatesOfCenter[1]);
  441.  
  442.                             x = x + coordinatesOfCenter[0];
  443.                             y = y + coordinatesOfCenter[1];
  444.  
  445.                         setPiece(x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  446.                         updateScreen();
  447.                     }
  448.                     break;
  449.             }
  450.             usleep(TIME);
  451.         }
  452.         if(isPossible(x, y, whichPiece[rotation], 'd') == true)
  453.         {
  454.             clearPiece(x, y, whichPiece[rotation]);
  455.             y++;
  456.             setPiece(x,y,whichPiece[rotation], MOVING_PIECE_COLOR);
  457.             updateScreen();
  458.         }
  459.         else
  460.     {
  461.       break;
  462.     }
  463.     }
  464. saving:
  465.     for(column = 0; column < 4; column++)
  466.   {
  467.     for(row = 0; row < 4; row++)
  468.     {
  469.       if(whichPiece[rotation][column][row] != 0)
  470.       {
  471.         gameArray[x + column][y + row][2] = STATIC_PIECE_COLOR;
  472.       }
  473.     }
  474.   }
  475.     return true;
  476. }
  477. void findOldCenter(int rotation, char whichPiece[4][4][4])
  478. {
  479.     int oldCenter_x, oldCenter_y, column, row;
  480.     int counter = 0;
  481.     for(column = 0; column < 4; column++)
  482.     {
  483.         for(row = 0; row < 4; row++)
  484.         {
  485.             if(whichPiece[rotation][column][row] == 2)
  486.             {
  487.                 oldCenter_x = column;
  488.                 oldCenter_y = row;
  489.                 counter += 1;
  490.                 break;
  491.             }
  492.         }
  493.         if(counter != 0)
  494.         {
  495.             break;
  496.         }
  497.     }
  498.     printf("pierwsza funkcja oldcenters: %d %d\n", oldCenter_x, oldCenter_y);
  499.     coordinatesOfCenter[0] = oldCenter_x;
  500.     coordinatesOfCenter[1] = oldCenter_y;
  501.     printf("pierwsza funkcja wklada w array te wartosci: %d %d\n", coordinatesOfCenter[0], coordinatesOfCenter[1]);
  502.  
  503. }
  504. void findNewCenter(int rotation, char whichPiece[4][4][4])
  505. {
  506. int column, row, counter, newCenter_x, newCenter_y;
  507.     counter = 0;
  508.     for(column = 0; column < 4; column++)
  509.     {
  510.         for(row = 0; row < 4; row++)
  511.         {
  512.             if(whichPiece[rotation][column][row] == 2)
  513.             {
  514.                 newCenter_x = column;
  515.                 newCenter_y = row;
  516.                 counter += 1;
  517.                 break;
  518.             }
  519.         }
  520.         if(counter != 0)
  521.         {
  522.             break;
  523.         }
  524.     }
  525.  
  526.     printf("druga funkcja newcenters: %d %d\n", newCenter_x, newCenter_y);
  527.     coordinatesOfCenter[0] = coordinatesOfCenter[0] - newCenter_x;
  528.     coordinatesOfCenter[1] = coordinatesOfCenter[1] - newCenter_y;
  529.     printf("druga funkcja wklada w array te wartosci(po odjeciu stare-nowe): %d %d\n", coordinatesOfCenter[0], coordinatesOfCenter[1]);
  530.  
  531. }
  532.  
  533. void deleteRows()
  534. {
  535.   int column, row;
  536.     int deletedRow;
  537.     int checks = 1;
  538.  
  539.     for(row = 0; row < GAME_HEIGHT; row++)
  540.     {
  541.         for(column = 0; column < GAME_WIDTH; column++)
  542.         {
  543.             checks *= gameArray[column][row][2];
  544.             if (checks == 0)
  545.       {
  546.         break;
  547.       }
  548.         }
  549.         if(checks != 0)
  550.         {
  551.             for(deletedRow = row; deletedRow > 0; deletedRow--)
  552.       {
  553.         for(column = 0; column < GAME_WIDTH; column++)
  554.         {
  555.           gameArray[column][deletedRow][2] = gameArray[column][deletedRow - 1][2];
  556.         }
  557.       }
  558.  
  559.             for(column = 0; column < GAME_WIDTH; column++)
  560.       {
  561.         gameArray[column][0][2] = 0;
  562.       }
  563.         }
  564.         checks = 1;
  565.     }
  566. }
  567.  
  568. void gameOver()
  569. {
  570.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
  571.     textout((screenWidth() - 1) / 2 - 60, (screenHeight() - 1) / 2, "GAME OVER", BLUE);
  572.     updateScreen();
  573.     sleep(GAME_OVER_TIME);
  574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement