Advertisement
Mary_99

Tetris mysl

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