Advertisement
Monika__

tetris-wymaga popraw estetycznych

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