Advertisement
Mary_99

tetris do odania

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