SHARE
TWEET

tetris 100%

Mary_99 May 8th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "primlib.h"
  2. #include "pieces.inl"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <stdbool.h>
  8.  
  9. #define COLOUR_BACKGROUND BLACK
  10. #define COLOUR_FRAME WHITE
  11. #define COLOUR_PIECE_STATIC BLUE
  12. #define COLOUR_PIECE_MOVING MAGENTA
  13. #define COLOUR_PIECE_COVERED RED
  14. #define COLOUR_PIECE_FRAME BLACK
  15. #define COLOUR_TEXT MAGENTA
  16.  
  17. #define COLUMNS 10
  18. #define ROWS 20
  19. #define BLOCK_SIZE 20
  20. #define NUMBER_OF_PIECES 7
  21. #define NUMBER_OF_ROTATIONS 4
  22. #define PIECE_ARRAY_SIZE 4
  23. #define FALLING_RATE 300
  24. #define LOOP_ITERATION_TIME 50
  25.  
  26. #define FRAME_THICKNESS 3
  27. #define NUMBER_OF_END_TEXTS 3
  28. #define INSTRUCTION_SPACING 10
  29.  
  30. #define FONT_SIZE 8
  31. #define EMPTY_SPACE 0
  32. #define MOVEMENT_LEFT -1
  33. #define MOVEMENT_RIGHT 1
  34.  
  35. #define TEXT_INITIAL_CONTENT "PRESS ANY KEY TO START"
  36. #define TEXT_NEXT_PIECE_CONTENT "NEXT PIECE:"
  37. #define TEXT_GAMEOVER_CONTENT "GAME OVER"
  38. #define TEXT_INSTRUCTION1_CONTENT "ESC TO CLOSE"
  39. #define TEXT_INSTRUCTION2_CONTENT "SPACE TO TRY AGAIN"
  40.  
  41. #define BOARD_LEFTX (screenWidth() - sizeOfWindowNextPiece) / 2 - (calculatedBlockSize * COLUMNS) / 2
  42. #define BOARD_BOTTOMY screenHeight() / 2 + (calculatedBlockSize * ROWS)/  2
  43. #define BOARD_RIGHTX (screenWidth() - sizeOfWindowNextPiece) /2 + (calculatedBlockSize * COLUMNS) / 2
  44. #define BOARD_TOPY screenHeight() / 2 - (calculatedBlockSize * ROWS) / 2
  45. #define BOARD_FRAME_LEFTX BOARD_LEFTX - FRAME_THICKNESS
  46. #define BOARD_FRAME_BOTTOMY BOARD_BOTTOMY + FRAME_THICKNESS
  47. #define BOARD_FRAME_RIGHTX BOARD_RIGHTX + FRAME_THICKNESS
  48. #define BOARD_FRAME_TOPY BOARD_TOPY - FRAME_THICKNESS
  49.  
  50. #define GAME_OVER_BOX_LEFT_X BOARD_FRAME_LEFTX
  51. #define GAME_OVER_BOX_BOTTOM_Y INSTRUCTION2_TEXT_Y + INSTRUCTION_SPACING + FONT_SIZE
  52. #define GAME_OVER_BOX_RIGHT_X BOARD_FRAME_RIGHTX
  53. #define GAME_OVER_BOX_TOP_Y screenHeight()/2-NUMBER_OF_END_TEXTS * INSTRUCTION_SPACING
  54. #define GAME_OVER_FRAME_LEFT_X BOARD_FRAME_LEFTX
  55. #define GAME_OVER_FRAME_BOTTOM_Y GAME_OVER_BOX_BOTTOM_Y + FRAME_THICKNESS
  56. #define GAME_OVER_FRAME_RIGHT_X BOARD_FRAME_RIGHTX
  57. #define GAME_OVER_FRAME_TOP_Y GAME_OVER_BOX_TOP_Y - FRAME_THICKNESS
  58.  
  59. #define NEXT_PIECE_BOX_LEFT_X NEXT_PIECE_FRAME_LEFT_X + FRAME_THICKNESS
  60. #define NEXT_PIECE_BOX_BOTTOM_Y NEXT_PIECE_FRAME_BOTTOM_Y - FRAME_THICKNESS
  61. #define NEXT_PIECE_BOX_RIGHT_X NEXT_PIECE_FRAME_RIGHT_X - FRAME_THICKNESS
  62. #define NEXT_PIECE_BOX_TOP_Y NEXT_PIECE_FRAME_TOP_Y + FRAME_THICKNESS
  63. #define NEXT_PIECE_FRAME_LEFT_X screenWidth() - sizeOfWindowNextPiece
  64. #define NEXT_PIECE_FRAME_BOTTOM_Y NEXT_PIECE_FRAME_TOP_Y + sizeOfWindowNextPiece - BORDER_TO_BOARD_DISTANCE / 2
  65. #define NEXT_PIECE_FRAME_RIGHT_X NEXT_PIECE_FRAME_LEFT_X + sizeOfWindowNextPiece - BORDER_TO_BOARD_DISTANCE / 2
  66. #define NEXT_PIECE_FRAME_TOP_Y BOARD_FRAME_TOPY+2*INSTRUCTION_SPACING
  67.  
  68. #define INITIAL_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_INITIAL_CONTENT) / 2  * FONT_SIZE
  69. #define INITIAL_TEXT_Y screenHeight() / 3
  70. #define NEXT_PIECE_TEXT_X NEXT_PIECE_FRAME_LEFT_X
  71. #define NEXT_PIECE_TEXT_Y BOARD_FRAME_TOPY
  72. #define GAME_OVER_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_GAMEOVER_CONTENT) / 2 * FONT_SIZE
  73. #define GAME_OVER_TEXT_Y GAME_OVER_BOX_TOP_Y + INSTRUCTION_SPACING
  74. #define INSTRUCTION1_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_INSTRUCTION1_CONTENT) / 2 * FONT_SIZE
  75. #define INSTRUCTION1_TEXT_Y GAME_OVER_TEXT_Y + 2 * INSTRUCTION_SPACING
  76. #define INSTRUCTION2_TEXT_X (screenWidth() - sizeOfWindowNextPiece) / 2 - (float)strlen(TEXT_INSTRUCTION2_CONTENT) / 2 * FONT_SIZE
  77. #define INSTRUCTION2_TEXT_Y INSTRUCTION1_TEXT_Y + INSTRUCTION_SPACING
  78.  
  79. #define BLOCK_X BOARD_LEFTX + calculatedBlockSize * boardXPosition
  80. #define BLOCK_Y BOARD_BOTTOMY - calculatedBlockSize * boardYPosition
  81. #define NEXT_PIECE_BLOCK_X NEXT_PIECE_BOX_LEFT_X + BLOCK_SIZE * boardXPosition + 10
  82. #define NEXT_PIECE_BLOCK_Y NEXT_PIECE_BOX_TOP_Y + BLOCK_SIZE * (boardYPosition + 1) + 10
  83.  
  84. #define SPAWN_BOARD_POSITION COLUMNS / 2 - PIECE_ARRAY_SIZE / 4
  85. #define NEXT_PIECE_NOT_DISPLAYED NUMBER_OF_PIECES + 1
  86. #define BLOCK_COVERED 2
  87. #define BORDER_TO_BOARD_DISTANCE 4 * FRAME_THICKNESS
  88.  
  89.  
  90. void initialize(void);
  91. void randomSpawn(void);
  92. void drawing(void);
  93. void falling(void);
  94. void moving(void);
  95. void rotating(void);
  96. void drop(void);
  97. void cleaningLine(void);
  98. void gameOver(void);
  99. void checkingBoundry(void);
  100.  
  101. int movingGameState[COLUMNS][ROWS];
  102. int staticGameState[COLUMNS][ROWS];
  103.  
  104. int pressedButton,  timeCounter, currentMovingPiece, nextMovingPiece;
  105. int currentMovingPieceRotation, nextMovingPieceRotation, calculatedBlockSize, sizeOfWindowNextPiece;
  106.  
  107. bool checkSpawn, loseCondition;
  108.  
  109. int main(int argc, char* argv[])
  110. {
  111.     if(initGraph())
  112.     {
  113.         exit(3);
  114.     }
  115.  
  116.     do
  117.     {
  118.         initialize();
  119.         pressedButton = pollkey();
  120.         if(pressedButton == SDLK_ESCAPE)
  121.         {
  122.             exit(3);
  123.         }
  124.         else if((pressedButton != -1) && (pressedButton != SDLK_ESCAPE))
  125.         {
  126.             nextMovingPiece = rand()%NUMBER_OF_PIECES;
  127.             nextMovingPieceRotation = rand()%NUMBER_OF_ROTATIONS;
  128.             do
  129.             {
  130.                 if(checkSpawn)
  131.                 {
  132.                     randomSpawn();
  133.                 }
  134.                 if(loseCondition)
  135.                 {
  136.                 gameOver();
  137.                 break;
  138.                 }
  139.  
  140.                 drawing();
  141.                 updateScreen();
  142.                 pressedButton = pollkey();
  143.  
  144.                 switch(pressedButton)
  145.                 {
  146.                     case SDLK_LEFT:
  147.                     case SDLK_RIGHT:
  148.                         moving();
  149.                         break;
  150.                     case SDLK_SPACE:
  151.                         rotating();
  152.                         break;
  153.                     case SDLK_DOWN:
  154.                         drop();
  155.                         break;
  156.                     case SDLK_ESCAPE:
  157.                         exit(3);
  158.                 }
  159.  
  160.                 if(timeCounter >= FALLING_RATE)
  161.                 {
  162.                     falling();
  163.                 }
  164.                 cleaningLine();
  165.  
  166.                 SDL_Delay(LOOP_ITERATION_TIME);
  167.  
  168.                 timeCounter += LOOP_ITERATION_TIME;
  169.             }while(1);
  170.         }
  171.     }while(1);
  172.  
  173.   return 0;
  174. }
  175.  
  176.  
  177. void initialize()
  178. {
  179.     int initialHorizontalPosition;  
  180.     int initialVerticalPosition;
  181.  
  182.     srand(time(NULL));
  183.     checkSpawn = true;
  184.     timeCounter = 0;
  185.     currentMovingPiece = 0;
  186.     currentMovingPieceRotation = 0;
  187.     loseCondition = false;
  188.     nextMovingPiece = NEXT_PIECE_NOT_DISPLAYED;
  189.  
  190.     for(initialHorizontalPosition = 0; initialHorizontalPosition < COLUMNS; initialHorizontalPosition++)
  191.     {
  192.         for(initialVerticalPosition = 0; initialVerticalPosition < ROWS; initialVerticalPosition++)
  193.         {
  194.             movingGameState[initialHorizontalPosition][initialVerticalPosition] = EMPTY_SPACE;
  195.             staticGameState[initialHorizontalPosition][initialVerticalPosition] = EMPTY_SPACE;
  196.         }
  197.     }
  198.  
  199.     sizeOfWindowNextPiece = BORDER_TO_BOARD_DISTANCE + (PIECE_ARRAY_SIZE + 1) * BLOCK_SIZE;
  200.  
  201.     if(COLUMNS > ROWS)
  202.     {
  203.         calculatedBlockSize = (screenWidth() - sizeOfWindowNextPiece-  BORDER_TO_BOARD_DISTANCE) / (COLUMNS);
  204.     }else if(ROWS >= COLUMNS)
  205.     {
  206.     calculatedBlockSize = (screenHeight() - BORDER_TO_BOARD_DISTANCE) / (ROWS);
  207.     }
  208.  
  209.     drawing();
  210.     textout(INITIAL_TEXT_X, INITIAL_TEXT_Y ,TEXT_INITIAL_CONTENT, COLOUR_TEXT);
  211.     updateScreen();
  212. }
  213.  
  214.  
  215. void randomSpawn()
  216. {
  217.     int arrayX;
  218.     int arrayPositionY;
  219.     int arrayPieceY;
  220.  
  221.     currentMovingPiece = nextMovingPiece;
  222.     nextMovingPiece = rand()%NUMBER_OF_PIECES;
  223.     currentMovingPieceRotation = nextMovingPieceRotation;
  224.     nextMovingPieceRotation = rand()%NUMBER_OF_ROTATIONS;
  225.  
  226.     for(arrayX = 0; arrayX < PIECE_ARRAY_SIZE; arrayX++)
  227.     {
  228.         for(arrayPositionY = 0, arrayPieceY = PIECE_ARRAY_SIZE - 1; arrayPositionY < PIECE_ARRAY_SIZE; arrayPositionY++, arrayPieceY--)
  229.         {
  230.             if((staticGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE + arrayPositionY] != 0) && (pieces[currentMovingPiece][currentMovingPieceRotation][arrayX][arrayPieceY] != 0))
  231.             {
  232.                 staticGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE + arrayPositionY] = 0;
  233.                 movingGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE + arrayPositionY] = BLOCK_COVERED;
  234.                 loseCondition = true;
  235.             }
  236.             else if(staticGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE+arrayPositionY] == 0)
  237.                     {
  238.                     movingGameState[SPAWN_BOARD_POSITION + arrayX][ROWS-PIECE_ARRAY_SIZE+arrayPositionY] = pieces[currentMovingPiece][currentMovingPieceRotation]   [arrayX][arrayPieceY];
  239.                     }
  240.         }
  241.     }
  242.  
  243.     checkSpawn = false;
  244. }
  245.  
  246.  
  247. void drawing()
  248. {
  249.     int boardXPosition;
  250.     int boardYPosition;
  251.  
  252.     filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, COLOUR_BACKGROUND);
  253.  
  254.     filledRect(BOARD_FRAME_LEFTX, BOARD_FRAME_BOTTOMY, BOARD_FRAME_RIGHTX, BOARD_FRAME_TOPY, COLOUR_FRAME);
  255.     filledRect(BOARD_LEFTX, BOARD_BOTTOMY, BOARD_RIGHTX, BOARD_TOPY, COLOUR_BACKGROUND);
  256.  
  257.     for(boardXPosition = 0; boardXPosition < COLUMNS; boardXPosition++)
  258.     {
  259.         for(boardYPosition = 0; boardYPosition < ROWS; boardYPosition++)
  260.         {
  261.             if(movingGameState[boardXPosition][boardYPosition] > 0)
  262.             {
  263.                 if(movingGameState[boardXPosition][boardYPosition] == BLOCK_COVERED)
  264.                 {
  265.                     filledRect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_COVERED);
  266.                     rect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_FRAME);
  267.                 }
  268.                 else
  269.                 {
  270.                     filledRect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_MOVING);
  271.                     rect(BLOCK_X, BLOCK_Y, BLOCK_X + calculatedBlockSize, BLOCK_Y - calculatedBlockSize, COLOUR_PIECE_FRAME);
  272.                 }
  273.             }
  274.             else if(staticGameState[boardXPosition][boardYPosition] > 0)
  275.             {
  276.                 filledRect(BLOCK_X,BLOCK_Y,BLOCK_X+calculatedBlockSize,BLOCK_Y-calculatedBlockSize,COLOUR_PIECE_STATIC);
  277.                 rect(BLOCK_X,BLOCK_Y,BLOCK_X+calculatedBlockSize,BLOCK_Y-calculatedBlockSize,COLOUR_PIECE_FRAME);
  278.             }
  279.         }
  280.     }
  281.  
  282.     filledRect(NEXT_PIECE_FRAME_LEFT_X, NEXT_PIECE_FRAME_BOTTOM_Y, NEXT_PIECE_FRAME_RIGHT_X, NEXT_PIECE_FRAME_TOP_Y, COLOUR_FRAME);
  283.     filledRect(NEXT_PIECE_BOX_LEFT_X, NEXT_PIECE_BOX_BOTTOM_Y, NEXT_PIECE_BOX_RIGHT_X, NEXT_PIECE_BOX_TOP_Y, COLOUR_BACKGROUND);
  284.     textout(NEXT_PIECE_TEXT_X, NEXT_PIECE_TEXT_Y, TEXT_NEXT_PIECE_CONTENT, COLOUR_TEXT);
  285.  
  286.  
  287.     if(nextMovingPiece != NEXT_PIECE_NOT_DISPLAYED)
  288.     {
  289.         for(boardXPosition = 0; boardXPosition < PIECE_ARRAY_SIZE; boardXPosition++)
  290.         {
  291.             for(boardYPosition = 0; boardYPosition < PIECE_ARRAY_SIZE; boardYPosition++)
  292.             {
  293.                 if(pieces[nextMovingPiece][nextMovingPieceRotation][boardXPosition][boardYPosition] > 0)
  294.                 {
  295.                     filledRect(NEXT_PIECE_BLOCK_X, NEXT_PIECE_BLOCK_Y, NEXT_PIECE_BLOCK_X + BLOCK_SIZE,
  296.                                NEXT_PIECE_BLOCK_Y -BLOCK_SIZE, COLOUR_PIECE_MOVING);
  297.                     rect(NEXT_PIECE_BLOCK_X, NEXT_PIECE_BLOCK_Y, NEXT_PIECE_BLOCK_X + BLOCK_SIZE,
  298.                          NEXT_PIECE_BLOCK_Y - BLOCK_SIZE, COLOUR_PIECE_FRAME);
  299.                 }
  300.             }
  301.         }
  302.     }
  303. }
  304.  
  305.  
  306. void falling(void)
  307. {
  308.     int fallingX, fallingY;
  309.     int convertToStatic = 0;
  310.  
  311.     for(fallingY = 0; fallingY < ROWS; fallingY++)
  312.     {
  313.         for(fallingX = 0; fallingX < COLUMNS; fallingX++)
  314.         {
  315.             if(((fallingY == 0) && (movingGameState[fallingX][fallingY] > 0 )) || ((fallingY > 0) && (movingGameState[fallingX][fallingY] > 0) && (staticGameState[fallingX][fallingY - 1]> 0)))
  316.             {
  317.                 convertToStatic = 1;
  318.                 break;
  319.             }
  320.         }
  321.         if(convertToStatic)
  322.         {
  323.             break;
  324.         }
  325.     }
  326.  
  327.     for(fallingY = 0; fallingY < ROWS; fallingY++)
  328.     {
  329.         for(fallingX = 0;fallingX < COLUMNS; fallingX++)
  330.         {
  331.             if((convertToStatic == 1) && (movingGameState[fallingX][fallingY] > 0))
  332.             {
  333.                 staticGameState[fallingX][fallingY] = movingGameState[fallingX][fallingY];
  334.                 movingGameState[fallingX][fallingY] = 0;
  335.                 checkSpawn = true;
  336.             }
  337.             else if((convertToStatic == 0) && (movingGameState[fallingX][fallingY] > 0))
  338.             {
  339.                 movingGameState[fallingX][fallingY - 1] = movingGameState[fallingX][fallingY];
  340.                 movingGameState[fallingX][fallingY] = 0;
  341.                 }
  342.         }
  343.     }
  344.  
  345.     timeCounter = 0;
  346. }
  347.  
  348.  
  349. void moving(void)
  350. {
  351.     int directionOfMovement, movementXPosition, movementYPosition, moveable = 1;
  352.  
  353.     if(pressedButton==SDLK_LEFT)
  354.     {  
  355.         directionOfMovement = MOVEMENT_LEFT;
  356.     }
  357.     else if(pressedButton == SDLK_RIGHT)
  358.     {
  359.         directionOfMovement = MOVEMENT_RIGHT;
  360.     }
  361.  
  362.     for(movementYPosition = 0; movementYPosition < ROWS; movementYPosition++)
  363.     {
  364.         if(directionOfMovement > 0)
  365.         {
  366.             for(movementXPosition = COLUMNS - 1; movementXPosition >= 0; movementXPosition--)
  367.             {
  368.                 if((movingGameState[movementXPosition][movementYPosition] > 0) &&((movementXPosition == COLUMNS - 1)||(staticGameState[movementXPosition+directionOfMovement][movementYPosition] > 0)))
  369.                 {
  370.                     moveable = 0;
  371.                     break;
  372.                 }
  373.             }
  374.         }
  375.         else if(directionOfMovement < 0)
  376.         {
  377.             for(movementXPosition = 0; movementXPosition < COLUMNS; movementXPosition++)
  378.             {
  379.                 if((movingGameState[movementXPosition][movementYPosition] > 0) && ((movementXPosition == 0)||(staticGameState[movementXPosition+directionOfMovement][movementYPosition] > 0)))
  380.                 {
  381.                     moveable = 0;
  382.                     break;
  383.                 }
  384.             }
  385.         }
  386.         if(moveable == 0)
  387.         {
  388.             break;
  389.         }
  390.     }
  391.  
  392.     if(moveable)
  393.     {
  394.         for(movementYPosition = 0;movementYPosition < ROWS; movementYPosition++)
  395.         {
  396.             if(directionOfMovement > 0)
  397.             {
  398.                 for(movementXPosition = COLUMNS - 1;movementXPosition >= 0; movementXPosition--)
  399.                 {
  400.                     if(movingGameState[movementXPosition][movementYPosition] > EMPTY_SPACE)
  401.                     {
  402.                         movingGameState[movementXPosition + directionOfMovement][movementYPosition] = movingGameState[movementXPosition][movementYPosition];
  403.                         movingGameState[movementXPosition][movementYPosition] = EMPTY_SPACE;
  404.                     }
  405.                 }
  406.             }
  407.             else if(directionOfMovement < 0)
  408.             {
  409.                 for(movementXPosition = 0; movementXPosition < COLUMNS; movementXPosition++)
  410.                 {
  411.                     if(movingGameState[movementXPosition][movementYPosition] > EMPTY_SPACE)
  412.                     {
  413.                         movingGameState[movementXPosition+directionOfMovement][movementYPosition] = movingGameState[movementXPosition][movementYPosition];
  414.                         movingGameState[movementXPosition][movementYPosition] = EMPTY_SPACE;
  415.                     }
  416.                 }
  417.             }
  418.         }
  419.     }
  420. }
  421.  
  422.  
  423. void rotating(void)
  424. {
  425.     int nextPieceX,nextPieceY,xBeforeRotation,yBeforeRotation,newRotation,foundCenter,rotationIsValid, nextPieceStartX,nextPieceStartY,pieceWidth,pieceHeight,piece_inversed_height;
  426.  
  427.     if(currentMovingPieceRotation == NUMBER_OF_ROTATIONS - 1)
  428.     {
  429.         newRotation = 0;
  430.     }  
  431.     else if((currentMovingPieceRotation >= 0) && (currentMovingPieceRotation < NUMBER_OF_ROTATIONS - 1))
  432.     {
  433.         newRotation = currentMovingPieceRotation + 1;
  434.     }
  435.  
  436.     foundCenter = 0;
  437.    
  438.     for(pieceWidth = 0; pieceWidth < COLUMNS; pieceWidth++)
  439.     {
  440.         for(pieceHeight = 0;pieceHeight < ROWS; pieceHeight++)
  441.         {
  442.             if(movingGameState[pieceWidth][pieceHeight] > 1)  
  443.             {
  444.                 foundCenter = 1;
  445.                 xBeforeRotation = pieceWidth;
  446.                 yBeforeRotation = pieceHeight;
  447.                 break;
  448.             }
  449.         }
  450.         if(foundCenter)
  451.         {
  452.             break;
  453.         }
  454.     }
  455.  
  456.     foundCenter = 0;
  457.    
  458.     for(pieceWidth = 0; pieceWidth < PIECE_ARRAY_SIZE; pieceWidth++)
  459.     {
  460.         for(pieceHeight = 0; pieceHeight < PIECE_ARRAY_SIZE; pieceHeight++)
  461.         {
  462.             if(pieces[currentMovingPiece][newRotation][pieceWidth][pieceHeight] > 1)
  463.             {
  464.                 foundCenter = 1;
  465.                 nextPieceX = pieceWidth;
  466.                 nextPieceY = PIECE_ARRAY_SIZE - 1- pieceHeight;
  467.                 break;
  468.             }
  469.         }
  470.         if(foundCenter)
  471.         {
  472.             break;
  473.         }
  474.     }
  475.  
  476.     nextPieceStartX  = xBeforeRotation - nextPieceX;
  477.     nextPieceStartY = yBeforeRotation - nextPieceY;
  478.  
  479.     if(nextPieceStartX >= 0 && nextPieceStartY >= 0)
  480.     {
  481.         rotationIsValid = 1;
  482.         for(pieceWidth = 0; pieceWidth < PIECE_ARRAY_SIZE; pieceWidth++)
  483.         {
  484.             for(pieceHeight = 0,piece_inversed_height = PIECE_ARRAY_SIZE - 1;pieceHeight < PIECE_ARRAY_SIZE; pieceHeight++, piece_inversed_height--)
  485.             {
  486.                 if((pieces[currentMovingPiece][newRotation][pieceWidth][piece_inversed_height] > 0)&&
  487.                 (((nextPieceStartX + pieceWidth) >= COLUMNS)||((nextPieceStartY + pieceHeight) >= ROWS)||
  488.                 ((nextPieceStartX + pieceWidth) < 0)||((nextPieceStartY + pieceHeight) < 0)||
  489.                 (staticGameState[nextPieceStartX + pieceWidth][nextPieceStartY + pieceHeight] > 0)))
  490.                 {
  491.                 rotationIsValid = 0;
  492.                 break;
  493.                 }
  494.             }
  495.             if(rotationIsValid == 0)
  496.             {
  497.                 break;
  498.             }
  499.         }
  500.     }else rotationIsValid = 0;
  501.  
  502.     if(rotationIsValid)
  503.     {
  504.         for(pieceWidth = 0; pieceWidth < COLUMNS; pieceWidth++)
  505.         {
  506.             for(pieceHeight = 0;pieceHeight < ROWS; pieceHeight++)
  507.             {
  508.                 movingGameState[pieceWidth][pieceHeight] = EMPTY_SPACE;
  509.             }
  510.         }
  511.         for(pieceHeight = 0, piece_inversed_height = PIECE_ARRAY_SIZE - 1; pieceHeight < PIECE_ARRAY_SIZE; pieceHeight++, piece_inversed_height--)
  512.         {
  513.             for(pieceWidth = 0; pieceWidth < PIECE_ARRAY_SIZE; pieceWidth++)
  514.             {
  515.                 if((nextPieceStartX + pieceWidth >= 0) && (nextPieceStartX + pieceWidth < COLUMNS) &&
  516.                     (nextPieceStartY+pieceHeight >= 0) && (nextPieceStartY + pieceHeight < ROWS))
  517.                 {
  518.                     movingGameState[nextPieceStartX + pieceWidth][nextPieceStartY + pieceHeight] = pieces[currentMovingPiece][newRotation][pieceWidth][piece_inversed_height];
  519.                 }
  520.             }
  521.         }
  522.         currentMovingPieceRotation = newRotation;
  523.     }
  524. }
  525.  
  526.  
  527. void drop(void)
  528. {
  529.     while(checkSpawn == 0) falling();
  530.     timeCounter = 0;
  531. }
  532.  
  533.  
  534. void cleaningLine(void)
  535. {
  536.     int rowIndex,columnIndex,emptyColumnIndex,moveRowDown,takenSpacesCount;
  537.    
  538.     for(rowIndex = 0;rowIndex<ROWS;rowIndex++)
  539.     {
  540.         takenSpacesCount = 0;
  541.         for(columnIndex = 0; columnIndex < COLUMNS; columnIndex++)
  542.         {
  543.             if(staticGameState[columnIndex][rowIndex] > EMPTY_SPACE)
  544.             {
  545.                 takenSpacesCount++;
  546.             }
  547.             else if(staticGameState[columnIndex][rowIndex] == EMPTY_SPACE)
  548.             {    
  549.                 break;
  550.             }
  551.             if(takenSpacesCount == COLUMNS)
  552.                     for(emptyColumnIndex = 0; emptyColumnIndex < COLUMNS; emptyColumnIndex++)
  553.                     {
  554.                         staticGameState[emptyColumnIndex][rowIndex] = EMPTY_SPACE;
  555.                         for(moveRowDown = rowIndex; moveRowDown<ROWS - 1; moveRowDown++)
  556.                         {
  557.                             staticGameState[emptyColumnIndex][moveRowDown] = staticGameState[emptyColumnIndex][moveRowDown + 1];
  558.                         }
  559.                     }
  560.         }
  561.     }
  562. }
  563.  
  564.  
  565. void gameOver(void)
  566. {
  567.     nextMovingPiece = 0;
  568.     drawing();
  569.     filledRect(GAME_OVER_FRAME_LEFT_X, GAME_OVER_FRAME_BOTTOM_Y, GAME_OVER_FRAME_RIGHT_X, GAME_OVER_FRAME_TOP_Y, COLOUR_FRAME);
  570.     filledRect(GAME_OVER_BOX_LEFT_X, GAME_OVER_BOX_BOTTOM_Y, GAME_OVER_BOX_RIGHT_X, GAME_OVER_BOX_TOP_Y, COLOUR_BACKGROUND);
  571.     textout(GAME_OVER_TEXT_X,GAME_OVER_TEXT_Y,TEXT_GAMEOVER_CONTENT,COLOUR_TEXT);
  572.     textout(INSTRUCTION1_TEXT_X, INSTRUCTION1_TEXT_Y, TEXT_INSTRUCTION1_CONTENT, COLOUR_TEXT);
  573.     textout(INSTRUCTION2_TEXT_X, INSTRUCTION2_TEXT_Y, TEXT_INSTRUCTION2_CONTENT, COLOUR_TEXT);
  574.     updateScreen();
  575.     do{
  576.         pressedButton = pollkey();
  577.         if(pressedButton == SDLK_ESCAPE)
  578.         {
  579.             exit(3);
  580.         }
  581.         if(pressedButton == SDLK_SPACE)
  582.         {
  583.             break;
  584.         }
  585.     }while(1);
  586. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top