SHARE
TWEET

tetris 1

Mary_99 Apr 10th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <stdio.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 MAGENTA
  14. #define STATIC_PIECE_COLOR BLUE
  15. #define PICES_TIME 20000
  16. #define SLEEP 3
  17.  
  18.  
  19. void updatingState (int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece[4][4]);
  20. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[4][4], char move);
  21. int play(int area[GAME_WIDTH][GAME_HEIGHT][3],int x,int y, char whichPiece[4][4][4]);
  22. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3],int x,int y, char thatPiece[4][4], enum color colour);
  23. /*after checking the movement 'legality' deplace the piece*/
  24.  
  25. void endingGame();
  26. void checkingBoundry();
  27.  
  28.  
  29. int xLeftUpperCordinate = 100;
  30. int yLeftUpperCordinate = 65;
  31.  
  32.  
  33.  
  34. int main ()
  35. {
  36.     int columnCounter;
  37.     int rowCounter;
  38.     int area[GAME_WIDTH][GAME_HEIGHT][3];
  39.     int whichPiece;
  40.     int nextPiece;
  41.  
  42.     initGraph();
  43.  
  44.     for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  45.     {
  46.         for(rowCounter=0;rowCounter<GAME_HEIGHT;rowCounter++)
  47.         {
  48.             area [columnCounter][rowCounter][0] = xLeftUpperCordinate + columnCounter * SQUARE_SIZE;
  49.             area [columnCounter][rowCounter][1] = yLeftUpperCordinate + rowCounter * SQUARE_SIZE;
  50.             area [columnCounter][rowCounter][2] = 0;
  51.         }
  52.     }
  53.     whichPiece = (int)rand()%7;
  54.    
  55.     while(1)
  56.     {
  57.         nextPiece = (int)rand()%7;
  58.        
  59.         updatingState(area, pieces[nextPiece][0]);
  60.         updateScreen();
  61.  
  62.         if(isPossible(area,(GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == 0)
  63.         {
  64.             endingGame();
  65.             break;
  66.         }
  67.  
  68.         startingPiecePosition(area,(GAME_WIDTH/2)-1,0,pieces[whichPiece][0], MOVING_PIECE_COLOR);
  69.         /*Puts the piece at the center of upper frame of the game area*/
  70.         updateScreen();
  71.  
  72.         if(play(area,(GAME_WIDTH/2)-1,0,pieces[whichPiece])==0){
  73.             endingGame();
  74.             break;
  75.         }
  76.  
  77.        
  78.  
  79.         whichPiece = nextPiece;
  80.     }
  81.  
  82.     updateScreen();
  83.     return 0;
  84. }
  85.  
  86.  
  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. void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4])
  109. {
  110.     int columnCounter,rowCounter; /*counters while working on arrays */
  111.     char result[31];
  112.  
  113.  
  114.     filledRect(0,0,screenWidth()-1,screenHeight()-1,BLACK); /*clears the screen*/
  115.  
  116.     textout(0,10,"Use arrowCounters to move the piece and space to rotate it. Tdimensionso quit press 'q'.",WHITE);
  117.     textout(0,20,"You will receive additional points for cancelling multiple rowCounters.",WHITE);
  118.     /*Instruction on how to play*/
  119.  
  120.     filledRect(xLeftUpperCordinate-5, yLeftUpperCordinate, xLeftUpperCordinate-1, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE)+4, WHITE);/* the frame around the area dependingGameing on its size */
  121.     filledRect(xLeftUpperCordinate, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE), xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE)-1, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE)+4,WHITE);
  122.     filledRect(xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE), yLeftUpperCordinate, xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE)+4, yLeftUpperCordinate+(GAME_HEIGHT*SQUARE_SIZE)+4, WHITE);
  123.  
  124.     /* the static pieces on the game's area*/
  125.     for(columnCounter=0;columnCounter<GAME_WIDTH;columnCounter++)
  126.         for(rowCounter=0;rowCounter<GAME_HEIGHT;rowCounter++)
  127.             filledRect(area[columnCounter][rowCounter][0]+1, area[columnCounter][rowCounter][1]+1, area[columnCounter][rowCounter][0]+SQUARE_SIZE-2, area[columnCounter][rowCounter][1]+SQUARE_SIZE-2, area[columnCounter][rowCounter][2]);
  128.  
  129.     /* the preview of the next piece will be placed on the right from the game's area*/
  130.     textout( xLeftUpperCordinate+(GAME_WIDTH*20)+11, yLeftUpperCordinate,"next piece: ",WHITE);
  131.     for(columnCounter=0;columnCounter<4;columnCounter++)
  132.         for(rowCounter=0;rowCounter<4;rowCounter++)
  133.             if (nextPiece[columnCounter][rowCounter]!=0)
  134.                 filledRect(area[columnCounter][rowCounter][0]+(GAME_WIDTH*SQUARE_SIZE)+11, area[columnCounter][rowCounter][1]+11, area[columnCounter][rowCounter][0]+(GAME_WIDTH*SQUARE_SIZE)+28, area[columnCounter][rowCounter][1]+28,  MOVING_PIECE_COLOR);
  135.    
  136.     sprintf(result,"Your score is: %d");
  137.  
  138.     textout(xLeftUpperCordinate+(GAME_WIDTH*SQUARE_SIZE)+11,yLeftUpperCordinate+100,result,WHITE);
  139. }
  140.  
  141.  
  142. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char piece[4][4], char move)
  143. {
  144.     int columnCounter;
  145.     int rowCounter;
  146.  
  147.     switch(move)
  148.     {
  149.         case('l'):
  150.             x--;
  151.             break;
  152.         case('r'):
  153.             x++;
  154.             break; 
  155.         case('d'):
  156.             y++;
  157.             break;
  158.         case('0'): break;
  159.     };
  160.    
  161.     if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
  162.     {
  163.         return 0; /* if index is out of boundary the movement is not possible */
  164.     }
  165.    
  166.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  167.     {
  168.         for(rowCounter = 0 ; rowCounter < 4; rowCounter++)
  169.         {
  170.             if(piece[columnCounter][rowCounter]!= 0 && (x + columnCounter >= GAME_WIDTH || y + rowCounter >= GAME_HEIGHT))
  171.             {
  172.            
  173.                 return 0; /* if the piece is out of the game area */
  174.             }
  175.             if ((piece[columnCounter][rowCounter]!= 0) && (area[x + columnCounter][y + rowCounter][2]!= 0))
  176.             {
  177.                 return 0; /* if the piece reaches the non-empty place */
  178.             }
  179.         }
  180.     }
  181.        
  182.     return 1;
  183. }
  184.  
  185.  
  186.  
  187. int play(int area[GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char whichPiece[4][4][4])
  188. {
  189.     int columnCounter;
  190.     int rowCounter;
  191.     int turnCounter;
  192.     int rotation = 0;
  193.  
  194.     while(1)
  195.     {
  196.         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*/
  197.         {
  198.             if(isKeyDown('q')==1) return 0; /* in order to quit the game */
  199.             switch(pollkey())
  200.             {
  201.                 case(SDLK_DOWN): /*accelerated movement downward*/
  202.                     while(isPossible(area,x,y,whichPiece[rotation],'d')==1)
  203.                     {
  204.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  205.                         y++; /* move downward */   
  206.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  207.                         updateScreen();
  208.                     }
  209.                     goto saving;
  210.  
  211.                 case(SDLK_RIGHT): /* move to the right */
  212.                     if(isPossible(area,x,y,whichPiece[rotation],'r')==1)
  213.                     {
  214.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK); /* draw the black piece */
  215.                         x++;/* move to the right */
  216.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  217.                         updateScreen();
  218.                     }break;
  219.                 case(SDLK_LEFT): /* move to the left*/
  220.                     if(isPossible(area,x,y,whichPiece[rotation],'l')==1)
  221.                     {
  222.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  223.                         x--; /* move left*/
  224.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  225.                         updateScreen();
  226.                     }break;
  227.                 case(SDLK_SPACE):
  228.            
  229.                     if(isPossible(area,x,y,whichPiece[(rotation+1)%4],'0'))
  230.                     {  
  231.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  232.                         ++rotation;/* rotate */rotation%=4;
  233.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  234.                         updateScreen();
  235.                     }
  236.                     /* 'else-if' checks if the movement to right or left enables the rotation */
  237.                     else if (isPossible(area,x-1,y,whichPiece[(rotation+1)%4],'0'))
  238.                     {
  239.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  240.                         ++rotation;/* rotate */rotation%=4; x--;
  241.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  242.                         updateScreen();
  243.                     }
  244.                     else if (isPossible(area,x+1,y,whichPiece[(rotation+1)%4],'0'))
  245.                     {
  246.                         startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  247.                         ++rotation;/* rotate */rotation%=4; x++;
  248.                         startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  249.                         updateScreen();
  250.                     }
  251.                     break;
  252.             };
  253.             usleep(PICES_TIME); /*This function makes the programm to wait for PICES_TIME before proceeding with other instructions */
  254.         }
  255.         if(isPossible(area,x,y,whichPiece[rotation],'d')==1)
  256.         {
  257.             startingPiecePosition(area,x,y,whichPiece[rotation],BLACK);/* draw the black piece */
  258.             y++; /*not accelerated movement downward */
  259.             startingPiecePosition(area,x,y,whichPiece[rotation], MOVING_PIECE_COLOR);/* draw the coloured piece */
  260.             updateScreen();
  261.         }
  262.         else break; /* if it is impossible to move the piece downward the piece's place is stored and may not be changed any more */
  263.     }
  264. saving: /* the piece's storage, we can jump to this etiquette pressing SDLK_DOWN key  */
  265.     for(columnCounter=0;columnCounter<4;columnCounter++)
  266.         for(rowCounter=0;rowCounter<4;rowCounter++)
  267.             if(whichPiece[rotation][columnCounter][rowCounter]!=0)
  268.                 area[x+columnCounter][y+rowCounter][2]=STATIC_PIECE_COLOR;
  269.     return 1;
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3],int x,int y,char piece[4][4],enum color colour)
  277. {
  278.     int columnCounter;
  279.     int rowCounter;
  280.  
  281.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  282.     {
  283.         for(rowCounter = 0;rowCounter < 4; rowCounter++)
  284.         {
  285.             if(piece[columnCounter][rowCounter] != 0) /*draws pices of when their colour is other than black */
  286.             {
  287.                 filledRect(area[x + columnCounter][y + rowCounter][0] + 1, area[x + columnCounter][y + rowCounter][1] + 1,
  288.                            area[x + columnCounter][y + rowCounter][0] + SQUARE_SIZE - 2, area[x + columnCounter][y + rowCounter][1] + SQUARE_SIZE - 2, colour);
  289.             }
  290.         }
  291.     }
  292. }
  293.  
  294.  
  295. void endingGame()
  296. {
  297.     char result[31];// why 31?
  298.    
  299.     filledRect(0, 0,screenWidth() - 1, screenHeight() -1, BLACK);
  300.     sprintf(result, "    GAME OVER !!!");
  301.     textout((screenWidth() - 1) / 2 - 100,(screenHeight() - 1) / 2, result, MAGENTA);
  302.     updateScreen();
  303.     sleep(SLEEP);
  304. }
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