Advertisement
Mary_99

tetris part 1a

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