Advertisement
Mary_99

tetris 2.0

Apr 23rd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <unistd.h>//usleep  suspends the performance for a period of time expressed in microseconds
  3. #include <stdio.h>
  4. #include "primlib.h"
  5. #include "pieces.inl"
  6. #include <stdbool.h>
  7.  
  8. #define SQUARE_SIZE 20
  9. #define GAME_WIDTH 15
  10. #define GAME_HEIGHT 20
  11. #define MOVING_PIECE_COLOR MAGENTA
  12. #define STATIC_PIECE_COLOR BLUE
  13. #define PICES_TIME 20000
  14. #define SLEEP 3
  15. #define ARENA_FRAME_COLOUR WHITE
  16. #define GROUND_COLOR BLACK
  17. #define TEXT_COLOR MAGENTA
  18. #define ROTATION_COLOR RED
  19. #define X_LEFT_UPPER_CORDINATE 100
  20. #define Y_LEFT_UPPER_CORDINATE 60
  21.  
  22. void playMainGameLoop();
  23. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[4][4], char move);
  24. int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4]);
  25. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char thatPiece[4][4], enum color colour, enum color color);
  26. void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4]);
  27. void checkingBoundry();
  28. bool deleteRows(int area[GAME_WIDTH][GAME_HEIGHT][3]);
  29. void endingGame();
  30. void DrawingBackground();
  31. void findCenter(int rotation, char whichPiece[4][4][4]);
  32.  
  33. int coordinatesOfCenter[2];
  34.  
  35. int main ()
  36. {
  37.     int columnCounter;
  38.     int rowCounter;
  39.     int area[GAME_WIDTH][GAME_HEIGHT][3];
  40.     int whichPiece;
  41.     int nextPiece;
  42.    
  43.     initGraph();
  44.     playMainGameLoop();
  45.     updateScreen();
  46.     return 0;
  47. }
  48.  
  49.  
  50. void playMainGameLoop()
  51. {
  52.     int columnCounter;
  53.     int rowCounter;
  54.     int area[GAME_WIDTH][GAME_HEIGHT][3];
  55.     int whichPiece;
  56.     int nextPiece;
  57.    
  58.     for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  59.     {
  60.         for(rowCounter=0;rowCounter<GAME_HEIGHT;rowCounter++)
  61.         {
  62.             area [columnCounter][rowCounter][0] = X_LEFT_UPPER_CORDINATE + columnCounter * SQUARE_SIZE;
  63.             area [columnCounter][rowCounter][1] = Y_LEFT_UPPER_CORDINATE + rowCounter * SQUARE_SIZE;
  64.             area [columnCounter][rowCounter][2] = 0;
  65.         }
  66.     }
  67.     whichPiece = (int)rand()%7;
  68.    
  69.     while(1)
  70.     {
  71.         nextPiece = (int)rand()%7;
  72.         updatingState(area, pieces[nextPiece][0]);
  73.         updateScreen();
  74.         if(isPossible(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == 0)
  75.         {
  76.             endingGame();
  77.             break;
  78.         }
  79.         startingPiecePosition(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR, ROTATION_COLOR);
  80.         updateScreen();
  81.  
  82.         if(play(area,(GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
  83.         {
  84.             endingGame();
  85.             break;
  86.         }
  87.        
  88.         deleteRows(area);
  89.         whichPiece = nextPiece;
  90.     }
  91.  
  92. }
  93.  
  94.  
  95. void checkingBoundry()
  96. {
  97.     if(GAME_WIDTH < 10 || GAME_WIDTH > 20)
  98.     {
  99.         printf("Invalid GAME_WIDTH \n");
  100.         exit(3);
  101.     }
  102.  
  103.     if(GAME_HEIGHT < 10 || GAME_HEIGHT >20)
  104.     {
  105.         printf("Invalid GAME_HEIGHT\n");
  106.         exit(3);
  107.     }
  108. }
  109.  
  110.  
  111. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x,int y,char piece[4][4], char move)
  112. {
  113.     int columnCounter;
  114.     int rowCounter;
  115.  
  116.     switch(move)
  117.     {
  118.         case('l'):
  119.             x--;
  120.             break;
  121.         case('r'):
  122.             x++;
  123.             break;
  124.         case('d'):
  125.             y++;
  126.             break;
  127.         case('0'):
  128.             break;
  129.     };
  130.    
  131.     if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
  132.     {
  133.         return 0;
  134.     }
  135.    
  136.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  137.     {
  138.         for(rowCounter = 0 ; rowCounter < 4; rowCounter++)
  139.         {
  140.             if(piece[columnCounter][rowCounter]!= 0 && (x + columnCounter >= GAME_WIDTH || y + rowCounter >= GAME_HEIGHT))
  141.             {
  142.            
  143.                 return 0; //piece out of the game area
  144.             }
  145.             if ((piece[columnCounter][rowCounter]!= 0) && (area[x + columnCounter][y + rowCounter][2]!= 0))
  146.             {
  147.                 return 0; // piece reaches the non-empty place
  148.             }
  149.         }
  150.     }
  151.        
  152.     return 1;
  153. }
  154.  
  155. bool deleteRows(int area[GAME_WIDTH][GAME_HEIGHT][3])
  156. {
  157.     int columnCounter;
  158.     int rowCounter;
  159.     int deletedRow;
  160.     int checkingEmptySpot;
  161.  
  162.     for(rowCounter = 0; rowCounter < GAME_HEIGHT; rowCounter++)
  163.     {
  164.         for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  165.         {
  166.             checkingEmptySpot*= area[columnCounter][rowCounter][2]; //because the black colour is'0' value
  167.             if (checkingEmptySpot == false)
  168.             {
  169.                 break; // if empty spot we not need  check anything more
  170.        
  171.             }
  172.         }
  173.         if(checkingEmptySpot != 0)
  174.         {
  175.             for(deletedRow = rowCounter; deletedRow > 0; deletedRow--)
  176.             {
  177.                 for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  178.                 {
  179.                     area[columnCounter][deletedRow][2] = area[columnCounter][deletedRow - 1][2];
  180.                 }
  181.             }
  182.             for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  183.             {
  184.                 area[columnCounter][0][2]=0;
  185.             }
  186.  
  187.         }
  188.         checkingEmptySpot = true;
  189.     }
  190. }
  191.  
  192.  
  193. int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4])
  194. {
  195.     int columnCounter;
  196.     int rowCounter;
  197.     int turnCounter;
  198.     int rotation = 0;
  199.     int oldCenter_x;
  200.     int oldCenter_y, newCenter_x, newCenter_y;
  201.  
  202.     while(1)
  203.     {
  204.         for(turnCounter = 0; turnCounter < 10; turnCounter++) //10moves 0.1s each,gives 1s time for the movement downward
  205.         {
  206.             if(isKeyDown('e') == 1) return 0; // to quit the game
  207.             switch(pollkey())
  208.             {
  209.                 case(SDLK_DOWN):
  210.                     while(isPossible(area, x, y, whichPiece[rotation], 'd') == 1)
  211.                     {
  212.                         y++;
  213.                         startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  214.                         updateScreen();
  215.                     }
  216.                     goto storingPices;
  217.  
  218.                 case(SDLK_RIGHT):
  219.                     if(isPossible(area, x, y, whichPiece[rotation], 'r') == 1)
  220.                     {
  221.                         startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, ROTATION_COLOR);
  222.                         x++;
  223.                         startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  224.                         updateScreen();
  225.                     }break;
  226.                 case(SDLK_LEFT):
  227.                     if(isPossible(area, x, y, whichPiece[rotation],'l') == 1)
  228.                     {
  229.                         startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, ROTATION_COLOR);
  230.                         x--;
  231.                         startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  232.                         updateScreen();
  233.                     }break;
  234.                    
  235.                 case(SDLK_SPACE):
  236.                     if(isPossible(area, x, y, whichPiece[(rotation + 1) % 4],'0'))
  237.                     {
  238.            
  239.                     int counter = 0;
  240.                     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  241.                     {
  242.                         for(rowCounter = 0; rowCounter < 4; rowCounter++)
  243.                         {
  244.                             if(whichPiece[rotation][columnCounter][rowCounter] == 2)
  245.                             {
  246.                                 oldCenter_x = columnCounter;
  247.                                 oldCenter_y = rowCounter;
  248.                                 counter += 1;
  249.                                 break;
  250.                             }
  251.                         }
  252.                         if(counter != 0)
  253.                         {
  254.                             break;
  255.                         }
  256.                     }
  257.                    
  258.                     startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, ROTATION_COLOR);
  259.                     ++rotation;
  260.                     rotation %= 4;
  261.                
  262.                     counter = 0;
  263.                     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  264.                     {
  265.                         for(rowCounter = 0; rowCounter < 4; rowCounter++)
  266.                         {
  267.                             if(whichPiece[rotation][columnCounter][rowCounter] == 2)
  268.                             {
  269.                                 newCenter_x = columnCounter;
  270.                                 newCenter_y = rowCounter;
  271.                                 counter += 1;
  272.                                 break;
  273.                             }
  274.                         }
  275.                         if(counter != 0)
  276.                         {
  277.                             break;
  278.                         }
  279.                     }
  280.                    
  281.                 coordinatesOfCenter[0] = oldCenter_x - newCenter_x;
  282.                 coordinatesOfCenter[1] = oldCenter_y - newCenter_y;
  283.                 x = x + coordinatesOfCenter[0];
  284.                 y = y + coordinatesOfCenter[1];
  285.                
  286.                 startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  287.                 updateScreen();
  288.                 }
  289.                 else if (isPossible(area, x - 1, y, whichPiece[(rotation + 1) % 4],'0'))
  290.                 {
  291.                 startingPiecePosition(area,x , y, whichPiece[rotation], GROUND_COLOR, ROTATION_COLOR);
  292.                 ++rotation;
  293.                 rotation %= 4;
  294.                 findCenter(rotation, whichPiece[4][4][4]);
  295.                 x = coordinatesOfCenter[0];
  296.                 y = coordinatesOfCenter[1];
  297.                 x--;
  298.                 startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  299.                 updateScreen();
  300.                 }
  301.                 else if (isPossible(area, x + 1, y, whichPiece[(rotation + 1) % 4], '0'))
  302.                 {
  303.                 startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR, ROTATION_COLOR);
  304.                 ++rotation;
  305.                 rotation %= 4;
  306.                 findCenter(rotation, whichPiece[4][4][4]);
  307.                 x = coordinatesOfCenter[0];
  308.                 y = coordinatesOfCenter[1];
  309.                 x++;
  310.                 startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  311.                 updateScreen();
  312.                 }
  313.                 break;
  314.                 };
  315.                 usleep(PICES_TIME);
  316.             }
  317.             if(isPossible(area, x, y, whichPiece[rotation],'d') == 1)
  318.             {
  319.                 startingPiecePosition(area, x, y,whichPiece[rotation], GROUND_COLOR, ROTATION_COLOR);
  320.                 y++;
  321.                 startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR, ROTATION_COLOR);
  322.                 updateScreen();
  323.             }
  324.             else break;
  325.         }
  326. storingPices:
  327.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  328.     {
  329.         for(rowCounter = 0;rowCounter < 4; rowCounter++)
  330.         {
  331.             if(whichPiece[rotation][columnCounter][rowCounter] != 0)
  332.             {
  333.                 area[x+columnCounter][y+rowCounter][2] = STATIC_PIECE_COLOR;
  334.             }
  335.         }
  336.     }
  337.     return 1;
  338. }
  339.  
  340. void DrawingBackground()
  341. {
  342.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, GROUND_COLOR);
  343.  
  344.     textout(0, 10, "Use arrows on your keyboard to move the piece and space to rotate it.", TEXT_COLOR);
  345.     textout(0, 20, "To Accelerate the pice press downn arrow.TO END THE GAME PRESS 'e'", TEXT_COLOR);
  346.    
  347.  
  348.     filledRect(X_LEFT_UPPER_CORDINATE - 5, Y_LEFT_UPPER_CORDINATE, X_LEFT_UPPER_CORDINATE - 1, Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE) + 4,
  349.                ARENA_FRAME_COLOUR);
  350.  
  351.     filledRect(X_LEFT_UPPER_CORDINATE, Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE), X_LEFT_UPPER_CORDINATE + (GAME_WIDTH * SQUARE_SIZE) - 1,
  352.                Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
  353.    
  354.     filledRect(X_LEFT_UPPER_CORDINATE + (GAME_WIDTH * SQUARE_SIZE), Y_LEFT_UPPER_CORDINATE, X_LEFT_UPPER_CORDINATE + (GAME_WIDTH*SQUARE_SIZE) + 4,
  355.                Y_LEFT_UPPER_CORDINATE + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
  356. }
  357.  
  358. void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4])
  359. {
  360.     int columnCounter;
  361.     int rowCounter;
  362.    
  363.     DrawingBackground();
  364.  
  365.     //StaticPices
  366.     for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  367.     {
  368.         for(rowCounter = 0; rowCounter < GAME_HEIGHT; rowCounter++)
  369.         {
  370.            filledRect(area[columnCounter][rowCounter][0] + 1, area[columnCounter][rowCounter][1] + 1,
  371.                       area[columnCounter][rowCounter][0] + SQUARE_SIZE - 2, area[columnCounter][rowCounter][1] + SQUARE_SIZE - 2,
  372.                       area[columnCounter][rowCounter][2]);
  373.         }
  374.     }
  375.  
  376.     textout( X_LEFT_UPPER_CORDINATE + (GAME_WIDTH * 20) + 11, Y_LEFT_UPPER_CORDINATE, "next piece: ", TEXT_COLOR);
  377.    
  378.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  379.     {
  380.         for(rowCounter = 0; rowCounter < 4; rowCounter++)
  381.         {
  382.             if (nextPiece[columnCounter][rowCounter]!= 0)
  383.             {
  384.                 filledRect(area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 11, area[columnCounter][rowCounter][1] + 11,
  385.                            area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 28, area[columnCounter][rowCounter][1] + 28,
  386.                            MOVING_PIECE_COLOR);
  387.             }
  388.        }
  389.     }
  390. }
  391.  
  392.  
  393. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[4][4], enum color colour, enum color color)
  394. // enum is the keyword to declare a new enumeration type. color is the tag name that you can use later as a type name.
  395. {
  396.     int columnCounter;
  397.     int rowCounter;
  398.  
  399.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  400.     {
  401.         for(rowCounter = 0;rowCounter < 4; rowCounter++)
  402.         {
  403.             if(piece[columnCounter][rowCounter] != 0) /*draws pices of when their colour is other than black */
  404.             {
  405.                 filledRect(area[x + columnCounter][y + rowCounter][0] + 1, area[x + columnCounter][y + rowCounter][1] + 1,
  406.                            area[x + columnCounter][y + rowCounter][0] + SQUARE_SIZE - 2, area[x + columnCounter][y + rowCounter][1] + SQUARE_SIZE - 2, colour);
  407.             }
  408.         }
  409.     }
  410. }
  411.  
  412.  
  413. void endingGame()
  414. {
  415.     filledRect(0, 0,screenWidth() - 1, screenHeight() -1, GROUND_COLOR);  
  416.     textout(280, 240, "Game over , sorry  :( ", TEXT_COLOR );
  417.     updateScreen();
  418.     sleep(SLEEP);
  419. }
  420. void findCenter(int rotation, char whichPiece[4][4][4])
  421. {
  422.     int oldCenter_x, oldCenter_y, column, row;
  423.     int counter = 0;
  424.     for(column = 0; column < GAME_WIDTH; column++)
  425.     {
  426.         for(row = 0; row < GAME_HEIGHT; row++)
  427.         {
  428.             if(whichPiece[rotation][column][row] == 2)
  429.             {
  430.                 oldCenter_x = column;
  431.                 oldCenter_y = row;
  432.                 counter += 1;
  433.                 break;
  434.             }
  435.         }
  436.         if(counter != 0)
  437.         {
  438.             break;
  439.         }
  440.     }
  441.     coordinatesOfCenter[0] = oldCenter_x;
  442.     coordinatesOfCenter[1] = oldCenter_y;
  443.  
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement