Advertisement
Mary_99

TEtris part 1

Apr 10th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.56 KB | None | 0 0
  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. #define ARENA_FRAME_COLOUR WHITE
  15. #define GROUND_COLOR BLACK
  16.  
  17. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char piece[4][4], char move);
  18. int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4]);
  19. void startingPiecePosition(int area [GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char thatPiece[4][4], enum color colour);
  20. void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4]);
  21. void endingGame();
  22. void checkingBoundry();
  23.  
  24. //------global variables ---------//
  25. int xLeftUpperCordinate = 100;
  26. int yLeftUpperCordinate = 65;
  27.  
  28. int main ()
  29. {
  30.     int columnCounter;
  31.     int rowCounter;
  32.     int area[GAME_WIDTH][GAME_HEIGHT][3];
  33.     int whichPiece;
  34.     int nextPiece;
  35.  
  36.     initGraph();
  37.  
  38.     for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  39.     {
  40.         for(rowCounter=0;rowCounter<GAME_HEIGHT;rowCounter++)
  41.         {
  42.             area [columnCounter][rowCounter][0] = xLeftUpperCordinate + columnCounter * SQUARE_SIZE;
  43.             area [columnCounter][rowCounter][1] = yLeftUpperCordinate + rowCounter * SQUARE_SIZE;
  44.             area [columnCounter][rowCounter][2] = 0;
  45.         }
  46.     }
  47.     whichPiece = (int)rand()%7;
  48.    
  49.     while(1)
  50.     {
  51.         nextPiece = (int)rand()%7;
  52.         updatingState(area, pieces[nextPiece][0]);
  53.         updateScreen();
  54.         if(isPossible(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], '0') == 0)
  55.         {
  56.             endingGame();
  57.             break;
  58.         }
  59.         startingPiecePosition(area, (GAME_WIDTH / 2) - 1, 0, pieces[whichPiece][0], MOVING_PIECE_COLOR);
  60.         updateScreen();
  61.  
  62.         if(play(area,(GAME_WIDTH / 2) - 1, 0, pieces[whichPiece]) == 0)
  63.         {
  64.             endingGame();
  65.             break;
  66.         }
  67.         whichPiece = nextPiece;
  68.     }
  69.     updateScreen();
  70.     return 0;
  71. }
  72.  
  73. void checkingBoundry()
  74. {
  75.     if(GAME_WIDTH < 10 || GAME_WIDTH > 20)
  76.     {
  77.         printf("Invalid GAME_WIDTH \n");
  78.         exit(3);
  79.     }
  80.  
  81.     if(GAME_HEIGHT < 10 || GAME_HEIGHT >20)
  82.     {
  83.         printf("Invalid GAME_HEIGHT\n");
  84.         exit(3);
  85.     }
  86. }
  87.  
  88.  
  89. int isPossible(int area[GAME_WIDTH][GAME_HEIGHT][3], int x,int y,char piece[4][4], char move)
  90. {
  91.     int columnCounter;
  92.     int rowCounter;
  93.  
  94.     switch(move)
  95.     {
  96.         case('l'):
  97.             x--;
  98.             break;
  99.         case('r'):
  100.             x++;
  101.             break; 
  102.         case('d'):
  103.             y++;
  104.             break;
  105.         case('0'):
  106.             break;
  107.     };
  108.    
  109.     if(x < 0 || x >= GAME_WIDTH || y >= GAME_HEIGHT)
  110.     {
  111.         return 0;
  112.     }
  113.    
  114.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  115.     {
  116.         for(rowCounter = 0 ; rowCounter < 4; rowCounter++)
  117.         {
  118.             if(piece[columnCounter][rowCounter]!= 0 && (x + columnCounter >= GAME_WIDTH || y + rowCounter >= GAME_HEIGHT))
  119.             {
  120.            
  121.                 return 0; //piece out of the game area
  122.             }
  123.             if ((piece[columnCounter][rowCounter]!= 0) && (area[x + columnCounter][y + rowCounter][2]!= 0))
  124.             {
  125.                 return 0; // piece reaches the non-empty place
  126.             }
  127.         }
  128.     }
  129.        
  130.     return 1;
  131. }
  132.  
  133.  
  134. int play(int area[GAME_WIDTH][GAME_HEIGHT][3], int x, int y, char whichPiece[4][4][4])
  135. {
  136.     int columnCounter;
  137.     int rowCounter;
  138.     int turnCounter;
  139.     int rotation = 0;
  140.  
  141.     while(1)
  142.     {
  143.         for(turnCounter = 0; turnCounter < 10; turnCounter++) //10moves 0.1s each,gives 1s time for the movement downward
  144.         {
  145.             if(isKeyDown('e') == 1) return 0; // to quit the game
  146.             switch(pollkey())
  147.             {
  148.                 case(SDLK_DOWN):
  149.                     while(isPossible(area, x, y, whichPiece[rotation], 'd') == 1)
  150.                     {
  151.                         startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR);//black piece
  152.                         y++; //move down   
  153.                         startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  154.                         updateScreen();
  155.                     }
  156.                     goto storingPices;
  157.  
  158.                 case(SDLK_RIGHT):
  159.                     if(isPossible(area, x, y, whichPiece[rotation], 'r') == 1)
  160.                     {
  161.                         startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR);
  162.                         x++;
  163.                         startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  164.                         updateScreen();
  165.                     }break;
  166.                 case(SDLK_LEFT):
  167.                     if(isPossible(area, x, y, whichPiece[rotation],'l') == 1)
  168.                     {
  169.                         startingPiecePosition(area, x, y, whichPiece[rotation], GROUND_COLOR);
  170.                         x--;
  171.                         startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  172.                         updateScreen();
  173.                     }break;
  174.                    
  175.                     /*add here case whith rotation*/
  176.             };
  177.             usleep(PICES_TIME);
  178.         }
  179.         if(isPossible(area, x, y, whichPiece[rotation],'d') == 1)
  180.         {
  181.             startingPiecePosition(area, x, y,whichPiece[rotation], GROUND_COLOR);
  182.             y++; //not accelerated down
  183.             startingPiecePosition(area, x, y, whichPiece[rotation], MOVING_PIECE_COLOR);
  184.             updateScreen();
  185.         }
  186.         else break;
  187.     }
  188. storingPices:
  189.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  190.     {
  191.         for(rowCounter = 0;rowCounter < 4; rowCounter++)
  192.         {
  193.             if(whichPiece[rotation][columnCounter][rowCounter] != 0)
  194.             {
  195.                 area[x+columnCounter][y+rowCounter][2] = STATIC_PIECE_COLOR;
  196.             }
  197.         }
  198.     }
  199.     return 1;
  200. }
  201.  
  202.  
  203. void updatingState(int area [GAME_WIDTH][GAME_HEIGHT][3], char nextPiece [4][4])
  204. {
  205.     int columnCounter;
  206.     int rowCounter;
  207.     char result[31];
  208.  
  209.  
  210.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, GROUND_COLOR);
  211.  
  212.     textout(0, 10, "Use arrows on your keyboard to move the piece and space to rotate it.", ARENA_FRAME_COLOUR);
  213.     textout(0, 20, "To Accelerate the pice press downn arrow.TO END THE GAME PRESS 'e'", ARENA_FRAME_COLOUR);
  214.    
  215.  
  216.     filledRect(xLeftUpperCordinate - 5, yLeftUpperCordinate, xLeftUpperCordinate - 1, yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE) + 4,
  217.                ARENA_FRAME_COLOUR);
  218.  
  219.     filledRect(xLeftUpperCordinate, yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE), xLeftUpperCordinate + (GAME_WIDTH * SQUARE_SIZE) - 1,
  220.                yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
  221.    
  222.     filledRect(xLeftUpperCordinate + (GAME_WIDTH * SQUARE_SIZE), yLeftUpperCordinate, xLeftUpperCordinate + (GAME_WIDTH*SQUARE_SIZE) + 4,
  223.                yLeftUpperCordinate + (GAME_HEIGHT * SQUARE_SIZE) + 4, ARENA_FRAME_COLOUR);
  224.  
  225.     // static pieces on the game's area
  226.     for(columnCounter = 0; columnCounter < GAME_WIDTH; columnCounter++)
  227.     {
  228.         for(rowCounter = 0; rowCounter < GAME_HEIGHT; rowCounter++)
  229.         {
  230.             filledRect(area[columnCounter][rowCounter][0] + 1, area[columnCounter][rowCounter][1] + 1,
  231.                        area[columnCounter][rowCounter][0] + SQUARE_SIZE - 2, area[columnCounter][rowCounter][1] + SQUARE_SIZE - 2,
  232.                        area[columnCounter][rowCounter][2]);
  233.         }
  234.     }
  235.  
  236.     //next pice
  237.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  238.     {
  239.         for(rowCounter = 0; rowCounter < 4; rowCounter++)
  240.         {
  241.             if (nextPiece[columnCounter][rowCounter]!= 0)
  242.             {
  243.                 filledRect(area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 11, area[columnCounter][rowCounter][1] + 11,
  244.                            area[columnCounter][rowCounter][0] + (GAME_WIDTH * SQUARE_SIZE) + 28, area[columnCounter][rowCounter][1] + 28,
  245.                            MOVING_PIECE_COLOR);
  246.             }
  247.         }
  248.     }
  249.  
  250. }
  251.  
  252.  
  253. 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.
  254. {
  255.     int columnCounter;
  256.     int rowCounter;
  257.  
  258.     for(columnCounter = 0; columnCounter < 4; columnCounter++)
  259.     {
  260.         for(rowCounter = 0;rowCounter < 4; rowCounter++)
  261.         {
  262.             if(piece[columnCounter][rowCounter] != 0) /*draws pices of when their colour is other than black */
  263.             {
  264.                 filledRect(area[x + columnCounter][y + rowCounter][0] + 1, area[x + columnCounter][y + rowCounter][1] + 1,
  265.                            area[x + columnCounter][y + rowCounter][0] + SQUARE_SIZE - 2, area[x + columnCounter][y + rowCounter][1] + SQUARE_SIZE - 2, colour);
  266.             }
  267.         }
  268.     }
  269. }
  270.  
  271.  
  272. void endingGame()
  273. {
  274.     char result[31];
  275.    
  276.     filledRect(0, 0,screenWidth() - 1, screenHeight() -1, GROUND_COLOR);
  277.     sprintf(result, "    GAME OVER !!!");
  278.     textout((screenWidth() - 1) / 2 - 100,(screenHeight() - 1) / 2, result, MAGENTA);
  279.     updateScreen();
  280.     sleep(SLEEP);
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement