Advertisement
Mary_99

tetris almost

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