Advertisement
Mary_99

TETRIS BETTER ROTATION

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