SHARE
TWEET

tetris backup

Mary_99 Apr 10th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. #include "primlib.h"
  5. #include "pieces.inl"
  6.  
  7. #define SQUARE_SIZE 20
  8. #define GAME_WIDTH 15
  9. #define GAME_HEIGHT 20
  10. #define MOVING_PIECE_COLOR MAGENTA
  11. #define STATIC_PIECE_COLOR BLUE
  12. #define PICES_TIME 20000
  13. #define SLEEP 3
  14.  
  15. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[4][4], char move);
  16. int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4]);
  17. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char thatPiece[4][4], enum color colour);
  18. void endingGame();
  19. void checkingBoundry();
  20.  
  21. //------global variables ---------//
  22. int xLeftUpperCordinate = 100;
  23. int yLeftUpperCordinate = 65;
  24.  
  25. int main ()
  26. {
  27.     int columnCounter;
  28.     int rowCounter;
  29.     int area[GAME_WIDTH][GAME_HEIGHT][3];
  30.     int whichPiece;
  31.     int nextPiece;
  32.  
  33.     initGraph();
  34.  
  35.     for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  36.     {
  37.         for(rowCounter=0;rowCounter<GAME_HEIGHT;rowCounter++)
  38.         {
  39.             area [columnCounter][rowCounter][0] = xLeftUpperCordinate + columnCounter * SQUARE_SIZE;
  40.             area [columnCounter][rowCounter][1] = yLeftUpperCordinate + rowCounter * SQUARE_SIZE;
  41.             area [columnCounter][rowCounter][2] = 0;
  42.         }
  43.     }
  44.     whichPiece = (int)rand()%7;
  45.    
  46.     while(1)
  47.     {
  48.         nextPiece = (int)rand()%7;
  49.         updateScreen();
  50.         if(isPossible(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == 0)
  51.         {
  52.             endingGame();
  53.             break;
  54.         }
  55.         startingPiecePosition(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR);
  56.         updateScreen();
  57.  
  58.         if(play(area,(GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
  59.         {
  60.             endingGame();
  61.             break;
  62.         }
  63.         whichPiece = nextPiece;
  64.     }
  65.     updateScreen();
  66.     return 0;
  67. }
  68.  
  69. void checkingBoundry()
  70. {
  71.     if(GAME_WIDTH < 10 || GAME_WIDTH > 20)
  72.     {
  73.         printf("Invalid GAME_WIDTH \n");
  74.         exit(3);
  75.     }
  76.  
  77.     if(GAME_HEIGHT < 10 || GAME_HEIGHT >20)
  78.     {
  79.         printf("Invalid GAME_HEIGHT\n");
  80.         exit(3);
  81.     }
  82. }
  83.  
  84.  
  85. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x,int y,char piece[4][4], char move)
  86. {
  87.     int columnCounter;
  88.     int rowCounter;
  89.  
  90.     switch(move)
  91.     {
  92.         case('l'):
  93.             x--;
  94.             break;
  95.         case('r'):
  96.             x++;
  97.             break; 
  98.         case('d'):
  99.             y++;
  100.             break;
  101.         case('0'):
  102.             break;
  103.     };
  104.    
  105.     if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
  106.     {
  107.         return 0;
  108.     }
  109.    
  110.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  111.     {
  112.         for(rowCounter = 0 ; rowCounter < 4; rowCounter++)
  113.         {
  114.             if(piece[columnCounter][rowCounter]!= 0 && (x + columnCounter >= GAME_WIDTH || y + rowCounter >= GAME_HEIGHT))
  115.             {
  116.            
  117.                 return 0; /*  piece out of the game area */
  118.             }
  119.             if ((piece[columnCounter][rowCounter]!= 0) && (area[x + columnCounter][y + rowCounter][2]!= 0))
  120.             {
  121.                 return 0; /*piece reaches the non-empty place */
  122.             }
  123.         }
  124.     }
  125.        
  126.     return 1;
  127. }
  128.  
  129.  
  130. int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4])
  131. {
  132.     int columnCounter;
  133.     int rowCounter;
  134.     int turnCounter;
  135.     int rotation = 0;
  136.  
  137.     while(1)
  138.     {
  139.         for(turnCounter = 0; turnCounter < 10; turnCounter++) /* 10 truns (to move the piece before it moves down) 0.1s each, which gives 1s time for the movement downward*/
  140.         {
  141.             if(isKeyDown('q')==1) return 0; /* in order to quit the game */
  142.             switch(pollkey())
  143.             {
  144.                 case(SDLK_DOWN): /*accelerated movement downward*/
  145.                     while(isPossible(area,x,y,whichPiece[rotation],'d')==1)
  146.                     {
  147.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  148.                         y++; /* move downward */   
  149.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  150.                         updateScreen();
  151.                     }
  152.                     goto saving;
  153.  
  154.                 case(SDLK_RIGHT): /* move to the right */
  155.                     if(isPossible(area,x,y,whichPiece[rotation],'r')==1)
  156.                     {
  157.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK); /* draw the black piece */
  158.                         x++;/* move to the right */
  159.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  160.                         updateScreen();
  161.                     }break;
  162.                 case(SDLK_LEFT): /* move to the left*/
  163.                     if(isPossible(area,x,y,whichPiece[rotation],'l')==1)
  164.                     {
  165.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  166.                         x--; /* move left*/
  167.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  168.                         updateScreen();
  169.                     }break;
  170.                 case(SDLK_SPACE):
  171.            
  172.                     if(isPossible(area,x,y,whichPiece[(rotation+1)%4],'0'))
  173.                     {  
  174.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  175.                         ++rotation;/* rotate */rotation%=4;
  176.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  177.                         updateScreen();
  178.                     }
  179.                     /* 'else-if' checks if the movement to right or left enables the rotation */
  180.                     else if (isPossible(area,x-1,y,whichPiece[(rotation+1)%4],'0'))
  181.                     {
  182.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  183.                         ++rotation;/* rotate */rotation%=4; x--;
  184.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  185.                         updateScreen();
  186.                     }
  187.                     else if (isPossible(area,x+1,y,whichPiece[(rotation+1)%4],'0'))
  188.                     {
  189.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  190.                         ++rotation;/* rotate */rotation%=4; x++;
  191.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  192.                         updateScreen();
  193.                     }
  194.                     break;
  195.             };
  196.             usleep(PICES_TIME); /*This function makes the programm to wait for PICES_TIME before proceeding with other instructions */
  197.         }
  198.         if(isPossible(area,x,y,whichPiece[rotation],'d')==1)
  199.         {
  200.             startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  201.             y++; /*not accelerated movement downward */
  202.             startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  203.             updateScreen();
  204.         }
  205.         else break; /* if it is impossible to move the piece downward the piece's place is stored and may not be changed any more */
  206.     }
  207. saving: /* the piece's storage, we can jump to this etiquette pressing SDLK_DOWN key  */
  208.     for(columnCounter=0;columnCounter<4;columnCounter++)
  209.         for(rowCounter=0;rowCounter<4;rowCounter++)
  210.             if(whichPiece[rotation][columnCounter][rowCounter]!=0)
  211.                 area[x+columnCounter][y+rowCounter][2]=STATIC_PIECE_COLOR;
  212.     return 1;
  213. }
  214.  
  215.  
  216. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char piece[4][4],enum color colour)// enum is the keyword to declare a new enumeration type. color is the tag name that you can use later as a type name.
  217. {
  218.     int columnCounter;
  219.     int rowCounter;
  220.  
  221.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  222.     {
  223.         for(rowCounter = 0;rowCounter < 4; rowCounter++)
  224.         {
  225.             if(piece[columnCounter][rowCounter] != 0) /*draws pices of when their colour is other than black */
  226.             {
  227.                 filledRect(area[x + columnCounter][y + rowCounter][0] + 1, area[x + columnCounter][y + rowCounter][1] + 1,
  228.                            area[x + columnCounter][y + rowCounter][0] + SQUARE_SIZE - 2, area[x + columnCounter][y + rowCounter][1] + SQUARE_SIZE - 2, colour);
  229.             }
  230.         }
  231.     }
  232. }
  233.  
  234.  
  235. void endingGame()
  236. {
  237.     char result[31];
  238.    
  239.     filledRect(0, 0,screenWidth() - 1, screenHeight() -1, BLACK);
  240.     sprintf(result, "    GAME OVER !!!");
  241.     textout((screenWidth() - 1) / 2 - 100,(screenHeight() - 1) / 2, result, MAGENTA);
  242.     updateScreen();
  243.     sleep(SLEEP);
  244. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top